-
Notifications
You must be signed in to change notification settings - Fork 48.1k
fix(oneshot): join memory daemon threads before process exit (#37632) #37635
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
Open
liuhao1024
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
liuhao1024:fix/oneshot-memory-shutdown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
β1
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -364,7 +364,19 @@ def _run_agent( | |
| agent.stream_delta_callback = None | ||
| agent.tool_gen_callback = None | ||
|
|
||
| return agent.chat(prompt) or "" | ||
| response = agent.chat(prompt) or "" | ||
|
|
||
| # Join memory-provider daemon threads (e.g. Honcho's honcho-sync, | ||
| # honcho-prewarm-dialectic, honcho-context-prefetch) before the process | ||
| # exits. Without this, CPython's Py_FinalizeEx force-terminates the | ||
| # still-running daemon threads via pthread_exit(), and glibc converts | ||
| # that forced unwind into abort() β SIGABRT β exit 134. (#37632) | ||
| try: | ||
| agent.shutdown_memory_provider() | ||
| except Exception: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please forward the agent's list-valued |
||
| pass # memory providers are strictly best-effort | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def _oneshot_clarify_callback(question: str, choices=None) -> str: | ||
|
|
||
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,75 @@ | ||
| """Regression test for #37632: oneshot mode must call shutdown_memory_provider. | ||
|
|
||
| Without this, Honcho memory daemon threads (honcho-sync, honcho-prewarm-dialectic, | ||
| honcho-context-prefetch) remain blocked in httpx I/O when the process exits. | ||
| CPython's Py_FinalizeEx force-terminates them via pthread_exit(), and glibc | ||
| converts that into abort() β SIGABRT β exit 134. | ||
| """ | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| class TestOneshotMemoryShutdown: | ||
| """shutdown_memory_provider() must be called after agent.chat() in oneshot mode.""" | ||
|
|
||
| @patch("hermes_cli.runtime_provider.resolve_runtime_provider") | ||
| @patch("hermes_cli.config.load_config") | ||
| @patch("hermes_cli.oneshot._create_session_db_for_oneshot", return_value=None) | ||
| @patch("hermes_cli.oneshot.get_fallback_chain", return_value=None) | ||
| @patch("run_agent.AIAgent") | ||
| def test_run_agent_calls_shutdown_after_chat( | ||
| self, MockAIAgent, mock_fb, mock_sdb, mock_load_config, mock_resolve | ||
| ): | ||
| """_run_agent must call agent.shutdown_memory_provider() after chat().""" | ||
| mock_load_config.return_value = {"model": {"default": "test-model", "provider": "test"}} | ||
| mock_resolve.return_value = { | ||
| "api_key": "test-key", | ||
| "base_url": "http://test", | ||
| "provider": "test", | ||
| "api_mode": "chat_completions", | ||
| "credential_pool": None, | ||
| } | ||
|
|
||
| mock_agent = MagicMock() | ||
| mock_agent.chat.return_value = "test response" | ||
| MockAIAgent.return_value = mock_agent | ||
|
|
||
| from hermes_cli.oneshot import _run_agent | ||
|
|
||
| result = _run_agent("test prompt") | ||
|
|
||
| assert result == "test response" | ||
| mock_agent.chat.assert_called_once_with("test prompt") | ||
| mock_agent.shutdown_memory_provider.assert_called_once() | ||
|
|
||
| @patch("hermes_cli.runtime_provider.resolve_runtime_provider") | ||
| @patch("hermes_cli.config.load_config") | ||
| @patch("hermes_cli.oneshot._create_session_db_for_oneshot", return_value=None) | ||
| @patch("hermes_cli.oneshot.get_fallback_chain", return_value=None) | ||
| @patch("run_agent.AIAgent") | ||
| def test_run_agent_shutdown_does_not_crash_on_error( | ||
| self, MockAIAgent, mock_fb, mock_sdb, mock_load_config, mock_resolve | ||
| ): | ||
| """shutdown_memory_provider raising must not crash _run_agent.""" | ||
| mock_load_config.return_value = {"model": {"default": "test-model", "provider": "test"}} | ||
| mock_resolve.return_value = { | ||
| "api_key": "test-key", | ||
| "base_url": "http://test", | ||
| "provider": "test", | ||
| "api_mode": "chat_completions", | ||
| "credential_pool": None, | ||
| } | ||
|
|
||
| mock_agent = MagicMock() | ||
| mock_agent.chat.return_value = "test response" | ||
| mock_agent.shutdown_memory_provider.side_effect = RuntimeError("boom") | ||
| MockAIAgent.return_value = mock_agent | ||
|
|
||
| from hermes_cli.oneshot import _run_agent | ||
|
|
||
| # Must not raise despite shutdown_memory_provider failing | ||
| result = _run_agent("test prompt") | ||
| assert result == "test response" | ||
| mock_agent.shutdown_memory_provider.assert_called_once() |
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.
This needs to be a
finallyaround the agent execution. Ifagent.chat()raises, control escapes before this block and one-shot still exits with provider threads unshut down; current main'srun_conversation()path has the same exception route.