fix(URLSession): drop request state once a completion handler reports the task #1295
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
nachoBonafonte
Summary
urlSessionTaskWillResumestores aNetworkRequestStateinrequestMapfor every task. For tasks created with a completion handler, the completion wrapper reports the task itself and thesetState:fallback skips it. On a session with no delegate (for exampleURLSession.shared), nothing else runsdidCompleteWithErrorordidFinishCollecting, so nothing ever removes that entry. Each completion-handler request on such a session leaves oneNetworkRequestState, including itsURLRequest, in memory for the life of the instrumentation.This PR removes the entry in a
deferat the top of both completion wrappers (dataTask/downloadTaskanduploadTask), after the response or error has been logged. It follows the pattern #1168 introduced inreportCompletionIfUnreported: clear after logging, so the logger can still read the request.Testing
New tests in
URLSessionInstrumentationTests:testDataTaskCompletionHandlerDoesNotLeakRequestStatetestUploadTaskCompletionHandlerDoesNotLeakRequestStateEach runs a completion-handler task on a delegate-less session against a stub
URLProtocol, then readsrequestMap(throughMirror, on the instrumentation's queue) and checks that no entry for the request's URL remains.Without the fix (on main):
With the fix:
swift build --build-tests: OK.swift test --filter 'URLSessionInstrumentationTests\.':URLSessionInstrumentationTests76 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 --stricton the changed files: no violations.Limitations
requestMapthroughMirror, because there is no public way to observe it. They break if the property is renamed.didCompleteWithError/didFinishCollectingalready clear the entry.Checklist
swift testpasses locally🤖 Generated with Claude Code