Skip to content

fix: require explicit muxer start before handshake #1002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ func (c *Connection) setupConnection() error {
} else {
c.handshake.Client.Start()
}
c.muxer.StartOnce()
// Wait for handshake completion or error
select {
case <-c.doneChan:
Expand Down
32 changes: 20 additions & 12 deletions muxer/muxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ func (m *Muxer) Start() {
}
}

// StartOnce unblocks the read loop for one iteration. This is generally used to perform the handshake before registering
// additional protocols and calling Start
func (m *Muxer) StartOnce() {
select {
case m.startChan <- false:
default:
}
}

// Stop shuts down the muxer
func (m *Muxer) Stop() {
m.onceStop.Do(func() {
Expand Down Expand Up @@ -262,6 +271,17 @@ func (m *Muxer) readLoop() {
return
default:
}
// Wait until the muxer is started to continue
if !started {
select {
case <-m.doneChan:
// Break out of read loop if we're shutting down
return
case v := <-m.startChan:
// We block again on the next iteration of we get 'false' from startChan
started = v
}
}
header := SegmentHeader{}
if err := binary.Read(m.conn, binary.BigEndian, &header); err != nil {
if errors.Is(err, io.ErrClosedPipe) {
Expand Down Expand Up @@ -334,17 +354,5 @@ func (m *Muxer) readLoop() {
return
}
recvChan <- msg

// Wait until the muxer is started to continue
// We don't want to read more than one segment until the handshake is complete
if !started {
select {
case <-m.doneChan:
// Break out of read loop if we're shutting down
return
case <-m.startChan:
started = true
}
}
}
}
Loading