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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leak of job cancellation contexts #243

Merged
merged 2 commits into from Mar 1, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fixed a memory leak caused by not always cancelling the context used to enable jobs to be cancelled remotely. [PR #243](https://github.com/riverqueue/river/pull/243).

## [0.0.23] - 2024-02-29

### Added
Expand Down
7 changes: 7 additions & 0 deletions job_executor.go
Expand Up @@ -17,6 +17,10 @@ import (
"github.com/riverqueue/river/rivertype"
)

// Error used in CancelFunc in cases where the job was not cancelled for
// purposes of resource cleanup. Should never be user visible.
var errExecutorDefaultCancel = errors.New("context cancelled as executor finished")

// UnknownJobKindError is returned when a Client fetches and attempts to
// work a job that has not been registered on the Client's Workers bundle (using
// AddWorker).
Expand Down Expand Up @@ -138,6 +142,9 @@ func (e *jobExecutor) Cancel() {
}

func (e *jobExecutor) Execute(ctx context.Context) {
// Ensure that the context is cancelled no matter what, or it will leak:
defer e.CancelFunc(errExecutorDefaultCancel)

e.start = e.TimeNowUTC()
e.stats = &jobstats.JobStatistics{
QueueWaitDuration: e.start.Sub(e.JobRow.ScheduledAt),
Expand Down
22 changes: 22 additions & 0 deletions job_executor_test.go
Expand Up @@ -174,7 +174,12 @@ func TestJobExecutor_Execute(t *testing.T) {
jobRow: job,
}

// allocate this context just so we can set the CancelFunc:
_, cancel := context.WithCancelCause(ctx)
t.Cleanup(func() { cancel(nil) })

executor := baseservice.Init(archetype, &jobExecutor{
CancelFunc: cancel,
ClientRetryPolicy: &retryPolicyNoJitter{},
Completer: bundle.completer,
ErrorHandler: bundle.errorHandler,
Expand Down Expand Up @@ -618,6 +623,22 @@ func TestJobExecutor_Execute(t *testing.T) {
require.True(t, bundle.errorHandler.HandlePanicCalled)
})

t.Run("CancelFuncCleanedUpEvenWithoutCancel", func(t *testing.T) {
t.Parallel()

executor, bundle := setup(t)

executor.WorkUnit = newWorkUnitFactoryWithCustomRetry(func() error { return nil }, nil).MakeUnit(bundle.jobRow)

workCtx, cancelFunc := context.WithCancelCause(ctx)
executor.CancelFunc = cancelFunc

executor.Execute(workCtx)
executor.Completer.Wait()

require.ErrorIs(t, context.Cause(workCtx), errExecutorDefaultCancel)
})

runCancelTest := func(t *testing.T, returnErr error) *rivertype.JobRow { //nolint:thelper
executor, bundle := setup(t)

Expand All @@ -640,6 +661,7 @@ func TestJobExecutor_Execute(t *testing.T) {

workCtx, cancelFunc := context.WithCancelCause(ctx)
executor.CancelFunc = cancelFunc
t.Cleanup(func() { cancelFunc(nil) })

executor.Execute(workCtx)
executor.Completer.Wait()
Expand Down
1 change: 1 addition & 0 deletions producer.go
Expand Up @@ -358,6 +358,7 @@ func (p *producer) startNewExecutors(workCtx context.Context, jobs []*rivertype.
workUnit = workInfo.workUnitFactory.MakeUnit(job)
}

// jobCancel will always be called by the executor to prevent leaks.
jobCtx, jobCancel := context.WithCancelCause(workCtx)

executor := baseservice.Init(&p.Archetype, &jobExecutor{
Expand Down