Skip to content
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

Exit goroutine when stream closes #2

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 20 additions & 3 deletions v2/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ func (m *Mux) readLoop() {
cond: sync.Cond{L: new(sync.Mutex)},
covert: covert,
established: true,

closed: make(chan struct{}),
}
m.streams[h.id] = curStream
m.cond.Broadcast() // wake (*Mux).AcceptStream
Expand Down Expand Up @@ -314,6 +316,8 @@ func (m *Mux) DialStream() *Stream {
cond: sync.Cond{L: new(sync.Mutex)},
established: false,
err: m.err, // stream is unusable if m.err is set

closed: make(chan struct{}),
}
m.streams[s.id] = s
m.nextID += 2
Expand Down Expand Up @@ -341,15 +345,19 @@ func (m *Mux) DialCovertStream() *Stream {

// DialStreamContext creates a new Stream with the provided context. When the
// context expires, the Stream will be closed and any pending calls will return
// ctx.Err(). DialStreamContext spawns a goroutine whose lifetime matches that
// of the context.
// ctx.Err().
//
// Unlike e.g. net.Dial, this does not perform any I/O; the peer will not be
// aware of the new Stream until Write is called.
func (m *Mux) DialStreamContext(ctx context.Context) *Stream {
s := m.DialStream()
go func() {
<-ctx.Done()
select {
case <-s.closed:
return
case <-ctx.Done():
}

s.cond.L.Lock()
defer s.cond.L.Unlock()
if ctx.Err() != nil && s.err == nil {
Expand Down Expand Up @@ -425,6 +433,8 @@ type Stream struct {
err error
readBuf []byte
rd, wd time.Time // deadlines

closed chan struct{} // closed when the Stream is closed
}

// LocalAddr returns the underlying connection's LocalAddr.
Expand Down Expand Up @@ -567,6 +577,13 @@ func (s *Stream) Write(p []byte) (int, error) {

// Close closes the Stream. The underlying connection is not closed.
func (s *Stream) Close() error {
select {
case <-s.closed:
default:
// close the channel to signal the context goroutine to exit
close(s.closed)
}

// cancel outstanding Read/Write calls
//
// NOTE: Read calls will be interrupted immediately, but Write calls might
Expand Down
Loading