Avoid racing between session close + adding new streams#6
Merged
Conversation
Until now, there was a subtle race between session.die and a new stream being created. In session.die, we used `streamMap.Each` to close all streams. We also set `local.goneAway` in die, which in theory should mean new streams aren't added. However, setting `goneAway`, iterating with `Each`, and adding a stream aren't adequately synchronized, so it's possible to end up in a state where we've got a goroutine already in `handleSyn`, about to add a new stream, and then we context switch to 'die' and cleanup existing streams, and then switch back and add the stream. This puts the stream in a state where it will never get closed since `die` won't run again (due to `dieOnce`), however we won't process the stream properly since `die` closed the `s.dead` channel and cleaned up stuff related to that. So, what's the fix? Instead of using `streamMap.Each` during `die`, we use `streamMap.Drain()` to atomically clear the map and mark it drained, and then in Set we return whether we just added to a drained map (the race condition), and if we did we knock over the stream we just created. The included test failed before this change. The test was written by claude, the fix was written by me.
bmpngrok
approved these changes
Mar 2, 2026
bmpngrok
left a comment
There was a problem hiding this comment.
Thanks for the fix! Called out one nit and one question, but I genuinely don't have a better suggestion after looking at the code that isn't a lot heavier than what you've got here
| m.Lock() | ||
| defer m.Unlock() | ||
| streams := m.table | ||
| m.table = nil |
There was a problem hiding this comment.
I accept this is the best solution, but since it does open us to NPEs, was there any alternative other than the nil set? (I strongly guess not, but just double-checking. I see the several checks you had to add for m.table == nil, and that makes me nervous.)
Contributor
Author
There was a problem hiding this comment.
I mean, we could add a bool like m.frozen or such, but I feel like this struct is small and self-contained enough that it's easiest to just be careful about nils.
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.
Until now, there was a subtle race between session.die and a new stream being created.
In session.die, we used
streamMap.Eachto close all streams. We also setlocal.goneAwayin die, which in theory should mean new streams aren't added.However, setting
goneAway, iterating withEach, and adding a stream aren't adequately synchronized, so it's possible to end up in a state where we've got a goroutine already inhandleSyn, about to add a new stream, and then we context switch to 'die' and cleanup existing streams, and then switch back and add the stream.This puts the stream in a state where it will never get closed since
diewon't run again (due todieOnce), however we won't process the stream properly sincedieclosed thes.deadchannel and cleaned up stuff related to that.So, what's the fix?
Instead of using
streamMap.Eachduringdie, we usestreamMap.Drain()to atomically clear the map and mark it drained, and then in Set we return whether we just added to a drained map (the race condition), and if we did we knock over the stream we just created.The included test failed before this change.
The test was written by claude, the fix was written by me.