Skip to content

[Foundation] Improve potential reentrancy problems in NSUrlSessionHandler.#25183

Merged
rolfbjarne merged 4 commits intomainfrom
dev/rolf/nsurlsession-safer-dispose-20345
Apr 23, 2026
Merged

[Foundation] Improve potential reentrancy problems in NSUrlSessionHandler.#25183
rolfbjarne merged 4 commits intomainfrom
dev/rolf/nsurlsession-safer-dispose-20345

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

C# locks are re-entrant, so try to avoid re-entrancy issues by:

  • In RemoveInflightData: fetch and remove the inflight data in a single
    Dictionary.Remove call. This is also faster than doing two dictionary
    lookups.
  • In Dispose: collect all tasks to cancel and dispose first (inside the
    lock), and then we cancel and dispose after the lock. This hopefully fixes
    Crash in networking stack #20345 (this solution was tested by a
    customer and apparently worked).

Might fix #20345.

NET10_0_OR_GREATER is always set now, so remove other code paths.
…dler.

C# locks are re-entrant, so try to avoid re-entrancy issues by:

* In `RemoveInflightData`: fetch and remove the inflight data in a single
  `Dictionary.Remove` call. This is also faster than doing two dictionary
  lookups.
* In `Dispose`: collect all tasks to cancel and dispose first (inside the
  lock), and then we cancel and dispose after the lock. This hopefully fixes
  #20345 (this solution was tested by a
  customer and apparently worked).

Might fix #20345.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates NSUrlSessionHandler to reduce re-entrancy hazards during inflight request cleanup and handler disposal, aiming to improve stability in long-running/high-volume networking scenarios (potentially addressing #20345).

Changes:

  • Simplifies inflight request removal by using Dictionary.Remove(key, out value) to fetch+remove in one operation.
  • Refactors Dispose(bool) to snapshot inflight tasks under lock, clear the dictionary, then cancel/dispose tasks outside the lock to avoid re-entrant lock usage.

Comment on lines +196 to +204
var tasks = new List<NSUrlSessionTask> ();
lock (inflightRequestsLock) {
foreach (var pair in inflightRequests) {
pair.Key?.Cancel ();
pair.Key?.Dispose ();
}

tasks.AddRange (inflightRequests.Keys);
inflightRequests.Clear ();
}
foreach (var task in tasks) {
task.Cancel ();
task.Dispose ();
}
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Dispose, inflightRequests.Clear () happens before canceling tasks, so any subsequent DidCompleteWithError / DidReceiveData delegate callbacks won’t be able to retrieve the corresponding InflightData from inflightRequests. Since InflightData.CompletionSource is only completed when GetInflightData finds an entry, this can leave callers awaiting SendAsync permanently blocked if the handler is disposed while requests are in-flight. Consider capturing the InflightData instances (not just the tasks) while holding the lock and explicitly completing/canceling their CompletionSource (and canceling InflightData.CancellationTokenSource / faulting the stream) as part of Dispose, instead of relying on delegate callbacks after the dictionary is cleared.

Copilot uses AI. Check for mistakes.
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne changed the base branch from dev/rolf/nsurlsession-cleanup to main April 20, 2026 16:03
@rolfbjarne rolfbjarne marked this pull request as ready for review April 20, 2026 16:06
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #9764620] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 9764620da39250b2d45717679a6259a2ba2079d8 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #9764620] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 9764620da39250b2d45717679a6259a2ba2079d8 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 9764620da39250b2d45717679a6259a2ba2079d8 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #9764620] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 9764620da39250b2d45717679a6259a2ba2079d8 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🔥 [CI Build #9764620] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 1 tests failed, 155 tests passed.

Failures

❌ Tests on macOS Monterey (12) tests

1 tests failed, 4 tests passed.

Failed tests

  • monotouch-test: Failed (exit code 2)
    • TlsDefaults : Connection is ready

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 11 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Ventura (13): All 5 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: 9764620da39250b2d45717679a6259a2ba2079d8 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash in networking stack

4 participants