Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions dimos/core/native_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,17 @@ def stop(self) -> None:
)
self._process.kill()
self._process.wait(timeout=5)
if self._watchdog is not None and self._watchdog is not threading.current_thread():
self._watchdog.join(timeout=2)
self._watchdog = None
self._process = None
super().stop()
# Join the watchdog AFTER super().stop() so all module threads are
# cleaned up first. When the watchdog itself is the caller (crash
# path), it skips joining itself — but the thread exits naturally
# right after this returns. A second stop() from external code
# (e.g. test teardown) will reach here and join the now-finished
# watchdog thread, preventing monitor_threads from seeing a leak.
if self._watchdog is not None and self._watchdog is not threading.current_thread():
self._watchdog.join(timeout=2)
self._watchdog = None

def _watch_process(self) -> None:
"""Block until the native process exits; trigger stop() if it crashed."""
Expand Down
4 changes: 4 additions & 0 deletions dimos/core/test_native_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def test_process_crash_triggers_stop() -> None:

assert mod._process is None, f"Watchdog did not clean up after process {pid} died"

# Join the watchdog thread. stop() is idempotent but will now join the
# watchdog on the second call since the reference is preserved.
mod.stop()


@pytest.mark.slow
def test_manual(dimos_cluster: ModuleCoordinator, args_file: str) -> None:
Expand Down
Loading