Skip to content

fix(plugin): hand a Python plugin its inputs on stdin, never on disk - #2587

Merged
robcohen merged 3 commits into
mainfrom
fix/plugin-workdir-sigint
Oct 11, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/plugin-workdir-sigint

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

A Python plugin's inputs (entries, options, config, the compat layer and the plugin's source) now reach the CPython-WASI guest on its stdin, from memory, instead of as files in a temp directory preopened as /work. The script runs as python -c; the guest's only preopen is /lib (the standard library, read-only).

stdin carries four length-prefixed sections (compat layer, plugin source, options JSON, invocation JSON), then the entries as JSON lines to EOF, which load_entries still reads one line at a time.

Why

rledger exits at once on Ctrl-C (SIGINT), so the work directory, which held a serialized copy of the ledger, stayed in $TMPDIR. The issue preferred removing the on-disk copy over racing a signal handler, and wasmtime-wasi's MemoryInputPipe supports that directly. Nothing is written to disk now, so there is nothing to clean up.

Tracebacks still show the plugin's source lines: the sources are registered in linecache under the plugin's own name (e.g. File "bad.py", line 1), where they used to read /work/plugin.py.

How tested

  • New conformance case python_plugin_leaves_nothing_on_disk_after_sigint (Linux): starts rledger check on a ledger whose Python plugin sleeps, waits for the guest thread (rledger-python-plugin in /proc/<pid>/task), sends SIGINT, asserts rledger died of the signal and $TMPDIR is empty. Fails without the fix (left in $TMPDIR after Ctrl-C: [".../.tmpCbekx0"]), passes with it. It replaces python_plugin_work_dir_is_private, which tested the 0700 mode of the directory that no longer exists.
  • python_plugin_isolation now expects listable=['/lib'] (it listed /work too); the open-files cap case opens /lib/os.py.
  • cargo test -p rustledger-plugin --all-features, cargo test -p rustledger --all-features with RLEDGER_REQUIRE_PYTHON_WASI=1 RLEDGER_REQUIRE_WASM32=1: all pass. python_plugins_back_to_back_after_a_timeout (a 2-second budget) failed once locally while four other builds loaded the machine, and passed in an isolated run. It failed the same way on an unrelated branch under the same load.
  • 20k-transaction passthrough plugin timed before and after: same speed within noise (9-11 s debug, machine loaded), peak RSS 150 MB down to 135 MB.
  • clippy (workspace, all features, -D warnings) and fmt clean. The release-config clippy (--no-default-features --features mimalloc,ag-rledger --all-targets) fails on wasm_plugins_dir being unused in cli_commands_test.rs. That is on main already, not from this PR, and the docs: release binaries do not include plugin runtimes #2541 PR fixes it.

Closes #2551

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

A Python plugin read its inputs (the entries, options and config) from a
temp directory preopened as /work, removed when the plugin ended. A
Ctrl-C stops rledger at once, so the directory, holding a serialized copy
of the ledger, stayed in $TMPDIR. The inputs now go to the guest on its
stdin from memory (length-prefixed sections, then the entries line by
line), the script runs as `python -c`, and the guest sees only /lib.
There is nothing on disk to clean up.

Tracebacks still show the plugin's source lines (registered in
linecache) under the plugin's own name instead of /work/plugin.py.

Closes #2551

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 46.2ms ± 2.4ms 827.9ms ± 19.9ms 17.9x 51.1 MB / 49.9 MB
investment 72.9ms ± 1.3ms 3073.7ms ± 10.3ms 42.2x 57.1 MB / 58.2 MB
fifo 72.1ms ± 1.6ms 15645.3ms ± 166.6ms 217.0x 57.1 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.9ms ± 0.5ms 46.2ms ± 2.4ms +3.0% ✅ within noise 51.2 MB → 51.1 MB (-0.2%)
investment 73.7ms ± 3.1ms 72.9ms ± 1.3ms -1.1% ✅ within noise 57.1 MB → 57.1 MB (+0.1%)
fifo 70.7ms ± 0.8ms 72.1ms ± 1.6ms +2.0% ✅ within noise 57.1 MB → 57.1 MB (-0.0%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 1.1ms, investment 1.5ms, fifo 0.8ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 819
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

The entries arrive on the guest's stdin since the previous commit, so a
plugin reading sys.stdin at import (an input() left in) took them and
the plugin ran on none, which would delete the ledger's entries. The
runner now holds the entries stream privately and sys.stdin is empty,
as it was before.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen

Copy link
Copy Markdown
Member Author

Review loop

Round Scope Finding Fix
1 The whole diff, read adversarially: the stdin protocol, the bootstrap script, the tests A plugin reading sys.stdin at import (an input() left in) took the entries stream, so the plugin ran on no entries, and its returned empty list would replace the ledger's 820078e: the runner keeps the entries stream to itself and sys.stdin is empty, as it was before. New python_plugin_stdin_reads_nothing, which fails without the fix (took 1627 saw 0)
2 The round-1 change, plus areas not yet re-read: traceback naming via linecache, the output cap now sized from the stdin bytes, the -c argument, timing (20k-transaction passthrough) none n/a

The loop converged at round 2. The required checks (build, Python-gated cargo tests) are green on 820078e.

@robcohen
robcohen merged commit 4fc4724 into main Oct 11, 2026
54 of 55 checks passed
@robcohen
robcohen deleted the fix/plugin-workdir-sigint branch October 11, 2026 07:22
robcohen added a commit that referenced this pull request Oct 11, 2026
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.

plugin: a Python plugin's work directory survives Ctrl-C

1 participant