Skip to content

[BUG][swift6] urlsession: cancelling while an interceptor completes asynchronously still sends the request #25848

Backlog

Description

@Robuske

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue? (not spec-specific; any urlsession config reproduces it)
  • Have you validated the input using an OpenAPI validator? (not spec-specific)
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

#25188 (fixing #25182) re-checks Task.isCancelled after execute(completion:) returns. That only covers interceptors whose intercept completes synchronously. If OpenAPIInterceptor.intercept completes asynchronously (e.g. after a token refresh), RequestTask holds no data task yet, so neither onCancel nor the re-check has anything to cancel. The data task is created and resumed afterwards and the request is sent. The same happens when a cancel arrives while the retry interceptor is working (e.g. refreshing a token after a 401) before .retry re-executes the request.

The gap was acknowledged in #25188 (comment) and left for a follow-up.

Actual: the request is sent although the Task was cancelled. Expected: the request is not sent and no retry is attempted.

openapi-generator version

master (da82b98, 7.29.0-SNAPSHOT).

Generation Details

Any swift6 config with the urlsession library, e.g. bin/configs/swift6-asyncAwaitLibrary.yaml. The alamofire library is not affected: its request exists as soon as execute(completion:) returns.

Steps to reproduce
  1. Set an OpenAPIInterceptor whose intercept calls completion from a Task after a delay.
  2. Call try await builder.execute() from a Task.
  3. Cancel the Task while the interceptor is pending.
Suggest a fix

RequestTask should remember a cancel() for the current execution and cancel a data task that is set afterwards, while a fresh public execute(completion:) clears it, because RxSwift/Combine wrappers call cancel() on disposal and re-execute the same builder. PR follows.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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