Commit ae37050e authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: muxconn can't use stream ID 0 ever

parent 7177cc14
## 0.5.1 (unreleased)
BUG FIXES:
* core: If a stream ID loops around, don't let it use stream ID 0 [GH-767]
## 0.5.0 (12/30/2013)
......
......@@ -191,13 +191,13 @@ func (m *MuxConn) NextId() uint32 {
m.muAccept.Lock()
defer m.muAccept.Unlock()
for {
// We never use stream ID 0 because 0 is the zero value of a uint32
// and we want to reserve that for "not in use"
if m.curId == 0 {
m.curId = 1
}
for {
result := m.curId
m.curId += 1
if _, ok := m.streamsAccept[result]; !ok {
......@@ -416,9 +416,9 @@ func (m *MuxConn) loop() {
stream.mu.Unlock()
}
}
}
}
func (m *MuxConn) write(from muxPacketFrom, id uint32, dataType muxPacketType, p []byte) (int, error) {
func (m *MuxConn) write(from muxPacketFrom, id uint32, dataType muxPacketType, p []byte) (int, error) {
m.wlock.Lock()
defer m.wlock.Unlock()
......@@ -438,11 +438,11 @@ func (m *MuxConn) write(from muxPacketFrom, id uint32, dataType muxPacketType, p
return 0, nil
}
return m.rwc.Write(p)
}
}
// Stream is a single stream of data and implements io.ReadWriteCloser.
// A Stream is full-duplex so you can write data as well as read data.
type Stream struct {
// Stream is a single stream of data and implements io.ReadWriteCloser.
// A Stream is full-duplex so you can write data as well as read data.
type Stream struct {
from muxPacketFrom
id uint32
mux *MuxConn
......@@ -452,11 +452,11 @@ type Stream struct {
stateUpdated time.Time
mu sync.Mutex
writeCh chan<- []byte
}
}
type streamState byte
type streamState byte
const (
const (
streamStateClosed streamState = iota
streamStateListen
streamStateSynRecv
......@@ -467,9 +467,9 @@ const (
streamStateCloseWait
streamStateClosing
streamStateLastAck
)
)
func newStream(from muxPacketFrom, id uint32, m *MuxConn) *Stream {
func newStream(from muxPacketFrom, id uint32, m *MuxConn) *Stream {
// Create the stream object and channel where data will be sent to
dataR, dataW := io.Pipe()
writeCh := make(chan []byte, 4096)
......@@ -505,9 +505,9 @@ func newStream(from muxPacketFrom, id uint32, m *MuxConn) *Stream {
}()
return stream
}
}
func (s *Stream) Close() error {
func (s *Stream) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
......@@ -523,13 +523,13 @@ func (s *Stream) Close() error {
s.write(muxPacketFin, nil)
return nil
}
}
func (s *Stream) Read(p []byte) (int, error) {
func (s *Stream) Read(p []byte) (int, error) {
return s.reader.Read(p)
}
}
func (s *Stream) Write(p []byte) (int, error) {
func (s *Stream) Write(p []byte) (int, error) {
s.mu.Lock()
state := s.state
s.mu.Unlock()
......@@ -539,13 +539,13 @@ func (s *Stream) Write(p []byte) (int, error) {
}
return s.write(muxPacketData, p)
}
}
func (s *Stream) closeWriter() {
func (s *Stream) closeWriter() {
s.writeCh <- nil
}
}
func (s *Stream) setState(state streamState) {
func (s *Stream) setState(state streamState) {
//log.Printf("[TRACE] %p: Stream %d (%s) went to state %d", s.mux, s.id, s.from, state)
s.state = state
s.stateUpdated = time.Now().UTC()
......@@ -555,9 +555,9 @@ func (s *Stream) setState(state streamState) {
default:
}
}
}
}
func (s *Stream) waitState(target streamState) error {
func (s *Stream) waitState(target streamState) error {
// Register a state change listener to wait for changes
stateCh := make(chan streamState, 10)
s.stateChange[stateCh] = struct{}{}
......@@ -574,8 +574,8 @@ func (s *Stream) waitState(target streamState) error {
} else {
return fmt.Errorf("Stream %d went to bad state: %d", s.id, state)
}
}
}
func (s *Stream) write(dataType muxPacketType, p []byte) (int, error) {
func (s *Stream) write(dataType muxPacketType, p []byte) (int, error) {
return s.mux.write(s.from, s.id, dataType, p)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment