-
Notifications
You must be signed in to change notification settings - Fork 515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-catchable ObjectDisposedException when using NSUrlSessionHandler #20123
Comments
So there are two problems here:
I'll look into this to see what I can do to at the very least not make the process crash. |
We don't want to leak exceptions back to the calling native code in WrappedNSInputStream.Read, because that will likely crash the process. Example stack trace: ObjectDisposed_StreamClosed (System.ObjectDisposedException) at System.ThrowHelper.ThrowObjectDisposedException_StreamClosed(String) + 0x3c at System.IO.MemoryStream.Read(Byte[], Int32, Int32) + 0x124 at System.Net.Http.MultipartContent.ContentReadStream.Read(Byte[], Int32, Int32) + 0x78 at System.Net.Http.NSUrlSessionHandler.WrappedNSInputStream.Read(IntPtr buffer, UIntPtr len) + 0x58 at MyApp!<BaseAddress>+0x7082f8 Instead return -1 from the Read method, which is documented as an error condition, and then also return a custom NSError from the Error property - which is also documented to be where the error is supposed to be surfaced. Ref: https://developer.apple.com/documentation/foundation/nsinputstream/1411544-read Ref: xamarin#20123.
) We don't want to leak exceptions back to the calling native code in WrappedNSInputStream.Read, because that will likely crash the process. Example stack trace: ObjectDisposed_StreamClosed (System.ObjectDisposedException) at System.ThrowHelper.ThrowObjectDisposedException_StreamClosed(String) + 0x3c at System.IO.MemoryStream.Read(Byte[], Int32, Int32) + 0x124 at System.Net.Http.MultipartContent.ContentReadStream.Read(Byte[], Int32, Int32) + 0x78 at System.Net.Http.NSUrlSessionHandler.WrappedNSInputStream.Read(IntPtr buffer, UIntPtr len) + 0x58 at MyApp!<BaseAddress>+0x7082f8 Instead return -1 from the Read method, which is documented as an error condition, and then also return a custom NSError from the Error property - which is also documented to be where the error is supposed to be surfaced. Ref: https://developer.apple.com/documentation/foundation/nsinputstream/1411544-read Ref: #20123.
I've merged an attempt at fixing the crash itself (we'll now return -1 from the Read method and set the Error property on the NSInputStream when Read throws an exception). I can't reproduce the problem, so I can't say what will happen if this scenario occurs, but it will probably not be worse than crashing the process. Hopefully the error will bubble up and show up in a place where it can be handled/reported properly. The fix will be released with our release matching Xcode 15.3. |
Thank you! However, I just tried to reproduced that crash in a Debug build and it produces a catchable exception (without any mention of
Maybe the http requests & threading behave differently in Debug and Release (with NativeAot)? |
Looks like that changed in HttpClient: So I'm not sure what's exactly disposing the MultipartContent. It's still good that you were able to reproduce something similar: would you be able to provide a test project I can use to replicate this?
I think it's more likely just a timing issue, depending on exactly when the network goes down, and potentially also when the GC runs. |
In this project you see how I retry my http requests and how to reproduce the exception: http_test.zip The crash happens in this case because I manually dispose the HttpRequestMessage using the Edit: I tried refactoring the logic that it uses a while-loop to retry requests using the same
|
I tried to stress test this by:
Source code: https://gist.github.com/rolfbjarne/e415f1bc4ca9b98964041c28a37ad311 And there were no exceptions on a background thread, only the expected TaskCanceledException. So I'm not sure what to do here without a test project I can use to reproduce the ObjectDisposedException. |
Since there have been two fixes to the |
@rolfbjarne I was able to create a test project with which I was able to reproduce this possibly related crash 4 times within just a couple of minutes: #11799 (comment) |
I can reproduce a crash with your sample; I'll have a look. |
I am seeing a crash in one of my app when doing web requests and using an
NSUrlSessionHandler
.var httpClient = new HttpClient(new NSUrlSessionHandler())
I have noticed this crash being reported for months already, but with the last update I have added a button to the apps UI that allows users to cancel web requests (using
CancellationToken
) and it appears like as if this change has increased the occurence of the exception even more. I have all my web requests in atry-catch
clause, however this exception crashes my app nevertheless.According to Sentry, the crash happens in the
com.apple.NSURLConnectionLoader
thread (and that's also likely the reason I am unable to catch the exception)Environment
.NET 8, NativeAot-compiled
iOS 17.3.1
The text was updated successfully, but these errors were encountered: