fix race condition in GetOrOpenStream in incoming streams map

This commit is contained in:
Marten Seemann
2018-02-22 09:20:57 +08:00
parent 6322412eb8
commit 77a4bf17ca
3 changed files with 9 additions and 3 deletions

View File

@@ -69,18 +69,20 @@ func (m *incomingBidiStreamsMap) AcceptStream() (streamI, error) {
}
func (m *incomingBidiStreamsMap) GetOrOpenStream(id protocol.StreamID) (streamI, error) {
m.mutex.RLock()
if id > m.maxStream {
m.mutex.RUnlock()
return nil, fmt.Errorf("peer tried to open stream %d (current limit: %d)", id, m.maxStream)
}
// if the id is smaller than the highest we accepted
// * this stream exists in the map, and we can return it, or
// * this stream was already closed, then we can return the nil
if id <= m.highestStream {
m.mutex.RLock()
s := m.streams[id]
m.mutex.RUnlock()
return s, nil
}
m.mutex.RUnlock()
m.mutex.Lock()
var start protocol.StreamID

View File

@@ -67,18 +67,20 @@ func (m *incomingItemsMap) AcceptStream() (item, error) {
}
func (m *incomingItemsMap) GetOrOpenStream(id protocol.StreamID) (item, error) {
m.mutex.RLock()
if id > m.maxStream {
m.mutex.RUnlock()
return nil, fmt.Errorf("peer tried to open stream %d (current limit: %d)", id, m.maxStream)
}
// if the id is smaller than the highest we accepted
// * this stream exists in the map, and we can return it, or
// * this stream was already closed, then we can return the nil
if id <= m.highestStream {
m.mutex.RLock()
s := m.streams[id]
m.mutex.RUnlock()
return s, nil
}
m.mutex.RUnlock()
m.mutex.Lock()
var start protocol.StreamID

View File

@@ -69,18 +69,20 @@ func (m *incomingUniStreamsMap) AcceptStream() (receiveStreamI, error) {
}
func (m *incomingUniStreamsMap) GetOrOpenStream(id protocol.StreamID) (receiveStreamI, error) {
m.mutex.RLock()
if id > m.maxStream {
m.mutex.RUnlock()
return nil, fmt.Errorf("peer tried to open stream %d (current limit: %d)", id, m.maxStream)
}
// if the id is smaller than the highest we accepted
// * this stream exists in the map, and we can return it, or
// * this stream was already closed, then we can return the nil
if id <= m.highestStream {
m.mutex.RLock()
s := m.streams[id]
m.mutex.RUnlock()
return s, nil
}
m.mutex.RUnlock()
m.mutex.Lock()
var start protocol.StreamID