tus / TUSKit

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

Background uploads do not appear to work #127

Closed spatialbits closed 1 year ago

spatialbits commented 2 years ago

I am a Flutter developer integrating TUSKit into our app and having difficulties getting background uploading to work.

So I am attempting to run the example app just to observe the background capability, but it looks like it only runs while the app is in the foreground.

Running the TUSKit Demo app using latest master. The log output below is the result of the following sequence:

  1. Select an image from the picker.
  2. Allow upload to begin.
  3. Immediately switch to another app.
  4. Upload halts (or seems to, log output stops anyways).
  5. Switch back to demo app.
  6. A timeout error is logged
  7. A couldNotUploadFile error is logged.

I'm really new to iOS native and Swift and so this could be the result of me just misunderstanding of the library. If that is the case, I would very much appreciate some guidance on applying background uploads with TUSKit.

For reference, I have been following this guide that describes some common pitfalls with developing background uploads.

``` 2022-04-13 09:22:00.452918-0700 TUSKitExample[3564:1367287] [Picker] Showing picker unavailable UI (reason: still loading) with error: (null) Received 1 images TUSClient started upload, id is AF169ABE-0369-43FB-B9B1-CD5AA5EAC8DD TUSClient remaining is 1 TUSClient single upload progress: 4864 / 1283879 TUSClient total upload progress: 0 of 1283879 bytes. TUSClient single upload progress: 0 / 1283879 TUSClient total upload progress: 0 of 1283879 bytes. TUSClient single upload progress: 486400 / 1283879 TUSClient total upload progress: 0 of 1283879 bytes. TUSClient single upload progress: 512000 / 1283879 TUSClient total upload progress: 0 of 1283879 bytes. TUSClient single upload progress: 512000 / 1283879 TUSClient total upload progress: 512000 of 1283879 bytes. TUSClient single upload progress: 512000 / 1283879 TUSClient total upload progress: 512000 of 1283879 bytes. TUSClient single upload progress: 998400 / 1283879 TUSClient total upload progress: 512000 of 1283879 bytes. TUSClient single upload progress: 1024000 / 1283879 TUSClient total upload progress: 512000 of 1283879 bytes. TUSClient single upload progress: 1026468 / 1283879 TUSClient total upload progress: 1024000 of 1283879 bytes. TUSClient single upload progress: 1024000 / 1283879 TUSClient total upload progress: 1024000 of 1283879 bytes. 2022-04-13 09:22:10.397595-0700 TUSKitExample[3564:1369941] [connection] nw_read_request_report [C4] Receive failed with error "Software caused connection abort" 2022-04-13 09:22:10.401117-0700 TUSKitExample[3564:1369941] Task <31656C9F-C49B-4D8D-A1BE-122B37350C19>.<17> HTTP load failed, 260408/0 bytes (error code: -1005 [4:-4]) TUSClient single upload progress: 1270885 / 1283879 TUSClient total upload progress: 1024000 of 1283879 bytes. 2022-04-13 09:22:10.402273-0700 TUSKitExample[3564:1369971] Task <31656C9F-C49B-4D8D-A1BE-122B37350C19>.<17> finished with error [-1005] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=-4, NSUnderlyingError=0x281c4f630 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bb3415e3a20000000000000000}, _kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalUploadTask <31656C9F-C49B-4D8D-A1BE-122B37350C19>.<17>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalUploadTask <31656C9F-C49B-4D8D-A1BE-122B37350C19>.<17>" ), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=https://tusd.tusdemo.net/files/c9c23dcd1793ac8b297b6d35e0c1179b+IAoBKoX3r7J9hdg_IhmZD__79spDbbRe4hTqdQg.nfgSujmYkn4tzQ7Hsb5Y9TFwEZWG74hAwSmyxqgOjt5Rt5Wgj3CdGbml4q7Xr4Ziyq6fHO1KWRKAXRZ__KDDr.Zg, NSErrorFailingURLKey=https://tusd.tusdemo.net/files/c9c23dcd1793ac8b297b6d35e0c1179b+IAoBKoX3r7J9hdg_IhmZD__79spDbbRe4hTqdQg.nfgSujmYkn4tzQ7Hsb5Y9TFwEZWG74hAwSmyxqgOjt5Rt5Wgj3CdGbml4q7Xr4Ziyq6fHO1KWRKAXRZ__KDDr.Zg, _kCFStreamErrorDomainKey=4} TUSClient single upload progress: 1026468 / 1283879 TUSClient total upload progress: 1024000 of 1283879 bytes. TUSClient single upload progress: 1024000 / 1283879 TUSClient total upload progress: 1024000 of 1283879 bytes. 2022-04-13 09:22:10.860296-0700 TUSKitExample[3564:1369971] [tcp] tcp_input [C5.1:3] flags=[R] seq=2010685109, ack=4294967196, win=0 state=CLOSE_WAIT rcv_nxt=2010685871, snd_una=2624184247 TUSClient single upload progress: 1270885 / 1283879 TUSClient total upload progress: 1024000 of 1283879 bytes. 2022-04-13 09:22:41.364715-0700 TUSKitExample[3564:1369941] Task <0BB5EB6E-DC57-4FA4-B8E8-3EACD06133D2>.<18> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x281c4dbf0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalUploadTask <0BB5EB6E-DC57-4FA4-B8E8-3EACD06133D2>.<18>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalUploadTask <0BB5EB6E-DC57-4FA4-B8E8-3EACD06133D2>.<18>" ), NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=https://tusd.tusdemo.net/files/c9c23dcd1793ac8b297b6d35e0c1179b+IAoBKoX3r7J9hdg_IhmZD__79spDbbRe4hTqdQg.nfgSujmYkn4tzQ7Hsb5Y9TFwEZWG74hAwSmyxqgOjt5Rt5Wgj3CdGbml4q7Xr4Ziyq6fHO1KWRKAXRZ__KDDr.Zg, NSErrorFailingURLKey=https://tusd.tusdemo.net/files/c9c23dcd1793ac8b297b6d35e0c1179b+IAoBKoX3r7J9hdg_IhmZD__79spDbbRe4hTqdQg.nfgSujmYkn4tzQ7Hsb5Y9TFwEZWG74hAwSmyxqgOjt5Rt5Wgj3CdGbml4q7Xr4Ziyq6fHO1KWRKAXRZ__KDDr.Zg, _kCFStreamErrorDomainKey=4} TUSClient single upload progress: 1026468 / 1283879 TUSClient total upload progress: 1024000 of 1283879 bytes. TUSClient single upload progress: 1024000 / 1283879 TUSClient total upload progress: 1024000 of 1283879 bytes. 2022-04-13 09:22:41.931172-0700 TUSKitExample[3564:1370472] [tcp] tcp_input [C6.1:3] flags=[R] seq=3901238267, ack=4294967196, win=0 state=CLOSE_WAIT rcv_nxt=3901239029, snd_una=2220065410 TUSClient single upload progress: 1270885 / 1283879 TUSClient total upload progress: 1024000 of 1283879 bytes. 2022-04-13 09:23:12.388363-0700 TUSKitExample[3564:1370472] Task .<19> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x281ca3060 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalUploadTask .<19>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalUploadTask .<19>" ), NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=https://tusd.tusdemo.net/files/c9c23dcd1793ac8b297b6d35e0c1179b+IAoBKoX3r7J9hdg_IhmZD__79spDbbRe4hTqdQg.nfgSujmYkn4tzQ7Hsb5Y9TFwEZWG74hAwSmyxqgOjt5Rt5Wgj3CdGbml4q7Xr4Ziyq6fHO1KWRKAXRZ__KDDr.Zg, NSErrorFailingURLKey=https://tusd.tusdemo.net/files/c9c23dcd1793ac8b297b6d35e0c1179b+IAoBKoX3r7J9hdg_IhmZD__79spDbbRe4hTqdQg.nfgSujmYkn4tzQ7Hsb5Y9TFwEZWG74hAwSmyxqgOjt5Rt5Wgj3CdGbml4q7Xr4Ziyq6fHO1KWRKAXRZ__KDDr.Zg, _kCFStreamErrorDomainKey=4} TUSClient upload failed for AF169ABE-0369-43FB-B9B1-CD5AA5EAC8DD error couldNotUploadFile 2022-04-13 09:23:18.382887-0700 TUSKitExample[3564:1370472] [tcp] tcp_output [C6.1:3] flags=[R.] seq=2220101294, ack=3901239029, win=4096 state=CLOSED rcv_nxt=3901239029, snd_una=2220065410 2022-04-13 09:23:26.283814-0700 TUSKitExample[3564:1370472] [tcp] tcp_output [C5.1:3] flags=[R.] seq=2624221579, ack=2010685871, win=4096 state=CLOSED rcv_nxt=2010685871, snd_una=2624184247 ```
mabdullahsaeed commented 1 year ago

