From e6fa7679a729b75fa8fb24707a426b112103bfab Mon Sep 17 00:00:00 2001 From: "nekomimi.pet" Date: Wed, 11 Nov 2026 01:31:44 -0500 Subject: [PATCH] fix(kernel): raise an interrupt that lands in a `del` at the cell's next line instead of losing it to NameError --- priv/python/albedo_cells.py | 24 ++++++++++++++++++++++++ robot-docs/kernel.md | 2 +- test/e2e/cell_limits_test.py | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/priv/python/albedo_cells.py b/priv/python/albedo_cells.py index 872042394..e8b5749a8 100644 --- a/priv/python/albedo_cells.py +++ b/priv/python/albedo_cells.py @@ -760,6 +760,25 @@ def native_output(fd: int) -> None: return +def interrupt_after(caller: FrameType) -> None: + """Raises KeyboardInterrupt in `caller` once the statement it runs has + finished: at its next line, or as it returns. A trace that fires once, + and then puts back whatever trace the cell had.""" + previous = sys.gettrace() + + def once(frame: FrameType, event: str, _arg: object) -> object: + if frame is not caller: + return None + if event in ("line", "return"): + caller.f_trace = None + sys.settrace(previous) + raise KeyboardInterrupt() + return once + + caller.f_trace = once + sys.settrace(lambda *_: None) + + def interrupt(_signal: int, _frame: FrameType | None) -> None: # A job's supervising task inherits the cell's context; the interrupt is # for the cell, which the queued cancellation reaches either way. @@ -779,6 +798,11 @@ def interrupt(_signal: int, _frame: FrameType | None) -> None: if task is not None and not task.cancelling(): cur = _frame while cur is not None: + if cur.f_code is Namespace.__delitem__.__code__ and cur.f_back: + # `del name` reports anything __delitem__ raises as a + # NameError and drops it: let the delete finish. + interrupt_after(cur.f_back) + return if cur.f_code is Namespace.__setitem__.__code__: ns = cur.f_locals.get("self") key = cur.f_locals.get("key") diff --git a/robot-docs/kernel.md b/robot-docs/kernel.md index 3d1727c95..489613ba2 100644 --- a/robot-docs/kernel.md +++ b/robot-docs/kernel.md @@ -497,7 +497,7 @@ activity traces use the task's context; native fd output goes to `native` when multiple cells run, since it cannot be attributed safely. A synchronous cell can return its tool result early, but other cells must wait until it yields or finishes. Background and deadline timers run outside the asyncio loop so a -synchronous cell cannot prevent its deadline from firing. A delayed SIGINT does not interrupt a cell whose code has already returned (the memory guard can sample a cell in its cleanup), nor one whose task is already cancelling; its cancellation cleanup reports jobs that continue running. Nor does it land in a job's own supervising tasks (`run.supervising`, marked by `albedo_proc.SUPERVISING`), which inherit the cell's context: the interrupt is the cell's, and its queued cancellation reaches the cell's await while the job keeps running. +synchronous cell cannot prevent its deadline from firing. A delayed SIGINT does not interrupt a cell whose code has already returned (the memory guard can sample a cell in its cleanup), nor one whose task is already cancelling; its cancellation cleanup reports jobs that continue running. Nor does it land in a job's own supervising tasks (`run.supervising`, marked by `albedo_proc.SUPERVISING`), which inherit the cell's context: the interrupt is the cell's, and its queued cancellation reaches the cell's await while the job keeps running. One that lands inside the namespace's `__delitem__` waits for the `del` to finish and is raised at the cell's next line (`interrupt_after`): CPython reports anything raised during `del name` as a NameError, so raising there would lose the interrupt and fail the cell instead. At most 16 cells run concurrently. Running captures are protected from eviction; completed output rolls out under the existing 16-capture limit. `await cells.cancel(id)` requests cancellation and diff --git a/test/e2e/cell_limits_test.py b/test/e2e/cell_limits_test.py index d0bb30576..841062551 100644 --- a/test/e2e/cell_limits_test.py +++ b/test/e2e/cell_limits_test.py @@ -175,6 +175,23 @@ await asyncio.to_thread(fan_out) # could interrupt the cell before any thread held it. Allocate only the cap # itself: the existing kernel footprint takes RSS over the cap, while leaving # room below the allocation limit for imports and the worker stack. +# Grows past the cap a MiB at a time while most of its time goes to a slice +# copy, which checks for no signal, so the interrupt lands in the `del` right +# after it. `del` runs the namespace's own __delitem__, and CPython reports +# anything raised there as a NameError. +DELETE_HOG = """ +held = [] +copy = bytearray(4 << 20) +source = bytes(4 << 20) +x = 0 +while True: + held.append(bytearray(1 << 20)) + for _ in range(20): + copy[:] = source + del x + x = 0 +""" + STUCK_THREAD = f""" import asyncio, mmap, os, threading, time print("pid", os.getpid()) @@ -265,6 +282,8 @@ class CellLimitsTests(unittest.TestCase): return python(ODD_SIZE) if "oddafter" in user: return python(ODD_AFTER) + if "namedrop" in user: + return python(DELETE_HOG) if "fanout" in user: return python(THREAD_HOG) if "stuckthread" in user: @@ -359,6 +378,11 @@ class CellLimitsTests(unittest.TestCase): if "stays above the cap" not in hog["output"]: self.assertEqual(before, after, out) + def test_a_cell_stopped_while_it_deletes_a_name_is_interrupted(self): + hog = self.cell("namedrop") + self.assertEqual(hog["status"], "interrupted", hog) + self.assertIn("kernel memory passed its cap", hog["output"]) + def test_a_cell_whose_threads_cannot_be_stopped_restarts_the_kernel(self): stuck = self.cell("stuckthread") self.assertEqual(stuck["status"], "interrupted", stuck) -- knotmirror 0.0.1