The scenario: several requests discover an old token together
A mobile screen can start more than one authenticated request. If those requests receive an authorization failure around the same time, each might try to refresh the session independently. With rotating refresh credentials, that can create a race rather than a useful recovery. The application needs one coordinated renewal operation and a clear way for waiting callers to use its result.
The Movo-X iOS application has a portable Swift core with a session manager designed for this coordination. The selected API-client, session-manager and trust suites were rerun on Linux using fake transports and stores: 42 tests passed. This chapter concerns that core logic, not an iOS interface, live account or App Store acceptance result.
An actor still needs an explicit in-flight operation
The inspected session manager is an actor, which organizes access to its state. An asynchronous operation still yields while waiting for a response, so other callers can enter relevant work during that interval. Actor isolation is not a reason to assume that every network renewal automatically becomes one request.
The source therefore retains an in-flight refresh task. A caller that finds that task waits for the same result instead of starting another renewal. The tests exercise concurrent refresh callers and concurrent route authorization failures with a scripted refresher or transport. This makes the expected coordination an observable behaviour rather than a general claim that using actors removes all concurrency problems.
Recognize when the rejected token is already stale
A request can fail with an access token that was current when it began but has since been replaced. The inspected refresh method receives the rejected token context and compares it with the manager’s current state. If a newer token is already available, it can return that state rather than refresh again because of an older response.
This is a different case from joining a renewal that is still running. One caller may be waiting for a result; another may arrive after the result was already applied. A useful test set covers both timings. Otherwise a client can coordinate simultaneous failures correctly yet still perform unnecessary renewal when a delayed older request completes.
Keep refresh-task ownership clear when work finishes
The source clears the recorded in-flight task only when the finishing task is still the one recorded. That makes task ownership explicit across asynchronous completion. An older operation should not casually clear a newer operation’s coordination state merely because its own await has finished.
For an engineering review, distinguish the task identity from the credential identity and the account state. Each can answer a different question: which operation is being awaited, which credentials produced it and whether the user is still in the same session. A single “refreshing” Boolean would make those relationships harder to express and test.
A temporary failure should not automatically mean logout
The session manager distinguishes a definitive refresh refusal from network, throttling, server and unexpected processing failures under its implemented policy. The selected tests verify that transient cases preserve the session rather than turning every temporary error into a sign-out event. The client can then expose the appropriate retry or unavailable state.
That behaviour does not mean a protected operation can proceed without a valid access result. It preserves the session context while the failed operation remains a failure. Keep the error shown to the user, the stored session and the next permitted request as separate states. Retaining context is not the same as claiming the server authorized the original action.
Coordinate the API-client retry with the session manager
The selected API-client tests exercise a route failure followed by a refresh and a retry, using a fake transport that records requests. They also cover concurrent failures sharing the session-manager path. The test environment can inspect which operation was repeated and which credentials were used without contacting a real account service.
For a production integration, the retry must also preserve the meaning of any consequential request. A renewed credential does not make a failed mutation a new user intent. The operation’s request identity, body and server idempotency contract need to remain aligned. The portable tests are one evidence layer; the actual service and native lifecycle require their own acceptance cases.
What the portable run establishes
The rerun selected 22 API-client tests, 16 session-manager tests and four trust tests against the pinned Swift package. All passed with Swift 6.4.1 on Linux. The package isolates Foundation-based logic from the platform-specific interface and storage adapters, allowing these core cases to be exercised without a phone or real credentials.
That scope should remain visible in a handover. The run does not prove physical-device background behaviour, Keychain accessibility, current provider responses or the server’s complete session policy. Those are separate checks. The useful result is reproducible evidence for the selected request and state-management cases in the tested core environment.
A practical refresh-coordination delivery scope
Deliver the shared-task state map, stale-response handling, transient-failure policy and operation-retry contract. Include controlled concurrent tests with a scripted transport and a record of the source and toolchain used. Then identify the service, platform-adapter and device cases that remain before a release is accepted.
For a CTO, this turns intermittent “random logout” reports into an inspectable engineering problem. The goal is not simply making another refresh request succeed. It is coordinating callers and state transitions so an old response or temporary failure does not create an unintended session outcome.
Illustrative control flow
Pseudocode summarizing the decision boundary. It is not copied production code or a deployment instruction.
if rejected_token_is_older_than_current_state:
return current_session
if refresh_task_exists:
return await shared_refresh_task
shared_refresh_task = start_one_refresh(current_session)
return await shared_refresh_task
# Completion may clear only the task it owns.About this engineering series
Written by Omer Muneer Qazi from a review of Movo-X implementation and dated engineering records. This article explains source behaviour and historical observations; current production, clinical outcomes and store availability require their own verification. Examples are synthetic and proposed test cases are identified as such.
Explore the Movo-X engineering series → · Related implementation experience →
Discuss a similar engineering problem
Send the workflow, current platform and the decision your team needs to make. I can help define an implementation or review scope with concrete acceptance evidence.
Discuss your project on WhatsApp ↗