@spatialbits did you ever get any answer on this or to find a work around? The background uploads do nor work and couldNotUploadFile error is logged if app is backgrounded and then foregrounded during the upload.

donnywals commented 1 year ago

@mabdullahsaeed @spatialbits I'm not familiar with Flutter per se, but the current state of background uploads is that it uses background tasks which will not allow you to resume an in-progress upload in the background. Instead an upload that was scheduled using a background task would be executed at some time in the future (the system decides when).

We're currently in the process of looking into a transition back to background URLSessions which actually work as you'd expect. Unfortunately this isn't a quick fix that we can apply so it will take a while longer for us to get on this. (I've only recently started as a maintainer on the project, so apologies for some of the long standing questions in the issues).

Once we have a fix I will update this issue

Acconut commented 1 year ago

From https://github.com/tus/TUSKit/issues/128#issue-1214538206

@foxware00: I see we've got a new version using background support in iOS 13 and higher. However you cannot use background url sessions, something of a requirement if you're starting uploading in a share extension and then picking up where you left off in the main app.

Some of the old issues which were resolved, but have since been re-opened due to the recent 3.0.0 changes

https://github.com/tus/TUSKit/issues/57#issuecomment-610188074

Do you think we could move back to delegates rather than callbacks to allow for both background sessions and background tasks side by side?

abdulmomin703 commented 1 year ago

@donnywals any progress on transitioning to background URLSessions?

donnywals commented 1 year ago

I'm working on it but there are some pretty big differences between what we do now and how we need to handle background URLSession. Mainly due to the fact that we have to upload from files instead of from a stream of data.

TUSKit is something I work on part time so we're moving slowly but surely at this point.

donnywals commented 1 year ago

Hi @abdulmomin703, @spatialbits and @mabdullahsaeed.

I currently have a working implementation of background uploads that I'm testing and would love for you three to take it for a spin. Instead of pointing to a release version of TUSKit, can you use the dw/bg-urlsession branch to see if everything works as expected for you?

Please make sure to check out the README since there are a few minor steps to implement before background uploads can work.

Cheers!

Acconut commented 1 year ago

New background uploads are merged in https://github.com/tus/TUSKit/pull/161 and will be released soon! 🎉 Feel free to try it out.