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

Conversation

bgentry
Copy link
Contributor

@bgentry bgentry commented Mar 1, 2024

When remote job cancellation was added, a new cancellable context was allocated within the producer before the executor is spawned. The cancel func here was only called if the job was actually cancelled remotely or via a parent context cancellation, meaning we would slowly leak memory for every job worked that wasn't cancelled.

In my recent testing, the memory usage of my sample program is now stable. After churning through >100k jobs with pprof running, it's still cruising at ~17MB usage.

Thank you @brandur for pinpointing the issue, and @shawnstephens for getting us to look into it 馃檹

Fixes #239.

@bgentry bgentry requested a review from brandur March 1, 2024 03:21
@bgentry bgentry force-pushed the bg-fix-job-cancel-context-memory-leak branch from 36b2cb6 to 36aa883 Compare March 1, 2024 03:33
When remote job cancellation was added, a new cancellable context was
allocated within the producer before the executor is spawned. The cancel
func here was only called if the job was actually cancelled remotely or
via a parent context cancellation, meaning we would slowly leak memory
for every job worked that wasn't cancelled.

Thank you @brandur for pinpointing the issue.

Fixes #239.

Co-Authored-By: Brandur Leach <brandur@brandur.org>
@bgentry bgentry force-pushed the bg-fix-job-cancel-context-memory-leak branch from 36aa883 to aa7e128 Compare March 1, 2024 03:38
Builds on the rest of #243 with a few things I'd done in my own version
of the fix:

* Add a test case that verifies that that the executor's cancel function
  was called even in the event of no explicit job cancellation.

* Add a default context cancellation error that's never user visible,
  but which can easily be recognized for testing purposes.
Copy link
Contributor

@brandur brandur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!!

Happily, the fix I'd written looks almost identical to yours. I pushed one more test case into your branch that I'd written this morning that checks that the context was cancelled even if the executor itself was not explicitly cancelled.

@brandur brandur merged commit dba032f into master Mar 1, 2024
10 checks passed
@brandur brandur deleted the bg-fix-job-cancel-context-memory-leak branch March 1, 2024 04:27
brandur added a commit that referenced this pull request Mar 1, 2024
Prepares `v0.0.24`, a small release, but one which contains an important
fix from #243 which resolves a context memory leak that'd cause River's
memory usage to bloat over time.
@brandur brandur mentioned this pull request Mar 1, 2024
brandur added a commit that referenced this pull request Mar 1, 2024
Prepares `v0.0.24`, a small release, but one which contains an important
fix from #243 which resolves a context memory leak that'd cause River's
memory usage to bloat over time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak?
2 participants