-
Notifications
You must be signed in to change notification settings - Fork 52.5k
fix(honcho): drain context prefetch threads during shutdown #69070
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
Closed
bigstar0920
wants to merge
1
commit into
NousResearch:main
from
bigstar0920:fix/honcho-provider-shutdown
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Regression tests for Honcho provider shutdown.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import threading | ||
| from types import SimpleNamespace | ||
|
|
||
| from plugins.memory.honcho import HonchoMemoryProvider | ||
| from plugins.memory.honcho.session import HonchoSessionManager | ||
|
|
||
|
|
||
| def _async_config() -> SimpleNamespace: | ||
| return SimpleNamespace( | ||
| write_frequency="async", | ||
| dialectic_reasoning_level="low", | ||
| dialectic_dynamic=True, | ||
| dialectic_max_chars=600, | ||
| observation_mode="directional", | ||
| user_observe_me=True, | ||
| user_observe_others=True, | ||
| ai_observe_me=True, | ||
| ai_observe_others=True, | ||
| message_max_chars=25000, | ||
| dialectic_max_input_chars=10000, | ||
| ) | ||
|
|
||
|
|
||
| def test_provider_shutdown_stops_honcho_async_writer() -> None: | ||
| """Provider shutdown must not leave its session writer at interpreter exit.""" | ||
| manager = HonchoSessionManager(config=_async_config()) | ||
| provider = HonchoMemoryProvider() | ||
| provider._manager = manager | ||
|
|
||
| assert manager._async_thread is not None | ||
| assert manager._async_thread.is_alive() | ||
|
|
||
| try: | ||
| provider.shutdown() | ||
| assert not manager._async_thread.is_alive() | ||
| finally: | ||
| # Keep the regression test itself leak-free against the pre-fix code. | ||
| manager.shutdown() | ||
|
|
||
|
|
||
| def test_provider_shutdown_waits_for_context_prefetch() -> None: | ||
| """CLI cleanup must not leave Honcho HTTP work at interpreter finalization.""" | ||
| manager = HonchoSessionManager(config=_async_config()) | ||
| provider = HonchoMemoryProvider() | ||
| provider._manager = manager | ||
| started = threading.Event() | ||
| release = threading.Event() | ||
| shutdown_done = threading.Event() | ||
|
|
||
| def slow_prefetch( | ||
| session_key: str, user_message: str | None = None | ||
| ) -> dict[str, str]: | ||
| started.set() | ||
| release.wait(timeout=2) | ||
| return {"representation": "ready"} | ||
|
|
||
| manager.get_prefetch_context = slow_prefetch # type: ignore[method-assign] | ||
| manager.prefetch_context("session", "query") | ||
| assert started.wait(timeout=1) | ||
|
|
||
| def shut_down_provider() -> None: | ||
| try: | ||
| provider.shutdown() | ||
| finally: | ||
| shutdown_done.set() | ||
|
|
||
| shutdown_thread = threading.Thread(target=shut_down_provider) | ||
| shutdown_thread.start() | ||
| try: | ||
| assert not shutdown_done.wait(timeout=0.05) | ||
| release.set() | ||
| shutdown_thread.join(timeout=1) | ||
| assert shutdown_done.is_set() | ||
| assert not manager._context_prefetch_threads | ||
| finally: | ||
| release.set() | ||
| shutdown_thread.join(timeout=2) | ||
| manager.shutdown() | ||
|
|
||
|
|
||
| def test_context_prefetch_is_rejected_after_shutdown() -> None: | ||
| manager = HonchoSessionManager(config=_async_config()) | ||
| calls = 0 | ||
|
|
||
| def record_prefetch( | ||
| session_key: str, user_message: str | None = None | ||
| ) -> dict[str, str]: | ||
| nonlocal calls | ||
| calls += 1 | ||
| return {} | ||
|
|
||
| manager.get_prefetch_context = record_prefetch # type: ignore[method-assign] | ||
| manager.shutdown() | ||
| manager.prefetch_context("session", "query") | ||
|
|
||
| assert calls == 0 | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current main starts the async writer lazily in
_ensure_async_writer()aftersave()enqueues work (bd1a850fa2), so a newly constructed manager has_async_thread is None. Enqueue a save before asserting writer lifecycle, otherwise this regression test fails when salvaged.