fix: harden ClientGrain.OnDisconnect against stream errors and reentrancy#170
Open
d-barker wants to merge 1 commit into
Open
fix: harden ClientGrain.OnDisconnect against stream errors and reentrancy#170d-barker wants to merge 1 commit into
d-barker wants to merge 1 commit into
Conversation
…ancy OnDisconnect could throw and abort disconnect cleanup in two ways: - UnsubscribeAsync() on the stored server-disconnected handle could throw "Handle is no longer valid. It has been used to unsubscribe or resume." when the subscription had already been torn down. The exception is now caught and logged so the rest of cleanup (publishing the client-disconnect event, clearing state, deactivating) still runs. - The client-disconnection stream publish (OnNextAsync) was unguarded; a transient stream/provider error would likewise abort cleanup. It is now wrapped in try/catch and logged. Additionally, the subscription handle is captured to a local and the field cleared *before* awaiting UnsubscribeAsync(). Send is marked [ReadOnly] (interleaved), so nulling the field first prevents an interleaved call from observing the same handle and double-unsubscribing it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This was referenced Jun 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Makes
ClientGrain.OnDisconnectresilient so a single stream error can no longer abort the whole disconnect-cleanup sequence.Two failure modes are addressed:
UnsubscribeAsync()on the stored server-disconnected handle could throwHandle is no longer valid. It has been used to unsubscribe or resume.when the subscription had already been torn down on the producer side. This previously aborted cleanup. It is now caught and logged, so the rest ofOnDisconnect(publishing the client-disconnect event, clearing state, deactivating) still runs.OnNextAsyncpublish had no error handling; a transient stream/provider error would likewise abort cleanup. It is now wrapped in try/catch and logged.Additionally, the handle is captured to a local and the field cleared before awaiting
UnsubscribeAsync().Sendis marked[ReadOnly](interleaved), so nulling the field first prevents an interleaved call from observing the same handle and double-unsubscribing it.Notes
ClientGrain.OnDisconnectonly — independent of fix: tolerate missing/invalidated stream subscription handles in grain activation #169 (which fixes theOnActivatehandle resume/[0]indexing). The two PRs are intentionally kept separate and do not overlap textually.🤖 Generated with Claude Code