-
Notifications
You must be signed in to change notification settings - Fork 717
fix: interrupt monitoring buffer wait on shutdown #3457
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
Sanjays2402
wants to merge
2
commits into
ModelEngine-Group:develop
Choose a base branch
from
Sanjays2402:fix/3403-monitoring-stop-wakeup
base: develop
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import time | ||
|
|
||
| from sdk.nexent.monitor.monitoring import MonitoringRecordBuffer | ||
|
|
||
|
|
||
| def test_stop_interrupts_flush_interval(monkeypatch): | ||
| monkeypatch.setenv("ENABLE_MODEL_MONITORING", "true") | ||
| monkeypatch.setenv("MODEL_MONITORING_FLUSH_INTERVAL_SECONDS", "20") | ||
| buffer = MonitoringRecordBuffer() | ||
|
|
||
| time.sleep(0.1) | ||
| started = time.monotonic() | ||
| buffer.stop() | ||
|
|
||
| # The legacy worst case was ~2s (one tenth of the 20s interval); a 2s bound | ||
| # still fails the old behaviour while leaving headroom on a loaded runner. | ||
| assert time.monotonic() - started < 2.0 | ||
| assert not buffer._flush_thread.is_alive() | ||
|
|
||
|
|
||
| def test_flush_error_does_not_delay_retry_by_a_full_interval(monkeypatch): | ||
| monkeypatch.setenv("ENABLE_MODEL_MONITORING", "false") | ||
| monkeypatch.setenv("MODEL_MONITORING_FLUSH_INTERVAL_SECONDS", "20") | ||
| buffer = MonitoringRecordBuffer() | ||
|
|
||
| waits = [] | ||
|
|
||
| def fake_wait(timeout=None): | ||
| waits.append(timeout) | ||
| return len(waits) >= 3 | ||
|
|
||
| monkeypatch.setattr(buffer._stop_event, "wait", fake_wait) | ||
|
|
||
| def boom(): | ||
| raise RuntimeError("transient") | ||
|
|
||
| monkeypatch.setattr(buffer, "_flush_to_db", boom) | ||
| buffer._buffer.append({"record": 1}) | ||
| buffer._last_flush_time = 0.0 | ||
| buffer._running = True | ||
|
|
||
| buffer._flush_loop() | ||
|
|
||
| # Retries back off from a short delay instead of always waiting the full | ||
| # 20s flush interval. | ||
| assert waits == [1.0, 2.0, 4.0] |
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.
All three review points were fair - pushed f1481ba.
Retry latency: right, the
exceptfell straight through to a fullflush_intervalwait, so a transient DB error delayed the next attempt by up to 30s by default. The loop now tracks aretry_delaythat starts at_error_retry_delay(1s, or the flush interval if that is shorter) and doubles up to the interval, resetting to 0 on a clean pass. The wait is still_stop_event.wait(...), so shutdown stays immediate on the backoff path too.Test import: dropped the
spec_from_file_locationloading. The suite'sconftest.pyalready registerssdk.nexent.monitor.monitoringinsys.modulesbefore collection, so a plainfrom sdk.nexent.monitor.monitoring import MonitoringRecordBuffergets the same module the rest oftest_monitoring.pyuses, with no second copy of the module-level singletons.Timing bound: raised 0.5s to 2.0s. The legacy code slept in ten
flush_interval / 10chunks, so with the 20s interval the pre-fix worst case is ~2s - the bound still fails the old behaviour while leaving headroom on a loaded runner.Added
test_flush_error_does_not_delay_retry_by_a_full_interval, which stubs_flush_to_dbto raise and asserts the wait sequence is[1.0, 2.0, 4.0]rather than three 20s waits. RED-verified: withmonitoring.pyreverted it fails at that assertion (the loop waits the full interval each time).test/sdk/monitor/is 94 passed on the fixed tree; the one failure there,test_agent_context_survives_delayed_async_stream_iteration, fails identically on the stashed tree (missing async plugin locally) and is unrelated.