Crash on iOS when re-using background URLSession identifier
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 245
- Forks
- 128
- PR merge metrics
- No merged PRs in 30d
Description
TUSClient.deinit finishes and invalidates the session even when it's a background session. In my app I was re-using the same session ID after changing service environments (prod/staging/dev) and that causes this crash:
BackgroundSession <5B232D47-B8F1-4848-9A69-8196786E5921> attempted to create a NSURLSessionDataTask in a session that has been invalidated
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Task created in a session that has been invalidated'
*** First throw call stack:
(0x18ce25044 0x18a2bdabc 0x18e51c780 0x18e31997c 0x18e31607c 0x103cb2064 0x103ca919c 0x18e3170c4 0x18e31932c 0x1106bd630 0x1106dd500 0x1106b4944 0x103c98584 0x103cb2064 0x103ca091c 0x103ca15a4 0x103cad894 0x103caceb0 0x21720ca0c 0x21720caac)
libc++abi: terminating due to uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Task created in a session that has been invalidated'
*** First throw call stack:
(0x18ce25044 0x18a2bdabc 0x18e51c780 0x18e31997c 0x18e31607c 0x103cb2064 0x103ca919c 0x18e3170c4 0x18e31932c 0x1106bd630 0x1106dd500 0x1106b4944 0x103c98584 0x103cb2064 0x103ca091c 0x103ca15a4 0x103cad894 0x103caceb0 0x21720ca0c 0x21720caac)
terminating due to uncaught exception of type NSException
Should TUSClient.deinit only finish and invalidate tasks when it's a foreground session and its configuration identifier is nil? That fixes the issue here and I think it's correct, but wanted to talk it over before opening up a PR.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The affected entry point is TUSClient.deinit; inspect how the URLSession configuration identifier distinguishes background from foreground sessions. Reproduce the reuse case with a background identifier and verify that deinitialization no longer invalidates that session while foreground sessions still finish and invalidate their tasks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- mobile, networking
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100