-
Notifications
You must be signed in to change notification settings - Fork 555
Closed
Labels
bugIf an issue is a bug or a pull request a bug fixIf an issue is a bug or a pull request a bug fix
Milestone
Description
Apple platform
iOS
Framework version
net10.0-*
Affected platform version
.NET 8, 9 and 10
Description
When I try and create a background configuration for my NSUrlSessionHandler, I can't dispose that handler and create a new one later, since all requests will start timing out.
Steps to Reproduce
- Run the following code more than once. After the first time, all requests will time out:
NSUrlSessionHandler handler = new NSUrlSessionHandler(NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration("id"));
var client = new HttpClient(handler);
await client.GetByteArrayAsync("https://microsoft.com"); // Times out after the first run
client.Dispose();
handler.Dispose();Did you find any workaround?
I can workaround it with reflection, which is too risky for deployment, but it does clearly indicate what the fix will be:
var field = typeof(NSUrlSessionHandler).GetField("session", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
NSUrlSession session = (NSUrlSession)field.GetValue(handler)!;
session.InvalidateAndCancel();
Judging from this, we just need to add that InvalidateAndCancel call to the Dispose method. There's also FinishTasksAndInvalidate which would fix the timeout, but when I call Dispose on SocketsHttpHandler, and pending requests are cancelled, so the InvalidateAndCancel seems most appropriate.
Relevant logs
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugIf an issue is a bug or a pull request a bug fixIf an issue is a bug or a pull request a bug fix