tus / TUSKit

The tus client for iOS.
https://tus.io/
MIT License
209 stars 114 forks source link

Uploading stuck #159

Closed rick51231 closed 1 year ago

rick51231 commented 1 year ago

Hello

I'm using this library with Tusd When resuming near-completed (or completed) upload, sometimes it stucks and doesn't not send any delegate callbacks. self.tusClient.retry(id: tusId) (result = true) creates a StatusTask that returns success([]) in Scheduler.swift:104 and nothing happened after that. Is it bug or am i doing it wrong?

Thank you

donnywals commented 1 year ago

HI Rick, that sounds like it might be a bug. Could you share how you're resuming the upload exactly so I can attempt to reproduce and troubleshoot this?

lafalex commented 1 year ago

Hello, same bug on my side. This is how I resume an upload (after a lost connection for example) :

    guard let uploadID = uploadID else {
        return
    }

    self.state = .progress

    do {
        try tusClient.retry(id: uploadID)
    } catch {
        manageError()
    }
donnywals commented 1 year ago

Thanks! That looks like the way it should be done. I'm actually very close to a pretty big refactor that moves us to a background URLSession to have proper support for background uploads so I'll make sure to run some extra tests against this scenario as well

mike-switcherstudio commented 1 year ago

hey @donnywals, firstly, thanks for all the great work on the library, it is appreciated! I work with @lafalex and we don't perform uploads in the background, maybe your refactor would solve our issue, but maybe not. What do you think? Can we get an early cut of your refactor to try out? is dw/bg-urlsession a stable branch?

donnywals commented 1 year ago

Hi Mike! The background part of it is more that even supporting it required a pretty big refactor, so even if you don't do background uploads, the structural changes will still be relevant (and might fix the issue).

The dw/bg-urlsession branch should be mostly stable. It has a lot of debug logging noise right now but I try and avoid pushing to that branch if something's not quite working. It should definitely be good enough for you to run some tests. The last remaining issue I know off at this time is handling uploads that completed while the app was in the background. Since you wouldn't be using that feature right now I think it's safe to test this branch for now and validate whether it works for you.

rick51231 commented 1 year ago

Hi Donny! Sorry for the late answer, I really missed GitHub's notification

Here is an example that can reproduce the bug. The server side is made with tus-node-server

index.js Run: ```npm i @tus/server @tus/file-store && node index.js``` ```javascript const {Server} = require('@tus/server') const {FileStore} = require('@tus/file-store') const host = '127.0.0.1' const port = 1080 const server = new Server({ path: '/files', datastore: new FileStore({directory: './files'}), onUploadCreate: (req, res, upload) => { console.log('create'); console.log(upload); return res; }, onUploadFinish: (req, res, upload) => { console.log('finish'); console.log(upload); throw {status_code: 500, body: 'You shall not pass'}; } }) ```
ViewController.swift ```swift import UIKit import TUSKit class ViewController: UIViewController, TUSClientDelegate { var tusClient: TUSClient! override func viewDidLoad() { super.viewDidLoad() tusClient = try! TUSClient(server: URL(string: "http://127.0.0.1:1080/files")!, sessionIdentifier: "TUS DEMO", storageDirectory: URL(string: "TUS")!, chunkSize: 10) tusClient.delegate = self DispatchQueue.main.asyncAfter(deadline: .now() + 1) { let data = Data(String(repeating: "!", count: 30).utf8) let uploadId = try! self.tusClient.upload(data: data) print("Scheduled upload \(uploadId)") } } func didStartUpload(id: UUID, context: [String : String]?, client: TUSKit.TUSClient) { print("TUSClient started upload, id is \(id)") print("TUSClient remaining is \(client.remainingUploads)") } func didFinishUpload(id: UUID, url: URL, context: [String : String]?, client: TUSKit.TUSClient) { print("TUSClient finished upload, id is \(id) url is \(url)") print("TUSClient remaining is \(client.remainingUploads)") if client.remainingUploads == 0 { print("Finished uploading") } } func uploadFailed(id: UUID, error: Error, context: [String : String]?, client: TUSKit.TUSClient) { print("TUSClient upload failed for \(id) error \(error)") DispatchQueue.main.asyncAfter(deadline: .now() + 1) { let result = try! self.tusClient.retry(id: id) print("Resumed upload \(id) with status \(result)") } } func fileError(error: TUSKit.TUSClientError, client: TUSKit.TUSClient) { print("fileError: \(error)") } func totalProgress(bytesUploaded: Int, totalBytes: Int, client: TUSKit.TUSClient) { print("totalProgress: \(bytesUploaded) / \(totalBytes)") } func progressFor(id: UUID, context: [String : String]?, bytesUploaded: Int, totalBytes: Int, client: TUSKit.TUSClient) { print("progressFor: id:\(id) \(bytesUploaded) / \(totalBytes)") } } ```
Resulting output ``` Scheduled upload BB25A634-62C4-43D7-A771-8AECE781E72E TUSClient started upload, id is BB25A634-62C4-43D7-A771-8AECE781E72E TUSClient remaining is 1 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 0 / 30 totalProgress: 0 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 0 / 30 totalProgress: 0 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 9 / 30 totalProgress: 0 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 10 / 30 totalProgress: 0 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 10 / 30 totalProgress: 10 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 10 / 30 totalProgress: 10 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 19 / 30 totalProgress: 10 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 20 / 30 totalProgress: 10 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 20 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 20 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 29 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 30 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 20 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 30 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 30 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 30 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 20 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 20 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 29 / 30 totalProgress: 20 / 30 progressFor: id:BB25A634-62C4-43D7-A771-8AECE781E72E 30 / 30 totalProgress: 20 / 30 TUSClient upload failed for BB25A634-62C4-43D7-A771-8AECE781E72E error couldNotUploadFile Resumed upload BB25A634-62C4-43D7-A771-8AECE781E72E with status true ```

I also saw that there are already tags with versions 3.1.6 and 3.1.7, but there is no information in the release section. Is that how it should be? In 3.1.6+ I cannot reproduce this bug, so It might be already fixed in the 2ad3209400c22d1c8760156d4a4168a44ad064e3

donnywals commented 1 year ago

Hi @rick51231 thanks for getting back and re-testing! Given that you can no longer repro, I will close this issue for now. Feel free to reopen or create a new issue in case this comes up again.

For 3.1.6 and 3.1.7 you can see any fixes or changes in the CHANGELOG.md file: https://github.com/tus/TUSKit/blob/main/CHANGELOG.md

I'll take a look at the releases section and add the same information there in the future for convenience :)