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

Fix deadlock if Stream Shutdown races with Push. #190

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ Peter Kurfer <[email protected]>
Sam Roberts <[email protected]>
Moritz Wanzenböck <[email protected]>
Jenni Griesmann <[email protected]>
Nicholas Kwan <[email protected]>
16 changes: 9 additions & 7 deletions internal/event/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Stream struct {
in, out chan Event

// terminates processing
ctx context.Context
shutdown context.CancelFunc
}

Expand All @@ -59,7 +60,7 @@ func NewStream(program uint32, cbID int32) *Stream {

// Start the processing loop, which will return a routine we can use to
// shut the queue down later.
s.shutdown = s.start()
s.start()

return s
}
Expand All @@ -83,7 +84,10 @@ func (s *Stream) Recv() chan Event {

// Push appends a new event to the queue.
func (s *Stream) Push(e Event) {
s.in <- e
select {
case s.in <- e:
case <-s.ctx.Done():
}
}

// Shutdown gracefully terminates Stream processing, releasing all internal
Expand All @@ -97,12 +101,10 @@ func (s *Stream) Shutdown() {

// start starts the event processing loop, which will continue to run until
// terminated by the returned context.CancelFunc.
func (s *Stream) start() context.CancelFunc {
ctx, cancel := context.WithCancel(context.Background())

go s.process(ctx)
func (s *Stream) start() {
s.ctx, s.shutdown = context.WithCancel(context.Background())

return cancel
go s.process(s.ctx)
}

// process manages an Stream's lifecycle until canceled by the provided context.
Expand Down