fix(code-runtime-python): catch the model exception with a pre-program local exception class

The _run outer try/except used the module-global BaseException, which the
program (running as __main__) can rebind: __main__.BaseException = RuntimeError
made the except resolve to RuntimeError, so a subsequent ValueError escaped _run
with no done frame and misreported the run as worker-exit. Bind BaseException
into a _run local before the program runs so the catch is immune; a regression
test rebinds BaseException and raises, asserting an exception, not a worker-exit.

Also correct the NUL-escape comment text: the JSON escape-result side is \^@ (6
bytes, the valid JSON NUL escape), not \x00, so the 6x-budget arithmetic in the
comments is self-consistent. Register the BaseException-rebind case in the
settlement note Testing (en + zh) and re-record the pairing.
This commit is contained in:
Chinesezjc
2026-08-31 14:34:09 +08:00
committed by Tianyi Cui
parent 202c428137
commit 0102cd95bf
5 changed files with 41 additions and 9 deletions
@@ -998,6 +998,15 @@ async def _run(channel: ProtocolChannel) -> None:
_os_write_local = _os_write
_memoryview_local = _memoryview
_fallback_frame_local = _FALLBACK_DONE_FRAME
# The exception class the outer try/except catches is bound into a LOCAL
# here, before the program runs. This bootstrap is `__main__`, so
# `__main__.BaseException = RuntimeError` would otherwise rebind the module
# global `BaseException` the `except BaseException` clause resolves at
# runtime, and a subsequent `ValueError` would then not match the clause —
# escaping `_run` with no `done` frame and misreporting the run as a
# `worker-exit`. Binding the class into a local makes the catch immune to a
# one-line rebind.
_BaseException = BaseException
def send_done(payload: dict[str, Any] | str) -> None:
try:
@@ -1061,7 +1070,7 @@ async def _run(channel: ProtocolChannel) -> None:
flush_out()
flush_err()
done = _done_with_value(value, max_value_bytes)
except BaseException as exc: # noqa: BLE001 -- report every failure to host
except _BaseException as exc: # noqa: BLE001 -- report every failure to host; `_BaseException` is a pre-program local, not a rebindable module global
done = {
"type": "done",
"error": {