Skip to content

Show exceptions from threads in the python cell that started them - #1536

Merged
AnthonyRonning merged 1 commit into
masterfrom
agent/python-thread-errors
Nov 10, 2026
Merged

AnthonyRonning merged 1 commit into
masterfrom
agent/python-thread-errors

Conversation

@AnthonyRonning

@AnthonyRonning AnthonyRonning commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

A cell that starts threads now reports their exceptions instead of looking like a clean success.

Why

In real use, a cell ran eight subinterpreters through call_in_thread. Every thread raised, yet the cell returned completed and printed a bogus speedup. The tracebacks appeared only as "Background stderr (unattributed native/subprocess output)".

The cause: in CPython 3.14, threads do not inherit a cell's context by default, so the worker could not tell which cell their output belonged to.

What changed

  • Launch flag: the worker starts with -X thread_inherit_context=1, so threads a cell starts carry its execution id, and their output lands in that cell's stdout and stderr.
  • Thread exception hook: threading.excepthook runs after the thread leaves its context, so the worker wraps it and reads the cell's id from the thread. The original hook still prints the traceback, now attributed to the cell. Uncaught thread exceptions are counted while that cell runs, and the first one's type and message are kept. SystemExit is ignored, as the default hook ignores it.
  • Result: two defaulted fields in the done frame, and the status line the model sees says what happened:
    completed (132 ms); 8 exceptions in threads started by this cell (first: NotShareableError: func not shareable); tracebacks in stderr
    A cell without thread exceptions renders exactly as before, and a completed cell is still not an error.

Caveats

  • Thread pools: a thread pool created in one cell keeps that cell's id, so work a later cell gives it shows as the earlier cell's late output. asyncio.to_thread copies the caller's context, so it is unaffected. Exceptions from pool work go to futures, never to these counts.
  • Unjoined threads: only exceptions raised before the cell finishes are counted. The rest arrive as late output, now labelled with the right cell.
  • Private attribute: the hook reads Thread._context, guarded with getattr. The host requires exactly CPython 3.14.7 at startup.

Validation

  • Worker suite: a test checks the count, the summary, that everything is attributed to the cell and nothing is background, that SystemExit is not counted, and that a late thread exception is neither counted in the next cell nor left unlabelled. It fails without the change, without the hook, and without the flag.
  • Tool test: checks the plural line, the singular line, and an unchanged line for threads that only print. It also fails when only the production launch flag is removed.
  • Protocol test: a done frame without the new fields still decodes, and an oversized summary is rejected.
  • Pre-commit hook: just ci-lint and just ci-test pass, with 2,844 workspace tests and 26 worker tests.

🤖 Generated with Claude Code


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

A cell whose threads all raised still read "completed": threads did not
inherit the cell's context, and threading runs its excepthook outside the
thread's context, so the tracebacks arrived as unattributed background
output.

Launch the worker with -X thread_inherit_context=1, and wrap
threading.excepthook to print each traceback as output of the cell that
started the thread and to count it while that cell runs. The done frame
gains defaulted thread_exceptions and first_thread_exception fields, and
the python tool's status line reports them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@AnthonyRonning
AnthonyRonning enabled auto-merge (squash) November 10, 2026 05:45
@AnthonyRonning
AnthonyRonning merged commit 3a02899 into master Nov 10, 2026
23 checks passed
@AnthonyRonning
AnthonyRonning deleted the agent/python-thread-errors branch November 10, 2026 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant