Skip to content

Commit

Permalink
protect against concurrent use of Stream.Write (#3381)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Apr 25, 2022
1 parent ec118e4 commit 8bcb633
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions send_stream.go
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

0 comments on commit 8bcb633

Please sign in to comment.