Skip to content

docs(async): record the to_thread worker-exception seam (#400) - #1382

Merged
espetro merged 1 commit into
masterfrom
work/400-to-thread-frame
Nov 24, 2026
Merged

espetro merged 1 commit into
masterfrom
work/400-to-thread-frame

Conversation

@espetro

@espetro espetro commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Closes #400

Summary

The implementation for this issue landed upstream in #1352 (feat(async): lower asyncio.to_thread as a synthesized coroutine frame, commit c0e13d59), which is already on master. This PR closes the remaining gap: the user-facing divergence entry and the ledger row for the seam the frame lowering leaves open.

asyncio.to_thread(...) lowers to a synthesized coroutine frame (coro.ps_to_thread_<digest>), the same shape async def lowers to, so create_task, gather and wait_for compose with it through the existing task machinery. The value path is byte-identical to CPython 3.14 on the host. The worker record (PsThreadCall) carries the result value and a done flag and no exception slot, so an exception raised inside the worker does not reach the awaiter:

  • CPython: the exception is handed to the awaiting task, the except clause runs, the program continues (exit 0).
  • pyscriptc: the exception ends the process where the worker ran, the exception name lands on stderr, exit 1.

The numbered divergences list claims that anything unlisted is identical to CPython, so this seam needs an entry. Divergence 100 records it, with a probe that pins both sides' bytes.

The ledger generator drift

scripts/gen_divergence_ledger.py had drifted from the numbered list: it carried a duplicate D93 (the compare-dispatch row and the subprocess row) and a duplicate D94 (the hash row and the argparse row), and no row for D97 at all. Regenerating it therefore dropped the D97 row and renumbered the compare and hash rows onto D93 and D94, so mise run divergence:ledger -- --check failed on master and the committed ledger was not reproducible.

The rows are fixed to their numbered entries (D98 for the compare dispatch, D99 for the hash rules, D93 for the subprocess handle, D94 for the argparse subparsers), the missing D97 row is added, and D97's status moves from the off-vocabulary differs to the generator's divergent.

Verification

  • tests/test_divergence_ledger.py: 77 passed (the ledger replays every row against the pinned CPython and the current compiler).
  • mise run divergence:ledger -- --check: exit 0, the committed ledger is reproducible.
  • uv run pytest tests/test_divergence_ledger.py tests/test_coro_asyncio_sync.py tests/test_probe_guard.py: 108 passed.
  • uv run ruff check and uv run ruff format --check: clean.
  • uv run pytest tests/ -n auto -m "not linux_container" --ignore=tests/compat2: 4215 passed, 4 failed. The 4 failures reproduce on a clean master checkout and are unrelated to this diff (tests/test_compat_site.py reads files the sparse checkout leaves out, and tests/test_stdlib_ndarray.py::test_demo_program_matches_cpython fails on the numpy value shape). mise run check also fails on tests/compat2/test_verdict_replay.py::test_macos_arm64_reference_is_a_separate_slot on a clean master checkout: the host runs CPython 3.14.10 and the baseline recording wants 3.14.9.

The #400 acceptance criteria were checked against the merged implementation on this branch: create_task(to_thread(...)), gather(to_thread(...)) and wait_for(to_thread(...)) compile and run, and their output matches the pinned CPython byte for byte; cancelling a task that wraps a to_thread call hands the awaiter a CancelledError while the thread runs to completion, matching CPython; the old create_task takes a coroutine, not another value fence is gone for the frame case, and the non-frame uses still fence precisely.

Owned files

docs/reference/divergences.md, docs/reference/divergence-ledger.json (generated, never hand-merged), scripts/gen_divergence_ledger.py, probes/divergences/d100_to_thread_worker_exception.py, .agents/docs/design/async-lowering-plan.md.

`asyncio.to_thread` lowers to a synthesized coroutine frame (issue #400,
merged in #1352), so `create_task`, `gather` and `wait_for` take it. The
value path is byte-identical to CPython 3.14, but the worker record
carries the result value and a done flag and no exception slot, so a
raise inside the worker does not reach the awaiter: CPython hands the
exception to the awaiting task and the `except` clause runs, while the
lowered program prints the exception on stderr and exits 1.

Divergence 100 records that seam, with a probe that pins both sides'
bytes, so the numbered list keeps its promise that anything unlisted is
identical.

The ledger generator also drifted from the numbered list: it carried a
duplicate D93 and D94 and no D97, so regenerating it dropped the D97
row and renumbered D98 and D99 onto D93 and D94. The rows are fixed to
their numbered entries, the missing D97 row is added, and D97's status
moves from the off-vocabulary "differs" to "divergent".
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying pyscriptc with  Cloudflare Pages  Cloudflare Pages

Latest commit: 73da59b
Status: ✅  Deploy successful!
Preview URL: https://9b3b24d4.pyscriptc.pages.dev
Branch Preview URL: https://work-400-to-thread-frame.pyscriptc.pages.dev

View logs

@espetro espetro added this to the v0.11.0 milestone Nov 24, 2026
@espetro
espetro merged commit da5c93a into master Nov 24, 2026
81 of 91 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in pyscriptc Nov 24, 2026
@espetro
espetro deleted the work/400-to-thread-frame branch November 24, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

async: lower asyncio.to_thread as a synthesized coroutine frame so create_task and gather accept it

1 participant