Repository navigation
Close the dispatcher when a KV or Object Store watch fails to start - #1643
Conversation
NatsWatchSubscription.finishInit creates a dispatcher and then subscribes. When the subscribe throws, the exception leaves the KV or Object Store watch subscription constructor, so the caller has nothing to unsubscribe and the dispatcher and its thread stayed on the connection until it closed. The subscribe and the consumer-info read now run in a try/catch that closes the dispatcher, adds a close failure as suppressed, and rethrows. Fixes #1642
|
Claude finished @scottf's task in 1m 0s —— View job Reviewing PR #1643
ReviewThe fix is correct and well-scoped. A few things worth noting (none blocking):
No correctness, security, or thread-safety issues found. |
Fixes #1642.
NatsWatchSubscription.finishInitcreates a dispatcher and then subscribes. When the subscribe throws, for example because the bucket's stream is gone, the exception leaves theNatsKeyValueWatchSubscriptionorNatsObjectStoreWatchSubscriptionconstructor, so the caller never gets an object to unsubscribe, and the dispatcher and its thread stay on the connection until the connection is closed. Every failedKeyValue.watchorObjectStore.watchleaked one dispatcher.The subscribe and the consumer-info read that follows it now run inside a try/catch. On any exception the dispatcher created for the watch is closed through
NatsConnection.closeDispatcher, which also unsubscribes anything that was subscribed on it, an exception from the close is added as suppressed, and the original exception is rethrown. The same approach as the V3JetStreamsubscribe cleanup.Tests:
KeyValueTests.testWatchThatFailsToStartReleasesItsDispatcherandObjectStoreTests.testWatchThatFailsToStartReleasesItsDispatchercreate a bucket, take the bucket context, delete the bucket, then call watch five times. Each call throwsJetStreamApiException10059 and the connection's dispatcher count stays where it was. Both fail on main (the count goes from 1 to 2 on the first call) and pass with the change.