Repository navigation
Description
Description
We're in the process of switching our net472 WPF application to netcoreapp3.1. So far, it's working great!
But we're receiving crash reports from our users which beta test the .NET Core version. These exceptions are not blocking and we get them from the TaskScheduler.UnobservedTaskException event.
The stack traces are not very good as there's no user code inside it. There are different flavors of it:
System.ObjectDisposedException:
at System.IO.FileStream.BeginRead (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream+<>c.<BeginEndReadAsync>b__48_0 (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream.BeginEndReadAsync (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.ReadAsync (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream.ReadAsync (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.ReadAsync (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Diagnostics.AsyncStreamReader+<ReadBufferAsync>d__16.MoveNext (System.Diagnostics.Process, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
System.ObjectDisposedException:
at System.IO.FileStream.ValidateReadWriteArgs (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.Read (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream+<>c.<BeginReadInternal>b__43_0 (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.Task`1.InnerInvoke (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.Task+<>c.<.cctor>b__274_0 (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream.EndRead (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.EndRead (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream+<>c.<BeginEndReadAsync>b__48_1 (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.TaskFactory`1+FromAsyncTrimPromise`1.Complete (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1+ConfiguredValueTaskAwaiter.GetResult (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Diagnostics.AsyncStreamReader+<ReadBufferAsync>d__16.MoveNext (System.Diagnostics.Process, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
System.ObjectDisposedException:
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.StubHelpers.StubHelpers.SafeHandleAddRef (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Interop+Kernel32.ReadFile (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.ReadFileNative (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.ReadNative (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.ReadSpan (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.Read (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream+<>c.<BeginReadInternal>b__43_0 (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.Task`1.InnerInvoke (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream.EndRead (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.EndRead (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream+<>c.<BeginEndReadAsync>b__48_1 (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.TaskFactory`1+FromAsyncTrimPromise`1.Complete (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1+ConfiguredValueTaskAwaiter.GetResult (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Diagnostics.AsyncStreamReader+<ReadBufferAsync>d__16.MoveNext (System.Diagnostics.Process, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
This one might be a different problem:
System.NotSupportedException:
at System.IO.FileStream.ReadSpan (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.Read (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream+<>c.<BeginReadInternal>b__43_0 (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.Task`1.InnerInvoke (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream.EndRead (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.FileStream.EndRead (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Stream+<>c.<BeginEndReadAsync>b__48_1 (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.TaskFactory`1+FromAsyncTrimPromise`1.Complete (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Diagnostics.AsyncStreamReader+<ReadBufferAsync>d__16.MoveNext (System.Diagnostics.Process, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
In any case they all point out to System.Diagnostics.AsyncStreamReader which seems to be only used by System.Diagnostics.Process.
We don't have a clear repro yet but it seems it happens when starting external processes (like explorer.exe).
DisposedException does not seem to be an expected exception as seen here:
runtime/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/AsyncStreamReader.cs
Lines 89 to 117 in 5f96636
| private async Task ReadBufferAsync() | |
| { | |
| while (true) | |
| { | |
| try | |
| { | |
| int bytesRead = await _stream.ReadAsync(new Memory<byte>(_byteBuffer), _cts.Token).ConfigureAwait(false); | |
| if (bytesRead == 0) | |
| break; | |
| int charLen = _decoder.GetChars(_byteBuffer, 0, bytesRead, _charBuffer, 0); | |
| _sb!.Append(_charBuffer, 0, charLen); | |
| MoveLinesFromStringBuilderToMessageQueue(); | |
| } | |
| catch (IOException) | |
| { | |
| // We should ideally consume errors from operations getting cancelled | |
| // so that we don't crash the unsuspecting parent with an unhandled exc. | |
| // This seems to come in 2 forms of exceptions (depending on platform and scenario), | |
| // namely OperationCanceledException and IOException (for errorcode that we don't | |
| // map explicitly). | |
| break; // Treat this as EOF | |
| } | |
| catch (OperationCanceledException) | |
| { | |
| // We should consume any OperationCanceledException from child read here | |
| // so that we don't crash the parent with an unhandled exc | |
| break; // Treat this as EOF | |
| } |
As for the fact the exception is not observed, I guess we never wait for _readToBufferTask which seems to be only here:
runtime/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/AsyncStreamReader.cs
Lines 256 to 262 in 5f96636
| internal void WaitUntilEOF() | |
| { | |
| if (_readToBufferTask is Task task) | |
| { | |
| task.GetAwaiter().GetResult(); | |
| } | |
| } |
Configuration
- x64
- .NET Core 3.1.9
- Windows 10.0.18362
Regression?
- We don't have those crash reports with the .NET Framework 4.7.2 version of the same application
Activity
jairbubbles commentedon Nov 10, 2020
Important information, it seems the user code provoking htis is not that pretty:
We're disposing the process while we're still reading the error stream and we don't wait for the process to end.
stephentoub commentedon Nov 13, 2020
Great to hear.
Thanks, that's helpful. Here:
runtime/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/AsyncStreamReader.cs
Line 89 in 72eefe3
we currently catch and eat IOException and OperationCanceledException. In theory we could add more to the list. That said, disposing of objects while they're in use is highly discouraged ;-) and I wouldn't be surprised if you could cause other kinds of exceptions by doing that.
stephentoub commentedon Nov 13, 2020
That's probably the same thing, and an example of "and I wouldn't be surprised if you could cause other kinds of exceptions by doing that"... when the Process gets disposed of and its streams get disposed of, the stream's CanRead property will then return false, causing a NotSupportedException when trying to read.
stephentoub commentedon Nov 13, 2020
Can you elaborate on this? Why are unobserved exceptions causing crashes? Have you changed the default behavior for TaskScheduler.UnobservedTaskExceptions such that it will crash if one goes unobserved?
jairbubbles commentedon Nov 13, 2020
I didn't chose the good words, it's not killing the application.
Indeed, we fixed the code and we no longer receive any error reports on the subject.
After we pinpointed the code that was faulty, it was pretty obvious that something wrong was going on with the user code. So I'm wondering what could have been made differently so that we get more info in the exception?
I'm not sure catching the
ObjectDisposedExceptionwould be a solution here as it would completely swallow the fact that something is not correct in the user code and as you said "disposing of objects while they're in use is highly discouraged ;-) ".That being said, disposing objects is sometimes added later on a code base because for instance a static analyzer told you to do so and one can do the change too quickly without the full picture.
Should the _
readToBufferTaskawaited in the dispose (after its being canceled)? I guess we would a better stacktrace in that case?