stan.net icon indicating copy to clipboard operation
stan.net copied to clipboard

Unhandled exception in unsubscribe from closeDueToPing

Open MattiasCL opened this issue 3 years ago • 0 comments

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.

MattiasCL avatar Feb 15 '21 09:02 MattiasCL