nats-io / stan.net

The official NATS .NET C# Streaming Client
Apache License 2.0
138 stars 41 forks source link

Unhandled exception in unsubscribe from closeDueToPing #198

Open MattiasCL opened 3 years ago

MattiasCL commented 3 years ago

Had a process crash following a disconnect from the NATS server with this stack trace:

NATS.Client.NATSConnectionClosedException: Connection is closed.
   at NATS.Client.Subscription.unsubscribe(Boolean throwEx)
   at NATS.Client.AsyncSubscription.Unsubscribe()
   at STAN.Client.Connection.cleanupOnClose(Exception ex)
   at STAN.Client.Connection.closeDueToPing(Exception ex)
   at STAN.Client.Connection.pingServer(Object state)
   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.TimerQueueTimer.CallCallback()
   at System.Threading.TimerQueueTimer.Fire()
   at System.Threading.TimerQueue.FireNextTimers()

Had a brief look in the source code: cleanupOnClose only unsubscribes the ackSubscription and pingSubscription if the NATS connection isn't already closed, and unsubscribe throws if called on a closed subscription. The connection's internal mutex isn't held in between those checks, so my guess is that there's a chance for a data race with the connection closing between cleanupOnClose and unsubscribe.