Skip to content

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

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

Closed
Liviu-Ionel-Anton opened this issue Feb 5, 2021 · 3 comments · Fixed by #635
Closed

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

Liviu-Ionel-Anton opened this issue Feb 5, 2021 · 3 comments · Fixed by #635

Comments

@Liviu-Ionel-Anton
Copy link

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.d__19.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketBase.d__47.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.d__32.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.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.<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?

@rose-a
Copy link
Collaborator

rose-a commented Mar 2, 2021

Hi,

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

@rose-a
Copy link
Collaborator

rose-a commented Apr 2, 2021

propably related to #329

@kimlukas
Copy link
Contributor

kimlukas commented Apr 19, 2024

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants