Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protect against concurrent use of Stream.Write #3381

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions send_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type sendStream struct {
nextFrame *wire.StreamFrame

writeChan chan struct{}
writeOnce chan struct{}
deadline time.Time

flowController flowcontrol.StreamFlowController
Expand All @@ -73,6 +74,7 @@ func newSendStream(
sender: sender,
flowController: flowController,
writeChan: make(chan struct{}, 1),
writeOnce: make(chan struct{}, 1), // cap: 1, to protect against concurrent use of Write
version: version,
}
s.ctx, s.ctxCancel = context.WithCancel(context.Background())
Expand All @@ -84,6 +86,12 @@ func (s *sendStream) StreamID() protocol.StreamID {
}

func (s *sendStream) Write(p []byte) (int, error) {
// Concurrent use of Write is not permitted (and doesn't make any sense),
// but sometimes people do it anyway.
// Make sure that we only execute one call at any given time to avoid hard to debug failures.
s.writeOnce <- struct{}{}
defer func() { <-s.writeOnce }()

s.mutex.Lock()
defer s.mutex.Unlock()

Expand Down