justcoding121 / titanium-web-proxy

A cross-platform asynchronous HTTP(S) proxy server in C#.
MIT License
1.93k stars 618 forks source link

ObjectDisposedException on System.Net.Sockets.Socket #840

Closed liviu-anca closed 3 years ago

liviu-anca commented 3 years ago

We are trying to use Titanium Web Proxy in unit tests, as a mock Proxy Server. Sometimes, it crashes the testing environment, with this exception:

System.ObjectDisposedException
  HResult=0x80131622
  Message=Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
  Source=System.Net.Sockets
  StackTrace:
   at System.Net.Sockets.Socket.ThrowObjectDisposedException()
   at System.Net.Sockets.Socket.AcceptAsync(SocketAsyncEventArgs e)
   at System.Net.Sockets.Socket.AcceptAsync(Socket acceptSocket)
   at System.Net.Sockets.TcpListener.AcceptSocketAsync()
   at System.Net.Sockets.TcpListener.BeginAcceptSocket(AsyncCallback callback, Object state)
   at Titanium.Web.Proxy.ProxyServer.onAcceptConnection(IAsyncResult asyn)
   at System.Threading.Tasks.TaskToApm.TaskAsyncResult.InvokeCallback()
   at System.Runtime.CompilerServices.TaskAwaiter.<>c.<OutputWaitEtwEvents>b__12_0(Action innerContinuation, Task innerTask)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
   at System.Threading.Tasks.AwaitTaskContinuation.<>c.<.cctor>b__17_0(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__140_1(Object state)
   at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

  This exception was originally thrown at this call stack:
    [External Code]

We managed to reproduce this issue by starting the Proxy in a loop. It crashes after a few runs. Please see the attached project and source code. TitaniumSimpleTest.zip

justcoding121 commented 3 years ago

There was a fix for this in #862