-
Notifications
You must be signed in to change notification settings - Fork 52.8k
fix(gateway): systemd KillMode=mixed + restart drain timeout + cron stop guard (#37454 #37453 #37858) #39577
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
ashishpatel26
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
ashishpatel26:fix/systemd-rebase-37454-37453-37858
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.
+272
−6
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
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,137 @@ | ||
| """Tests for the cron-ticker drain guard (issue #37858). | ||
|
|
||
| When ``hermes gateway stop`` initiates shutdown, the gateway sets | ||
| ``runner._draining = True`` before the drain loop runs. If the cron ticker | ||
| background thread fires a tick at that exact moment it can launch a new | ||
| outbound agent API call — making an LLM request and (on cron-deliver jobs) | ||
| sending a platform message — after the operator has already asked the gateway | ||
| to stop. | ||
|
|
||
| The fix passes the ``runner`` reference into ``_start_cron_ticker`` so the | ||
| loop can skip ``cron_tick()`` calls whenever ``runner._draining`` is True. | ||
| """ | ||
|
|
||
| import threading | ||
| import time | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
|
|
||
| from gateway.run import _start_cron_ticker | ||
|
|
||
|
|
||
| class _FakeRunner: | ||
| """Minimal stand-in for GatewayRunner that exposes only the _draining flag.""" | ||
|
|
||
| def __init__(self, *, draining: bool = False): | ||
| self._draining = draining | ||
|
|
||
|
|
||
| def test_cron_ticker_skips_tick_when_runner_is_draining(): | ||
| """While runner._draining is True the ticker must NOT call cron_tick().""" | ||
| tick_calls = [] | ||
|
|
||
| stop_event = threading.Event() | ||
| runner = _FakeRunner(draining=True) | ||
|
|
||
| # Patch cron_tick so we can count invocations without running real jobs. | ||
| with patch("cron.scheduler.tick", side_effect=lambda **kw: tick_calls.append(1)) as _mock_tick: | ||
| # Run the ticker in a background thread for a couple of intervals. | ||
| thread = threading.Thread( | ||
| target=_start_cron_ticker, | ||
| args=(stop_event,), | ||
| kwargs={"interval": 0, "runner": runner}, | ||
| daemon=True, | ||
| ) | ||
| thread.start() | ||
| time.sleep(0.15) # enough for several zero-interval ticks | ||
| stop_event.set() | ||
| thread.join(timeout=2.0) | ||
|
|
||
| assert not thread.is_alive(), "Ticker thread did not exit cleanly" | ||
| assert tick_calls == [], ( | ||
| f"cron_tick() was called {len(tick_calls)} time(s) while runner._draining=True — " | ||
| "outbound agent calls must not fire after stop is initiated (#37858)" | ||
| ) | ||
|
|
||
|
|
||
| def test_cron_ticker_runs_tick_when_runner_is_not_draining(): | ||
| """Normal operation: cron_tick() fires when the gateway is NOT draining.""" | ||
| tick_calls = [] | ||
|
|
||
| stop_event = threading.Event() | ||
| runner = _FakeRunner(draining=False) | ||
|
|
||
| with patch("cron.scheduler.tick", side_effect=lambda **kw: tick_calls.append(1)): | ||
| thread = threading.Thread( | ||
| target=_start_cron_ticker, | ||
| args=(stop_event,), | ||
| kwargs={"interval": 0, "runner": runner}, | ||
| daemon=True, | ||
| ) | ||
| thread.start() | ||
| time.sleep(0.15) | ||
| stop_event.set() | ||
| thread.join(timeout=2.0) | ||
|
|
||
| assert not thread.is_alive(), "Ticker thread did not exit cleanly" | ||
| assert tick_calls, ( | ||
| "cron_tick() was never called when runner._draining=False — " | ||
| "normal tick operation is broken" | ||
| ) | ||
|
|
||
|
|
||
| def test_cron_ticker_skips_tick_without_runner(): | ||
| """When runner=None (legacy call sites), the ticker must still call cron_tick() | ||
| unchanged — the drain guard is a no-op when no runner reference is provided.""" | ||
| tick_calls = [] | ||
|
|
||
| stop_event = threading.Event() | ||
|
|
||
| with patch("cron.scheduler.tick", side_effect=lambda **kw: tick_calls.append(1)): | ||
| thread = threading.Thread( | ||
| target=_start_cron_ticker, | ||
| args=(stop_event,), | ||
| kwargs={"interval": 0, "runner": None}, | ||
| daemon=True, | ||
| ) | ||
| thread.start() | ||
| time.sleep(0.15) | ||
| stop_event.set() | ||
| thread.join(timeout=2.0) | ||
|
|
||
| assert not thread.is_alive() | ||
| assert tick_calls, ( | ||
| "cron_tick() was never called when runner=None — " | ||
| "backwards-compat with callers that omit runner is broken" | ||
| ) | ||
|
|
||
|
|
||
| def test_cron_ticker_resumes_after_drain_clears(): | ||
| """Once runner._draining reverts to False, ticks should resume normally. | ||
|
|
||
| This covers the case where the gateway runner temporarily sets _draining | ||
| during a restart then clears it (edge-case drain flag lifecycle). | ||
| """ | ||
| tick_calls = [] | ||
| stop_event = threading.Event() | ||
| runner = _FakeRunner(draining=True) | ||
|
|
||
| with patch("cron.scheduler.tick", side_effect=lambda **kw: tick_calls.append(1)): | ||
| thread = threading.Thread( | ||
| target=_start_cron_ticker, | ||
| args=(stop_event,), | ||
| kwargs={"interval": 0, "runner": runner}, | ||
| daemon=True, | ||
| ) | ||
| thread.start() | ||
| time.sleep(0.1) | ||
| # Simulate drain completing (e.g. the runner resets the flag internally) | ||
| runner._draining = False | ||
| time.sleep(0.15) | ||
| stop_event.set() | ||
| thread.join(timeout=2.0) | ||
|
|
||
| assert not thread.is_alive() | ||
| assert tick_calls, ( | ||
| "cron_tick() should fire once _draining is cleared, but never did" | ||
| ) |
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
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.
_start_cron_tickeris no longer the production gateway ticker on current main: it is now a deprecated shim, whilestart_gateway()invokes the resolvedCronSchedulerdirectly. Please move this drain gate into the active provider startup/InProcessCronScheduler.start()path; otherwise this newrunnerparameter is never used by the running gateway.