Fix memory leak in _tasks_by_key cleanup#309
Merged
chrisguidry merged 1 commit intomainfrom Jan 27, 2026
Merged
Conversation
The worker was storing tasks in `_tasks_by_key[execution.key]` but then trying to remove them with `_tasks_by_key.pop(task.get_name())`. Since task names are formatted as "docket-name - task:key" while the dict key is just "key", the pop never found a match and entries accumulated forever. Each leaked entry held a reference to the asyncio.Task and its associated Execution, so memory grew linearly with task count. Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
📚 Documentation has been built for this PR! You can download the documentation directly here: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #309 +/- ##
=======================================
Coverage 98.61% 98.61%
=======================================
Files 99 99
Lines 9906 9906
Branches 483 483
=======================================
Hits 9769 9769
Misses 121 121
Partials 16 16
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
chrisguidry
added a commit
that referenced
this pull request
Jan 27, 2026
Adds tests that verify Worker and StrikeList internal data structures are properly cleaned up after task completion and context exit. These tests would have caught the memory leak fixed in #309 where `_tasks_by_key` entries accumulated because we were using `task.get_name()` instead of `execution.key` for cleanup. Worker invariant tests check: - `_tasks_by_key` is empty after `run_until_finished()` - `_tasks_by_key` doesn't grow across multiple batches - `_execution_counts` is cleared after `run_at_most()` - Internal attributes are deleted after `__aexit__` - Cleanup works with failing tasks and varied task types - SharedContext ContextVars are properly reset StrikeList invariant tests check: - `_conditions` only contains default after removing temp conditions - No empty dicts remain in `task_strikes`/`parameter_strikes` after restore Also reorganizes tests into subdirectories: - `tests/worker/` for worker-related tests - `tests/instrumentation/` for metrics and tracing tests - Renames `test_synced_strikelist.py` to `test_strikelist.py` Adds `--import-mode=importlib` to pytest config to support same-named test files in different directories. Co-Authored-By: Claude Opus 4.5 <[email protected]>
chrisguidry
added a commit
that referenced
this pull request
Jan 27, 2026
) Adds tests that verify Worker and StrikeList internal data structures are properly cleaned up after task completion and context exit. These tests would have caught the memory leak fixed in #309 where `_tasks_by_key` entries accumulated because we were using `task.get_name()` instead of `execution.key` for cleanup. Worker invariant tests check: - `_tasks_by_key` is empty after `run_until_finished()` - `_tasks_by_key` doesn't grow across multiple batches - `_execution_counts` is cleared after `run_at_most()` - Internal attributes are deleted after `__aexit__` - Cleanup works with failing tasks and varied task types - SharedContext ContextVars are properly reset StrikeList invariant tests check: - `_conditions` only contains default after removing temp conditions - No empty dicts remain in `task_strikes`/`parameter_strikes` after restore Also reorganizes tests into subdirectories: - `tests/worker/` for worker-related tests - `tests/instrumentation/` for metrics and tracing tests - Renames `test_synced_strikelist.py` to `test_strikelist.py` Adds `--import-mode=importlib` to pytest config to support same-named test files in different directories. 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude Opus 4.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The worker was storing tasks in
_tasks_by_key[execution.key]but then trying to remove them with_tasks_by_key.pop(task.get_name()). Since task names are formatted as"docket-name - task:key"while the dict key is just"key", the pop never found a match and entries accumulated forever.Each leaked entry held a reference to the asyncio.Task and its associated Execution, so memory grew linearly with task count.
🤖 Generated with Claude Code