Skip to content

Commit

Permalink
Adds a lock to stream creation, solving several hair-pulling race con…
Browse files Browse the repository at this point in the history
…ditions
  • Loading branch information
mpiraux committed Mar 24, 2019
1 parent 466d218 commit 8570e6b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"sort"
"strings"
"sync"
"unsafe"
)

Expand Down Expand Up @@ -220,7 +221,7 @@ func (c *Connection) TransitionTo(version uint32, ALPN string) {
c.CryptoStates = make(map[EncryptionLevel]*CryptoState)
c.CryptoStreams = make(map[PNSpace]*Stream)
c.CryptoStates[EncryptionLevelInitial] = NewInitialPacketProtection(c)
c.Streams = Streams{make(map[uint64]*Stream), &c.StreamInput}
c.Streams = Streams{streams: make(map[uint64]*Stream), lock: &sync.Mutex{}, input: &c.StreamInput}
}
func (c *Connection) CloseConnection(quicLayer bool, errCode uint16, reasonPhrase string) {
if quicLayer {
Expand Down
6 changes: 5 additions & 1 deletion streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package quictracker
import (
"fmt"
"math"
"sync"
)

type StreamsType bool
Expand Down Expand Up @@ -45,13 +46,16 @@ type StreamInput struct {

type Streams struct {
streams map[uint64]*Stream
input *Broadcaster
lock *sync.Mutex
input *Broadcaster
}

func (s Streams) Get(streamId uint64) *Stream {
s.lock.Lock()
if s.streams[streamId] == nil {
s.streams[streamId] = NewStream()
}
s.lock.Unlock()
return s.streams[streamId]
}

Expand Down

0 comments on commit 8570e6b

Please sign in to comment.