Skip to content

Problem with background file upload and token refresh handling #211

@Serg-Pogrebnyak

Description

@Serg-Pogrebnyak

Hello,
I'm encountering an issue when using background file uploads with token-based authorization.

In my app, uploads are authorized via a bearer token. For testing purposes, I've set short-lived tokens (30 seconds) to simulate token expiry during background uploads.

Each time an error occurs due to an expired token, I refresh the token manually, recreate the URLSessionConfiguration with the new token, and then reinitialize the TUSClient. This is necessary, otherwise the internal URLSession in TUSClient continues using the old token from its original configuration.

Setup example:

let urlSessionConfig = URLSessionConfiguration
    .background(withIdentifier: sessionId.uuidString)
urlSessionConfig.httpAdditionalHeaders = [
    "Authorization": accessAPIToken.token
]

tusClient = try TUSClient(
    server: tusServerURL,
    sessionIdentifier: UUID().uuidString,
    sessionConfiguration: urlSessionConfig,
    storageDirectory: tusDirectory
)

The issue

  • When the token expires and I recreate the TUSClient with a new session config and token:
  • The upload fails silently - no error is delivered via URLSessionDataDelegate or TUSClient.
  • The upload does not resume.
  • No error message is available, which leads to the app becoming stuck in a hanging state.
  • Calling stopAndCancelAll() doesn’t resolve the issue.
  • It seems that after recreating the client, the error from the background session is no longer captured.

Questions / Suggestions

  • Would it be possible to allow passing a delegate explicitly to TUSClient, so background errors can be handled externally?
  • Is there a recommended way to refresh the token without recreating the whole URLSessionConfiguration or TUSClient?
  • Alternatively, is there a way to invalidate and recreate a URLSession with a new header while keeping the same background identifier?

Any advice or guidance would be greatly appreciated - thank you!

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions