Skip to content

Commit 6ed4eb3

Browse files
rolfbjarneCopilotCopilot
authored
[tests] Detect when we couldn't create a second background session. (#24878)
When disposing an NSUrlSessionHandler backed by a background NSUrlSession and immediately creating a new handler with the same background session identifier, the new session could fail with 'Task created in a session that has been invalidated'. This happened because InvalidateAndCancel() is asynchronous - it marks the session for invalidation but doesn't wait for it to complete. Apple reuses the same native session object for background sessions with the same identifier, so creating a new session before invalidation completes returns the already-invalidated session. Fix by detecting this scenario in the test, and marking the test as inconclusive. Fixes #24376 --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 76cf49e commit 6ed4eb3

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

tests/monotouch-test/System.Net.Http/NSUrlSessionHandlerTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ public void DisposeAndRecreateBackgroundSessionHandler ()
5959

6060
IgnoreIfExceptionDueToBackgroundServiceInUseByAnotherProcess (ex);
6161
TestRuntime.IgnoreInCIIfBadNetwork (ex);
62+
if (ex is ObjCException && ex.ToString ().Contains ("Task created in a session that has been invalidated")) {
63+
// When disposing an NSUrlSessionHandler backed by a background NSUrlSession
64+
// and immediately creating a new handler with the same background session
65+
// identifier, the new session can fail with 'Task created in a session
66+
// that has been invalidated'.
67+
//
68+
// This happens because InvalidateAndCancel() is asynchronous - it marks
69+
// the session for invalidation but doesn't wait for it to complete. Apple
70+
// reuses the same native session object for background sessions with the
71+
// same identifier, so creating a new session before invalidation completes
72+
// returns the already-invalidated session.
73+
//
74+
// There are a couple of fixes:
75+
// * Add a Thread.Sleep before creating the second NSUrlSessionHandler - but this will slow down every test run,
76+
// * Wait for the session to become invalid in NSUrlSessionHandler (add a 'DidBecomeInvalid' implementation, and wait for that in Dispose) - which may unnecessarily slow down working code.
77+
// * Detect this scenario here, and just mark the test as inconclusive. The test does something somewhat unusual (create two background sessions with the same identifier in quick succession), so this seems like the best approach for now.
78+
Assert.Inconclusive ("The previous background session wasn't fully invalidated before we tried to create a new background session (with the same identifier)");
79+
}
6280
Assert.IsNull (ex, "Second request exception");
6381
}
6482

0 commit comments

Comments
 (0)