← Back to Pull requests

fix(URLSession): drop request state once a completion handler reports the task #1295

Closed
unknown closed this pull request without merging on Nov 19, 2026 17:28 UTC
View on GitHub
unknown unknown commented on Nov 17, 2026 08:48 UTC
Author

Summary

urlSessionTaskWillResume stores a NetworkRequestState in requestMap for every task. For tasks created with a completion handler, the completion wrapper reports the task itself and the setState: fallback skips it. On a session with no delegate (for example URLSession.shared), nothing else runs didCompleteWithError or didFinishCollecting, so nothing ever removes that entry. Each completion-handler request on such a session leaves one NetworkRequestState, including its URLRequest, in memory for the life of the instrumentation.

This PR removes the entry in a defer at the top of both completion wrappers (dataTask/downloadTask and uploadTask), after the response or error has been logged. It follows the pattern #1168 introduced in reportCompletionIfUnreported: clear after logging, so the logger can still read the request.

Testing

New tests in URLSessionInstrumentationTests:

  • testDataTaskCompletionHandlerDoesNotLeakRequestState

  • testUploadTaskCompletionHandlerDoesNotLeakRequestState
  • Each runs a completion-handler task on a delegate-less session against a stub URLProtocol, then reads requestMap (through Mirror, on the instrumentation's queue) and checks that no entry for the request's URL remains.

    Without the fix (on main):

    URLSessionInstrumentationTests.swift:1608: error: ... testDataTaskCompletionHandlerDoesNotLeakRequestState : XCTAssertTrue failed - request state was never removed: 1 entry still held
    URLSessionInstrumentationTests.swift:1614: error: ... testUploadTaskCompletionHandlerDoesNotLeakRequestState : XCTAssertTrue failed - request state was never removed: 1 entry still held
    

    With the fix:

  • swift build --build-tests: OK.

  • swift test --filter 'URLSessionInstrumentationTests\.': URLSessionInstrumentationTests 76 tests (74 before + 2 new), 2 skipped, 0 failures.

  • swift test --sanitize=thread --filter 'URLSessionInstrumentationTests\.URLSessionInstrumentationTests': 76 tests, 0 failures, no ThreadSanitizer reports.

  • swiftlint lint --strict on the changed files: no violations.
  • Limitations

  • The tests read the private requestMap through Mirror, because there is no public way to observe it. They break if the property is renamed.

  • Sessions with a delegate were not affected: their didCompleteWithError/didFinishCollecting already clear the entry.
  • Checklist

  • swift test passes locally

  • Unit tests added or updated for the change

  • Instrumentation and event changes only: N/A, no span, event or attribute changes

  • Documentation updated: N/A
  • 🤖 Generated with Claude Code

    Diff: 333 lines (+249 / -84) across 4 files Reviewer: nachoBonafonte Review duration: 3400m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 333 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.348
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1295"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.1% (MEDIUM) · Higher = larger blast radius
    42.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.7% · Safe to merge
    16.7%