diff --git a/src/Foundation/NSUrlSessionHandler.cs b/src/Foundation/NSUrlSessionHandler.cs index 589c52efd8cd..c12d535b6ce5 100644 --- a/src/Foundation/NSUrlSessionHandler.cs +++ b/src/Foundation/NSUrlSessionHandler.cs @@ -293,6 +293,7 @@ protected override void Dispose (bool disposing) inflightRequests.Clear (); } + session.InvalidateAndCancel (); base.Dispose (disposing); } diff --git a/tests/monotouch-test/System.Net.Http/NSUrlSessionHandlerTest.cs b/tests/monotouch-test/System.Net.Http/NSUrlSessionHandlerTest.cs new file mode 100644 index 000000000000..1733f73a7869 --- /dev/null +++ b/tests/monotouch-test/System.Net.Http/NSUrlSessionHandlerTest.cs @@ -0,0 +1,63 @@ +// +// NSUrlSessionHandlerTest.cs +// + +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Foundation; +using NUnit.Framework; +using Xamarin.Utils; + +namespace MonoTests.System.Net.Http { + [TestFixture] + [Preserve (AllMembers = true)] + public class NSUrlSessionHandlerTest { + + // https://github.com/dotnet/macios/issues/24376 + [Test] + public void DisposeAndRecreateBackgroundSessionHandler () + { + bool firstRequestSucceeded = false; + + // First request - should succeed + var done = TestRuntime.TryRunAsync (TimeSpan.FromSeconds (30), async () => { + using (var handler = new NSUrlSessionHandler (NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration ("test-id"))) { + using (var client = new HttpClient (handler)) { + var response = await client.GetByteArrayAsync (NetworkResources.MicrosoftUrl); + Assert.IsNotNull (response, "First request response"); + Assert.IsTrue (response.Length > 0, "First request response length"); + firstRequestSucceeded = true; + } + } + }, out var ex); + + if (!done || !firstRequestSucceeded) { + TestRuntime.IgnoreInCI ("Transient network failure - ignore in CI"); + Assert.Inconclusive ("First request failed or timed out - cannot verify the bug."); + } + + TestRuntime.IgnoreInCIIfBadNetwork (ex); + Assert.IsNull (ex, "First request exception"); + + // Second request with new handler using same background session ID - should not timeout + done = TestRuntime.TryRunAsync (TimeSpan.FromSeconds (30), async () => { + using (var handler = new NSUrlSessionHandler (NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration ("test-id"))) { + using (var client = new HttpClient (handler)) { + var response = await client.GetByteArrayAsync (NetworkResources.MicrosoftUrl); + Assert.IsNotNull (response, "Second request response"); + Assert.IsTrue (response.Length > 0, "Second request response length"); + } + } + }, out ex); + + if (!done) { + TestRuntime.IgnoreInCI ("Transient network failure - ignore in CI"); + Assert.Fail ("Second request timedout - this indicates the bug is present."); + } + + TestRuntime.IgnoreInCIIfBadNetwork (ex); + Assert.IsNull (ex, "Second request exception"); + } + } +}