Bug 42871 - Asynchronous Stream Read after Process Dispose Throws System.ObjectDisposedException
Summary: Asynchronous Stream Read after Process Dispose Throws System.ObjectDisposedEx...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 4.4.0 (C7)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-07-28 10:34 UTC by Kratzer Kevin
Modified: 2018-03-13 11:07 UTC (History)
2 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:


Description Kratzer Kevin 2016-07-28 10:34:22 UTC
We are using mono on ubuntu.
After the dispose of a System.Diagnostics.Process the VM often crashes with the following stack trace:

Unhandled Exception:
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Stream has been closed'.
  at System.IO.FileStream.BeginRead (System.Byte[] array, Int32 offset, Int32 numBytes, System.AsyncCallback userCallback, System.Object stateObject) <0x7f5f73f0c1a0 + 0x00235> in <filename unknown>:0 
  at System.Diagnostics.AsyncStreamReader.ReadBuffer (IAsyncResult ar) <0x4135bee0 + 0x00425> in <filename unknown>:0 
  at System.IO.Stream+ReadWriteTask.InvokeAsyncCallback (System.Object completedTask) <0x7f5f73d6b920 + 0x0004f> in <filename unknown>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, Boolean preserveSyncCtx) <0x7f5f73e5a990 + 0x00178> in <filename unknown>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, Boolean preserveSyncCtx) <0x7f5f73e5a960 + 0x00020> in <filename unknown>:0 
  at System.IO.Stream+ReadWriteTask.System.Threading.Tasks.ITaskCompletionAction.Invoke (System.Threading.Tasks.Task completingTask) <0x7f5f73d6b990 + 0x00103> in <filename unknown>:0 
  at System.Threading.Tasks.Task.FinishContinuations () <0x7f5f73e497e0 + 0x00214> in <filename unknown>:0 
  at System.Threading.Tasks.Task.FinishStageThree () <0x7f5f73e47bf0 + 0x00068> in <filename unknown>:0 
  at System.Threading.Tasks.Task.FinishStageTwo () <0x7f5f73e47a90 + 0x00133> in <filename unknown>:0 
  at System.Threading.Tasks.Task.Finish (Boolean bUserDelegateExecuted) <0x7f5f73e478f0 + 0x0008d> in <filename unknown>:0 
  at System.Threading.Tasks.Task.ExecuteWithThreadLocal (System.Threading.Tasks.Task& currentTaskSlot) <0x7f5f73e48620 + 0x00131> in <filename unknown>:0 
  at System.Threading.Tasks.Task.ExecuteEntry (Boolean bPreventDoubleExecution) <0x7f5f73e484c0 + 0x000ee> in <filename unknown>:0 
  at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () <0x7f5f73e48440 + 0x0000e> in <filename unknown>:0 
  at System.Threading.ThreadPoolWorkQueue.Dispatch () <0x7f5f73e5f2e0 + 0x001d6> in <filename unknown>:0 
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () <0x7f5f73e60bf0 + 0x00008> in <filename unknown>:0 

This behaviour could not be confirmed for Mono  "Stable".

Maybe the process dispose, which only sets the streams to null, leaves the responsibility for the dispose of the streams to the GC which leads to a wrong dispose sequence of the streams. This might allow the AsyncStreamReader to read more data after the underlying stream has already been closed by the GC.

standardOutput = null;
standardInput = null;
standardError = null;

output = null; // AsyncStreamReader
error = null; // AsyncStreamReader

Maybe this PR which was merged into the mono-4.3.2-branch is related to this behaviour:
Comment 1 Marek Safar 2016-08-09 14:36:55 UTC
Could you provide some steps how to reproduce the issue?
Comment 2 Marek Safar 2018-03-13 11:07:10 UTC
We have not received the requested information. If you are still experiencing this issue please provide all the requested information and reopen the bug report.

Thank you!

Note You need to log in before you can comment on or make changes to this bug.