Skip to content

fix: interrupt monitoring buffer wait on shutdown#3457

Open
Sanjays2402 wants to merge 1 commit into
ModelEngine-Group:developfrom
Sanjays2402:fix/3403-monitoring-stop-wakeup
Open

fix: interrupt monitoring buffer wait on shutdown#3457
Sanjays2402 wants to merge 1 commit into
ModelEngine-Group:developfrom
Sanjays2402:fix/3403-monitoring-stop-wakeup

Conversation

@Sanjays2402

Copy link
Copy Markdown

Monitoring shutdown could block for up to one-tenth of the configured flush interval because the worker slept in uninterruptible slices. The worker now waits on a stop event, so shutdown wakes it immediately while retaining periodic flushing.

The regression test takes about 2 seconds and fails on the original code, then passes in under 0.5 seconds with this change. Focused pytest, Ruff, compilation, and both diff gates pass; the source file's three existing import-order findings are unchanged from develop.

Closes #3403

Monitoring shutdown could block for a fraction of the configured flush interval because the worker used uninterruptible sleeps. Use an event-backed wait so stop wakes the worker immediately, with a focused regression test.
Copilot AI review requested due to automatic review settings July 18, 2026 20:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the SDK monitoring buffer’s background flush worker so shutdown can interrupt the worker’s wait immediately (instead of waiting for a sleep slice), and adds a regression test to ensure stop() completes quickly even with a long flush_interval.

Changes:

  • Replace the flush loop’s sliced time.sleep() waiting with an interruptible threading.Event.wait(timeout=...).
  • Ensure the stop signal wakes the flush thread immediately on shutdown.
  • Add a regression test validating stop() returns quickly and the flush thread terminates.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
sdk/nexent/monitor/monitoring.py Adds a stop event and uses it to make the flush loop’s wait interruptible during shutdown.
test/sdk/monitor/test_monitoring_buffer.py Adds a regression test asserting stop() is not delayed by flush_interval-based waits.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 2542 to +2546
except Exception as e:
logger.error(f"Error in monitoring flush loop: {e}")

for _ in range(10):
if not self._running:
return
time.sleep(self._flush_interval / 10)
if self._stop_event.wait(timeout=self._flush_interval):
return
Comment on lines +1 to +15
import importlib.util
from pathlib import Path
import time


MODULE_PATH = Path(__file__).parents[3] / "sdk" / "nexent" / "monitor" / "monitoring.py"


def _load_monitoring_module():
spec = importlib.util.spec_from_file_location("monitoring_under_test", MODULE_PATH)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
started = time.monotonic()
buffer.stop()

assert time.monotonic() - started < 0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] MonitoringRecordBuffer._flush_loop sleeps an extra flush_interval after stop signal

2 participants