forked from quic-go/quic-go
Merge pull request #1189 from lucas-clemente/fix-1188
fix race conditions in new streams maps
This commit is contained in:
@@ -69,20 +69,25 @@ func (m *incomingBidiStreamsMap) AcceptStream() (streamI, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *incomingBidiStreamsMap) GetOrOpenStream(id protocol.StreamID) (streamI, error) {
|
func (m *incomingBidiStreamsMap) GetOrOpenStream(id protocol.StreamID) (streamI, error) {
|
||||||
|
m.mutex.RLock()
|
||||||
if id > m.maxStream {
|
if id > m.maxStream {
|
||||||
|
m.mutex.RUnlock()
|
||||||
return nil, fmt.Errorf("peer tried to open stream %d (current limit: %d)", id, m.maxStream)
|
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
|
// if the id is smaller than the highest we accepted
|
||||||
// * this stream exists in the map, and we can return it, or
|
// * this stream exists in the map, and we can return it, or
|
||||||
// * this stream was already closed, then we can return the nil
|
// * this stream was already closed, then we can return the nil
|
||||||
if id <= m.highestStream {
|
if id <= m.highestStream {
|
||||||
m.mutex.RLock()
|
|
||||||
s := m.streams[id]
|
s := m.streams[id]
|
||||||
m.mutex.RUnlock()
|
m.mutex.RUnlock()
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
m.mutex.RUnlock()
|
||||||
|
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
|
// no need to check the two error conditions from above again
|
||||||
|
// * maxStream can only increase, so if the id was valid before, it definitely is valid now
|
||||||
|
// * highestStream is only modified by this function
|
||||||
var start protocol.StreamID
|
var start protocol.StreamID
|
||||||
if m.highestStream == 0 {
|
if m.highestStream == 0 {
|
||||||
start = m.nextStream
|
start = m.nextStream
|
||||||
|
|||||||
@@ -67,20 +67,25 @@ func (m *incomingItemsMap) AcceptStream() (item, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *incomingItemsMap) GetOrOpenStream(id protocol.StreamID) (item, error) {
|
func (m *incomingItemsMap) GetOrOpenStream(id protocol.StreamID) (item, error) {
|
||||||
|
m.mutex.RLock()
|
||||||
if id > m.maxStream {
|
if id > m.maxStream {
|
||||||
|
m.mutex.RUnlock()
|
||||||
return nil, fmt.Errorf("peer tried to open stream %d (current limit: %d)", id, m.maxStream)
|
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
|
// if the id is smaller than the highest we accepted
|
||||||
// * this stream exists in the map, and we can return it, or
|
// * this stream exists in the map, and we can return it, or
|
||||||
// * this stream was already closed, then we can return the nil
|
// * this stream was already closed, then we can return the nil
|
||||||
if id <= m.highestStream {
|
if id <= m.highestStream {
|
||||||
m.mutex.RLock()
|
|
||||||
s := m.streams[id]
|
s := m.streams[id]
|
||||||
m.mutex.RUnlock()
|
m.mutex.RUnlock()
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
m.mutex.RUnlock()
|
||||||
|
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
|
// no need to check the two error conditions from above again
|
||||||
|
// * maxStream can only increase, so if the id was valid before, it definitely is valid now
|
||||||
|
// * highestStream is only modified by this function
|
||||||
var start protocol.StreamID
|
var start protocol.StreamID
|
||||||
if m.highestStream == 0 {
|
if m.highestStream == 0 {
|
||||||
start = m.nextStream
|
start = m.nextStream
|
||||||
|
|||||||
@@ -69,20 +69,25 @@ func (m *incomingUniStreamsMap) AcceptStream() (receiveStreamI, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *incomingUniStreamsMap) GetOrOpenStream(id protocol.StreamID) (receiveStreamI, error) {
|
func (m *incomingUniStreamsMap) GetOrOpenStream(id protocol.StreamID) (receiveStreamI, error) {
|
||||||
|
m.mutex.RLock()
|
||||||
if id > m.maxStream {
|
if id > m.maxStream {
|
||||||
|
m.mutex.RUnlock()
|
||||||
return nil, fmt.Errorf("peer tried to open stream %d (current limit: %d)", id, m.maxStream)
|
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
|
// if the id is smaller than the highest we accepted
|
||||||
// * this stream exists in the map, and we can return it, or
|
// * this stream exists in the map, and we can return it, or
|
||||||
// * this stream was already closed, then we can return the nil
|
// * this stream was already closed, then we can return the nil
|
||||||
if id <= m.highestStream {
|
if id <= m.highestStream {
|
||||||
m.mutex.RLock()
|
|
||||||
s := m.streams[id]
|
s := m.streams[id]
|
||||||
m.mutex.RUnlock()
|
m.mutex.RUnlock()
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
m.mutex.RUnlock()
|
||||||
|
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
|
// no need to check the two error conditions from above again
|
||||||
|
// * maxStream can only increase, so if the id was valid before, it definitely is valid now
|
||||||
|
// * highestStream is only modified by this function
|
||||||
var start protocol.StreamID
|
var start protocol.StreamID
|
||||||
if m.highestStream == 0 {
|
if m.highestStream == 0 {
|
||||||
start = m.nextStream
|
start = m.nextStream
|
||||||
|
|||||||
@@ -85,10 +85,11 @@ func (m *outgoingBidiStreamsMap) openStreamImpl() (streamI, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *outgoingBidiStreamsMap) GetStream(id protocol.StreamID) (streamI, error) {
|
func (m *outgoingBidiStreamsMap) GetStream(id protocol.StreamID) (streamI, error) {
|
||||||
|
m.mutex.RLock()
|
||||||
if id >= m.nextStream {
|
if id >= m.nextStream {
|
||||||
|
m.mutex.RUnlock()
|
||||||
return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))
|
return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))
|
||||||
}
|
}
|
||||||
m.mutex.RLock()
|
|
||||||
s := m.streams[id]
|
s := m.streams[id]
|
||||||
m.mutex.RUnlock()
|
m.mutex.RUnlock()
|
||||||
return s, nil
|
return s, nil
|
||||||
|
|||||||
@@ -86,10 +86,11 @@ func (m *outgoingItemsMap) openStreamImpl() (item, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *outgoingItemsMap) GetStream(id protocol.StreamID) (item, error) {
|
func (m *outgoingItemsMap) GetStream(id protocol.StreamID) (item, error) {
|
||||||
|
m.mutex.RLock()
|
||||||
if id >= m.nextStream {
|
if id >= m.nextStream {
|
||||||
|
m.mutex.RUnlock()
|
||||||
return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))
|
return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))
|
||||||
}
|
}
|
||||||
m.mutex.RLock()
|
|
||||||
s := m.streams[id]
|
s := m.streams[id]
|
||||||
m.mutex.RUnlock()
|
m.mutex.RUnlock()
|
||||||
return s, nil
|
return s, nil
|
||||||
|
|||||||
@@ -85,10 +85,11 @@ func (m *outgoingUniStreamsMap) openStreamImpl() (sendStreamI, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *outgoingUniStreamsMap) GetStream(id protocol.StreamID) (sendStreamI, error) {
|
func (m *outgoingUniStreamsMap) GetStream(id protocol.StreamID) (sendStreamI, error) {
|
||||||
|
m.mutex.RLock()
|
||||||
if id >= m.nextStream {
|
if id >= m.nextStream {
|
||||||
|
m.mutex.RUnlock()
|
||||||
return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))
|
return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))
|
||||||
}
|
}
|
||||||
m.mutex.RLock()
|
|
||||||
s := m.streams[id]
|
s := m.streams[id]
|
||||||
m.mutex.RUnlock()
|
m.mutex.RUnlock()
|
||||||
return s, nil
|
return s, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user