Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/monotouch-test/System.Net.Http/NSUrlSessionHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ public void DisposeAndRecreateBackgroundSessionHandler ()

IgnoreIfExceptionDueToBackgroundServiceInUseByAnotherProcess (ex);
TestRuntime.IgnoreInCIIfBadNetwork (ex);
if (ex is ObjCException && ex.ToString ().Contains ("Task created in a session that has been invalidated")) {
Comment thread
rolfbjarne marked this conversation as resolved.
// When disposing an NSUrlSessionHandler backed by a background NSUrlSession
// and immediately creating a new handler with the same background session
// identifier, the new session can fail with 'Task created in a session
// that has been invalidated'.
//
// This happens 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.
//
// There are a couple of fixes:
// * Add a Thread.Sleep before creating the second NSUrlSessionHandler - but this will slow down every test run,
// * 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.
// * 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.
Assert.Inconclusive ("The previous background session wasn't fully invalidated before we tried to create a new background session (with the same identifier)");
}
Assert.IsNull (ex, "Second request exception");
}

Expand Down
Loading