Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

wait on the kernel owner's exit instead of polling it every second (#3229)

Sebastian Müller2608039dccae
diff --git a/prime-agent-runtime/src/rlm/repl.py b/prime-agent-runtime/src/rlm/repl.py
index d97282be68..6a30bacd7e 100644
--- a/prime-agent-runtime/src/rlm/repl.py
+++ b/prime-agent-runtime/src/rlm/repl.py
@@ -12,12 +12,14 @@ import asyncio
import codecs
import contextvars
import ctypes
+import functools
import inspect
import io
import json
import linecache
import os
import platform
+import select
import signal
import sys
import tempfile
@@ -1495,6 +1497,40 @@ def _owner_alive_posix(owner: int, initial_ppid: int) -> bool:
return True
+def _wait_owner_posix(owner: int, initial_ppid: int) -> None:
+ # Blocks until the owner exits: the OS exit notification (kqueue NOTE_EXIT
+ # on macOS/BSD, a pidfd on Linux) wakes this thread once, with no polling.
+ # Registration binds whichever process holds the pid right now, so the
+ # liveness check after it catches a parent owner that died (and whose pid
+ # may be reused) before the watch existed.
+ try:
+ if hasattr(select, "kqueue"):
+ kq = select.kqueue()
+ # max_events=0 makes a registration error raise (ESRCH for a gone
+ # owner) instead of arriving as an EV_ERROR event.
+ kq.control(
+ [select.kevent(owner, select.KQ_FILTER_PROC, select.KQ_EV_ADD, select.KQ_NOTE_EXIT)],
+ 0,
+ 0,
+ )
+ wait_for_exit = functools.partial(kq.control, None, 1)
+ else:
+ poller = select.poll()
+ poller.register(os.pidfd_open(owner), select.POLLIN)
+ wait_for_exit = poller.poll
+ except ProcessLookupError:
+ return # already gone
+ except (AttributeError, OSError):
+ # Slow fallback, only where exit notification is unavailable: Linux
+ # before 5.3, a seccomp filter denying pidfd_open, a CPython built
+ # without os.pidfd_open.
+ while _owner_alive_posix(owner, initial_ppid):
+ time.sleep(30.0)
+ return
+ if _owner_alive_posix(owner, initial_ppid):
+ wait_for_exit()
+
+
def _wait_owner_windows(owner: int) -> None:
# Blocks until the owner exits. os.kill(pid, 0) on Windows TERMINATES the
# target, so a SYNCHRONIZE handle wait is the only sound probe.
@@ -1522,8 +1558,7 @@ def _owner_watchdog(owner: int, initial_ppid: int) -> None:
if os.name == "nt":
_wait_owner_windows(owner)
else:
- while _owner_alive_posix(owner, initial_ppid):
- time.sleep(1.0)
+ _wait_owner_posix(owner, initial_ppid)
# Event-loop-independent by design: a synchronous cell monopolizes the
# loop, so the queued EOF shutdown can never run; hard-exit from here.
try:
diff --git a/prime-agent-runtime/test/test_repl.py b/prime-agent-runtime/test/test_repl.py
index eb3f68a932..1657cd0d36 100644
--- a/prime-agent-runtime/test/test_repl.py
+++ b/prime-agent-runtime/test/test_repl.py
@@ -2984,5 +2984,48 @@ class OwnerWatchdogTest(unittest.TestCase):
self.assertEqual(calls, [("OpenProcess", 0x00100000, False, 778)])
+class OwnerExitWaitTest(unittest.TestCase):
+ def setUp(self):
+ sys.path.insert(0, SRC)
+ self.addCleanup(sys.path.remove, SRC)
+ import rlm.repl as repl_module
+
+ self.repl_module = repl_module
+ self.owner = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(300)"])
+ self.addCleanup(self.owner.wait)
+ self.addCleanup(self.owner.kill)
+
+ def test_owner_exit_wakes_the_wait_without_polling(self):
+ real_alive = self.repl_module._owner_alive_posix
+
+ def owner_exits_once_watched(*args):
+ alive = real_alive(*args) # runs after the exit registration, before the wait
+ self.owner.kill()
+ return alive
+
+ waiter = threading.Thread(
+ target=self.repl_module._wait_owner_posix, args=(self.owner.pid, os.getppid()), daemon=True
+ )
+ with (
+ mock.patch.object(self.repl_module, "_owner_alive_posix", owner_exits_once_watched),
+ mock.patch.object(self.repl_module.time, "sleep") as sleep,
+ ):
+ waiter.start()
+ waiter.join(timeout=10)
+ self.assertFalse(waiter.is_alive(), "the owner's exit must wake the watchdog")
+ sleep.assert_not_called()
+
+ def test_owner_wait_polls_when_exit_registration_fails(self):
+ with (
+ mock.patch.object(self.repl_module.select, "kqueue", create=True, side_effect=OSError("unavailable")),
+ mock.patch.object(self.repl_module.os, "pidfd_open", create=True, side_effect=OSError("unavailable")),
+ mock.patch.object(
+ self.repl_module.time, "sleep", side_effect=lambda _: (self.owner.kill(), self.owner.wait())
+ ) as sleep,
+ ):
+ self.repl_module._wait_owner_posix(self.owner.pid, os.getppid())
+ sleep.assert_called_once()
+
+
if __name__ == "__main__":
unittest.main()