Skip to content

fix: select on context to avoid leak in logFollower #7792

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
Jun 2, 2023
Merged
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
20 changes: 17 additions & 3 deletions coderd/provisionerjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ func newLogFollower(
}

func (f *logFollower) follow() {
var cancel context.CancelFunc
f.ctx, cancel = context.WithCancel(f.ctx)
defer cancel()
// note that we only need to subscribe to updates if the job is not yet
// complete.
if !f.complete {
Expand Down Expand Up @@ -405,17 +408,28 @@ func (f *logFollower) follow() {
}

func (f *logFollower) listener(_ context.Context, message []byte, err error) {
// in this function we always pair writes to channels with a select on the context
// otherwise we could block a goroutine if the follow() method exits.
if err != nil {
f.errors <- err
select {
case <-f.ctx.Done():
case f.errors <- err:
}
return
}
var n provisionersdk.ProvisionerJobLogsNotifyMessage
err = json.Unmarshal(message, &n)
if err != nil {
f.errors <- err
select {
case <-f.ctx.Done():
case f.errors <- err:
}
return
}
f.notifications <- n
select {
case <-f.ctx.Done():
case f.notifications <- n:
}
}

// query fetches the latest job logs from the database and writes them to the
Expand Down