Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(scheduling): release the kernel loop handle only when stop owns it

shadowinlife03e75956ea68
diff --git a/agent/src/live/runtime/scheduler.py b/agent/src/live/runtime/scheduler.py
index a9eddec01..2d1ba44bb 100644
--- a/agent/src/live/runtime/scheduler.py
+++ b/agent/src/live/runtime/scheduler.py
@@ -229,7 +229,6 @@ class Scheduler:
"""
self._on_fire = on_fire
self._max_recheck_ms = max_recheck_ms
- self._now_fn = now_fn
self._jobs: dict[str, Job] = {}
self._kernel = SchedulerKernel(
now_fn=now_fn,
diff --git a/agent/src/scheduling/kernel.py b/agent/src/scheduling/kernel.py
index 661c1901b..ee7bd29fc 100644
--- a/agent/src/scheduling/kernel.py
+++ b/agent/src/scheduling/kernel.py
@@ -30,8 +30,9 @@ arguments whose defaults mirror ``AgentTuningConfig`` in
inject configured values.
Single-writer invariant: one kernel instance is owned by exactly one
-composition root (the FastAPI lifespan for the scheduled-research executor).
-The kernel does not coordinate multiple owners and must not be started from
+composition root — today the FastAPI lifespan owns the scheduled-research
+executor's kernel, and the live runtime owns one kernel per runner start —
+so the kernel never coordinates multiple owners and must not be started from
two loops.
"""
@@ -202,7 +203,14 @@ class SchedulerKernel:
Idempotent. When disabled, this is a no-op. Must be called from the
event loop that will own the task.
"""
- if not self._enabled or self.is_running:
+ if not self._enabled:
+ return
+ if self.is_running:
+ if self._stopping:
+ # A stop() is in flight and the old loop task has not been reaped
+ # yet: refuse rather than risk racing the teardown. Logged so a
+ # refused restart is visible instead of a silent no-op.
+ logger.warning("scheduler kernel start refused: a stop is in flight")
return
self._stopping = False
self._wakeup = asyncio.Event()
@@ -216,7 +224,8 @@ class SchedulerKernel:
even when a tick is wedged: the wakeup set below releases a sleeping
loop in the common case, and ``task.cancel()`` is the fallback so
shutdown never blocks for a full tick (or forever) if the wakeup
- raced the loop's sleep or the tick itself is stuck.
+ raced the loop's sleep or the tick itself is stuck. The loop-task
+ handle is released only if this stop() still owns it.
"""
if not self._enabled:
return
@@ -231,7 +240,13 @@ class SchedulerKernel:
await task
except asyncio.CancelledError:
pass
- self._task = None
+ # Release the handle only if this stop() still owns it. A concurrent
+ # start() can legitimately land between the old loop task finishing and
+ # this line (once the task is done, is_running is False) and install a
+ # NEW loop task; clearing it here would orphan a running loop that
+ # reports itself stopped and that no later stop() can reach.
+ if self._task is task:
+ self._task = None
# -- loop internals -------------------------------------------------------
diff --git a/agent/tests/scheduling/test_kernel_sleep_hint.py b/agent/tests/scheduling/test_kernel_sleep_hint.py
index 2ebe36d1c..ad1f6ed9a 100644
--- a/agent/tests/scheduling/test_kernel_sleep_hint.py
+++ b/agent/tests/scheduling/test_kernel_sleep_hint.py
@@ -14,12 +14,23 @@ from __future__ import annotations
import asyncio
import itertools
+import logging
import pytest
from src.scheduling.kernel import SchedulerKernel
+async def _noop_tick(now_ms: int) -> None: # pragma: no cover - does nothing
+ pass
+
+
+def _tasks_named(name: str) -> list[asyncio.Task]:
+ """Return the not-yet-finished tasks with ``name``, excluding this one."""
+ current = asyncio.current_task()
+ return [t for t in asyncio.all_tasks() if t.get_name() == name and t is not current]
+
+
def test_sleep_hint_replaces_the_fixed_tick() -> None:
"""A 60s fixed tick would never reach the second tick inside the timeout;
the hint shortens the inter-tick sleep to 1ms."""
@@ -178,3 +189,95 @@ def test_wakeup_still_shortens_a_hinted_sleep() -> None:
asyncio.run(scenario())
assert len(ticks) >= 2
+
+
+def test_concurrent_start_while_stopping_does_not_orphan_the_loop() -> None:
+ """Regression: the old unconditional ``self._task = None`` at the tail of
+ stop() nulled a NEW loop task installed by a start() that landed in the
+ microstep where the old task was already done but stop() had not resumed.
+ The kernel must release the handle only if this stop() still owns it."""
+
+ async def scenario() -> None:
+ kernel = SchedulerKernel(
+ tick_interval_ms=60_000,
+ now_fn=lambda: 1000,
+ on_tick=_noop_tick,
+ task_name="t-orphan",
+ sleep_hint_fn=lambda now_ms: 60_000,
+ )
+ kernel.start()
+ first = kernel.task
+ assert first is not None
+ # Registered BEFORE stop() awaits the task, so this callback runs in
+ # the same completion batch but ahead of stop()'s own resume callback:
+ # at that point first is done (is_running is False) and start()
+ # legitimately installs a NEW loop task.
+ first.add_done_callback(lambda _t: kernel.start())
+ try:
+ await asyncio.wait_for(kernel.stop(), timeout=5.0)
+
+ live = _tasks_named("t-orphan")
+ assert len(live) == 1, "the restarted loop must still be alive"
+ assert kernel.task is not None
+ assert kernel.task is not first
+ assert not kernel.task.done()
+ assert kernel.is_running is True
+
+ # And the public stop() must be able to reap the replacement.
+ await asyncio.wait_for(kernel.stop(), timeout=5.0)
+ assert _tasks_named("t-orphan") == []
+ assert kernel.task is None
+ finally:
+ for t in _tasks_named("t-orphan"):
+ t.cancel()
+
+ asyncio.run(scenario())
+
+
+def test_start_refused_while_stop_in_flight_logs_and_does_not_spawn_second_task() -> None:
+ """While stop() is in flight and its loop task has not been reaped yet
+ (is_running still True), start() must refuse: no second loop task, and a
+ visible refusal log instead of a silent no-op."""
+ records: list[logging.LogRecord] = []
+
+ class _Capture(logging.Handler):
+ def emit(self, record: logging.LogRecord) -> None:
+ records.append(record)
+
+ async def scenario() -> None:
+ kernel = SchedulerKernel(
+ tick_interval_ms=60_000,
+ now_fn=lambda: 1000,
+ on_tick=_noop_tick,
+ task_name="t-refuse",
+ sleep_hint_fn=lambda now_ms: 60_000,
+ )
+ kernel.start()
+ first = kernel.task
+ assert first is not None
+ stopper = asyncio.create_task(kernel.stop())
+ await asyncio.sleep(0) # let stop() set _stopping and cancel the loop
+ assert kernel._stopping is True
+
+ kernel.start() # refused: the old task is still running (not reaped)
+ assert len(_tasks_named("t-refuse")) == 1 # no second loop spawned
+
+ await asyncio.wait_for(stopper, timeout=5.0)
+ assert kernel.task is None
+ assert kernel.is_running is False
+
+ handler = _Capture()
+ kernel_logger = logging.getLogger("src.scheduling.kernel")
+ old_propagate = kernel_logger.propagate
+ kernel_logger.propagate = False
+ kernel_logger.addHandler(handler)
+ try:
+ asyncio.run(scenario())
+ finally:
+ kernel_logger.removeHandler(handler)
+ kernel_logger.propagate = old_propagate
+
+ assert any(
+ record.levelno == logging.WARNING and "start refused" in record.getMessage()
+ for record in records
+ )
diff --git a/agent/tests/test_runtime_scheduler_kernel.py b/agent/tests/test_runtime_scheduler_kernel.py
index 045a19df7..0e22b1546 100644
--- a/agent/tests/test_runtime_scheduler_kernel.py
+++ b/agent/tests/test_runtime_scheduler_kernel.py
@@ -17,6 +17,8 @@ from __future__ import annotations
import asyncio
+import pytest
+
from src.live.runtime.scheduler import (
DEFAULT_MAX_RECHECK_MS,
TASK_NAME,
@@ -181,3 +183,12 @@ def test_default_max_recheck_is_unchanged() -> None:
assert sched._max_recheck_ms == DEFAULT_MAX_RECHECK_MS == 5 * 60 * 1000
# No jobs: the hinted sleep is the idle-poll cap.
assert sched._sleep_hint(0) == DEFAULT_MAX_RECHECK_MS
+
+
+def test_task_property_on_the_facade_is_read_only() -> None:
+ """``_task`` was a writable attribute before the kernel adoption; as a
+ property it must reject assignment so owners cannot bypass the kernel's
+ guarded handle release."""
+ sched = Scheduler(_noop_fire)
+ with pytest.raises(AttributeError):
+ sched._task = None