fix: replace asyncio.sleep FAF guards with deterministic awaits#919
Conversation
Bumps cpex to 0.1.0.dev12, which exposes PluginResult.background_tasks and wait_for_background_tasks(). Uses these to eliminate all racy asyncio.sleep() calls that were guarding FIRE_AND_FORGET plugin completion in tests. - Add drain_background_tasks() to mellea/plugins/manager.py: accumulates PluginResult objects with background tasks in invoke_hook(), drains via wait_for_background_tasks() in tests - test/plugins/test_execution_modes.py: capture invoke_hook result and call result.wait_for_background_tasks() directly - test/telemetry/test_metrics_backend.py: replace 7x asyncio.sleep(0.05) with drain_background_tasks() Closes generative-computing#691 Assisted-by: Claude Code Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
Add discard_background_tasks() to clear accumulated FIRE_AND_FORGET results without awaiting them, and call it in the enable_metrics fixture to discard stale results from previous test event loops before each test. Also clears _pending_background_results in shutdown_plugins() for completeness. Assisted-by: Claude Code Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
|
Follow-up to address a test failure in The Fix: added Also clears |
|
Claude decided to delete my worktree for this PR instead of #895 when I went to clean up and I didn't noticed it picked the wrong branch name |
|
cc @araujof as the original plugins/hook author |
planetf1
left a comment
There was a problem hiding this comment.
memory leak to consider
Adds `_collect_background_results` boolean (default False) so `_pending_background_results` is never populated in production. Collection is enabled/disabled explicitly by test fixtures via the new `enable_background_collection()` / `disable_background_collection()` helpers, eliminating the unbounded growth on metrics-enabled servers. Also replaces "FAF" abbreviation in docstrings with "fire-and-forget" for clarity. Assisted-by: Claude Code Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
|
@planetf1 Your review should be addressed if you want to take another look |
bd7fa38
…rative-computing#919) * fix: replace asyncio.sleep FAF guards with deterministic awaits Bumps cpex to 0.1.0.dev12, which exposes PluginResult.background_tasks and wait_for_background_tasks(). Uses these to eliminate all racy asyncio.sleep() calls that were guarding FIRE_AND_FORGET plugin completion in tests. - Add drain_background_tasks() to mellea/plugins/manager.py: accumulates PluginResult objects with background tasks in invoke_hook(), drains via wait_for_background_tasks() in tests - test/plugins/test_execution_modes.py: capture invoke_hook result and call result.wait_for_background_tasks() directly - test/telemetry/test_metrics_backend.py: replace 7x asyncio.sleep(0.05) with drain_background_tasks() Closes generative-computing#691 Assisted-by: Claude Code Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com> * fix: isolate FAF plugin results per test to prevent cross-loop errors Add discard_background_tasks() to clear accumulated FIRE_AND_FORGET results without awaiting them, and call it in the enable_metrics fixture to discard stale results from previous test event loops before each test. Also clears _pending_background_results in shutdown_plugins() for completeness. Assisted-by: Claude Code Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com> * fix: gate background result collection behind opt-in flag Adds `_collect_background_results` boolean (default False) so `_pending_background_results` is never populated in production. Collection is enabled/disabled explicitly by test fixtures via the new `enable_background_collection()` / `disable_background_collection()` helpers, eliminating the unbounded growth on metrics-enabled servers. Also replaces "FAF" abbreviation in docstrings with "fire-and-forget" for clarity. Assisted-by: Claude Code Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com> --------- Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
Misc PR
Type of PR
Description
Bumps
cpexto0.1.0.dev12, which addsPluginResult.background_tasksandwait_for_background_tasks()(upstream contextforge-org/contextforge-plugins-framework#33). Uses these to replace all racyasyncio.sleep()guards that were used to wait for FIRE_AND_FORGET plugin completion in tests.Changes:
mellea/plugins/manager.py: adddrain_background_tasks()— accumulatesPluginResultobjects with background tasks ininvoke_hook(), drains viawait_for_background_tasks()for tests where the hook fires inside a backend calltest/plugins/test_execution_modes.py: captureinvoke_hookresult directly and callresult.wait_for_background_tasks()(2 tests)test/telemetry/test_metrics_backend.py: replace 7×asyncio.sleep(0.05)withdrain_background_tasks()Testing
Attribution