graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Client app crashing with "Cannot access a disposed object." exception

Open Liviu-Ionel-Anton opened this issue 4 years ago • 2 comments

Hi,

I am using the GraphQl.Client 3.2.1 on a project and I get an exception crashing the application after a while. I have a client that it connects to a graphQl server from multiple threads and it is creating a subscription stream to receive notifications. The client has to do a reconnection to the server periodically. By reconnection I mean:

  • disposing the subscription created by CreateSubscriptionStream()
  • disposing error subscription from GraphqlClient.WebSocketReceiveErrors.Subscribe
  • disposing the GraphQLHttpClient instance
  • recreate GraphQLHttpClient instance
  • get error subscription from GraphqlClient.WebSocketReceiveErrors.Subscribe
  • create the subscription by CreateSubscriptionStream() After several reconnections I get this:

System.ObjectDisposedException HResult=0x80131622 Message=Cannot access a disposed object. Object name: 'System.Net.WebSockets.InternalClientWebSocket'. Source=System StackTrace: at System.Net.WebSockets.WebSocketBase.ThrowIfDisposed() at System.Net.WebSockets.WebSocketBase.WebSocketOperation.<Process>d__19.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.WebSockets.WebSocketBase.<SendAsyncCore>d__47.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.<SendWebSocketMessageAsync>d__32.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.<SendWebSocketRequestAsync>d__31.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.<>c__DisplayClass28_1`1.<<CreateSubscriptionStream>b__7>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch()

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

Can anyone help me understand what is the cause of this?

Liviu-Ionel-Anton avatar Feb 05 '21 10:02 Liviu-Ionel-Anton

Hi,

can you elaborate why you're disposing of the entire GraphQLHttpClient instance? Is this really necessary?

rose-a avatar Mar 02 '21 20:03 rose-a

propably related to #329

rose-a avatar Apr 02 '21 07:04 rose-a

I am using the GraphQl.Client 6.0.3

the described flow is valid me as well, because it is the same application:

- disposing the subscription created by CreateSubscriptionStream()
- disposing error subscription from GraphqlClient.WebSocketReceiveErrors.Subscribe
- disposing the GraphQLHttpClient instance
- recreate GraphQLHttpClient instance
- get error subscription from GraphqlClient.WebSocketReceiveErrors.Subscribe
- create the subscription by CreateSubscriptionStream()
- After several reconnections I get this:

we get one of the following errors sporadically:

Unhandled exception was thrown in the application. The application will now exit.
Exception: Cannot access a disposed object.
Object name: 'System.Net.WebSockets.InternalClientWebSocket'.
StackTrace:    at System.Net.WebSockets.WebSocketBase.ThrowIfDisposed()
   at System.Net.WebSockets.WebSocketBase.WebSocketOperation.<Process>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.WebSocketBase.<SendAsyncCore>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.<SendWebSocketMessageAsync>d__39.MoveNext() in /_/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs:line 265
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.<SendWebSocketRequestAsync>d__38.MoveNext() in /_/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs:line 253
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at GraphQL.Client.Http.Websocket.GraphQLWSProtocolHandler.<>c__DisplayClass7_1`1.<<CreateSubscriptionObservable>b__2>d.MoveNext() in /_/src/GraphQL.Client/Websocket/GraphQLWSProtocolHandler.cs:line 114
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

Or sometimes as well:

>Unhandled exception was thrown in the application. The application will now exit.
Exception: This operation is only allowed using a successfully authenticated context.
StackTrace:    at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)
   at System.Net.Security.SslState.get_SecureStream()
   at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
   at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_1(Stream stream, IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncTrimPromise`1.Complete(TInstance thisRef, Func`3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.WebSocketConnectionStream.<WriteAsync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Net.WebSockets.WebSocketBase.<SendFrameAsync>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Net.WebSockets.WebSocketBase.WebSocketOperation.<Process>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.WebSocketBase.<SendAsyncCore>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.<SendWebSocketMessageAsync>d__39.MoveNext() in /_/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs:line 265
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.<SendWebSocketRequestAsync>d__38.MoveNext() in /_/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs:line 253
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at GraphQL.Client.Http.Websocket.GraphQLWSProtocolHandler.<>c__DisplayClass7_1`1.<<CreateSubscriptionObservable>b__2>d.MoveNext() in /_/src/GraphQL.Client/Websocket/GraphQLWSProtocolHandler.cs:line 114
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

kimlukas avatar Apr 19 '24 15:04 kimlukas