[swift6] Fix urlsession losing a cancellation while an interceptor completes asynchronously

RO
10h ago
+1057-88

Fixes #25848. Follow-up to #25188 (fixes #25182), as discussed in https://github.com/OpenAPITools/openapi-generator/pull/25188#discussion_r4219559146.

With an OpenAPIInterceptor that completes intercept asynchronously (e.g. a token refresh), a cancel arriving before the data task exists was dropped and the request was sent anyway. The same happened when a cancel arrived while the retry interceptor was working.

RequestTask now remembers a cancel for the current execution:

  • cancel() records it. Once the data task exists it is stored first and the flag checked after, so a cancel that came earlier cancels the task before it is resumed, and one that comes later finds it.
  • A cancelled request does not call the retry interceptor, and .retry is ignored if a cancel lands while the interceptor decides.
  • A cancelled execution always ends with ErrorResponse.error(-1, nil, nil, URLError(.cancelled)), like a cancelled data task: also when the interceptor fails after the cancel, or when the cancel lands while a retry is decided.

Reactive wrappers. #25182 ruled out a RequestTask that remembers a cancel, because the reactive wrappers call cancel() when a subscription ends and can execute a builder again. This change scopes the cancel to one execution, so that keeps working:

  • The public execute(completion:) clears the flag before starting, so a cancel from an earlier subscription never stops the next one. With Combine's Deferred, every subscription re-executes the same builder; RxSwift builds a new one per subscription, so its late cancel() on disposal can't reach a retry at all.
  • Retries still go through execute(completion:), so subclass overrides see them, but a private marker makes that call keep the flag, so a cancel during a retry still holds.
  • testExecuteAfterCancelSendsRequest covers re-executing a builder after a cancel. Separately, I checked RxSwift .retry() and Combine Deferred .retry() after a failed request, with synchronous and asynchronous interceptors: every retry was sent and succeeded.

The alamofire library is unaffected: its request exists as soon as execute(completion:) returns.

Known limit: the flag has no execution identity. A late cancel() from an earlier execution that lands after a new execute() has cleared the flag cancels the new run, and the completion of an earlier cancelled task that arrives after that can still be retried. The standard wrappers rarely hit this, and a generation token would close it; left out to keep this change small.

Tests: RequestCancellationTests in the asyncAwaitLibrary sample covers cancel during an async intercept (also when the interceptor then fails), cancel before and during a retry, an uncancelled retry going through an overridden execute(completion:), and re-executing a builder after a cancel. They run without a server. The three cancellation tests fail without this change; the retry and re-execute tests guard behavior that already worked. The sample's host app traps at launch on an iOS 27 simulator because it doesn't adopt the scene lifecycle (pre-existing), so they were run on iOS 26.5.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/swift6*.yaml || exit
    
    Commit all changed files. Built with JDK 11. ./bin/utils/export_docs_generators.sh was not run because no generator options changed.
  • @mention the technical committee: @jgavris @ehyche @Edubits @jaz-ah @4brunu @dydus0x14

Summary by cubic

Fixes URLSession requests losing a cancellation when an interceptor completes asynchronously (e.g., token refresh), and when a cancel arrives during a retry decision. The request now reports URLError.cancelled and is never sent.

Bug Fixes

  • RequestTask records a cancel even before the data task exists, and clears it on each new execute() so Combine and RxSwift wrappers still work after disposal.
  • A cancelled request skips the retry interceptor; .retry is ignored if a cancel lands while the interceptor decides. Retries use the overridable execute(completion:) so subclasses keep working.
  • Adds RequestCancellationTests covering cancel during intercept, before/during retry, and re-execution after cancel.
  • The Petstore sample tests now only assert that an upload response has a message instead of matching the server's exact message text.