Repository navigation
Description
Summary
Python plugins do not run on main. Even a passthrough plugin fails, for two independent reasons, and no test exercises the CPython-on-WASI runtime end to end, so CI stays green.
# good.py
__plugins__ = ['good']
def good(entries, options_map):
return entries, []plugin "good.py"
rledger check (main, python-plugin-wasm, which is a default feature of source and nix builds):
error[E8002]: Python plugin execution failed: … wasm trap: all fuel consumed by WebAssembly
Release binaries are built without Python support and report E8005 … requires the python-plugins feature instead. So this affects source and nix users today, and it would ship broken in the next release that enables it.
Bug 1: PYTHON_FUEL is too small for CPython to start
crates/rustledger-plugin/src/python/runtime.rs sets const PYTHON_FUEL: u64 = 600_000_000. It was sized under the old "1M fuel ≈ 1 s" assumption. #2449 measured real wasm at 5–17G fuel/s, which makes 600M well under a second, and its doc now says the value "has not been re-measured".
CPython's startup runs out of that budget before the plugin is ever called. With PYTHON_FUEL raised 100× locally (to 60G) the fuel trap goes away and bug 2 appears.
#2449 and #2485 changed the WASM plugin and importer budget (FUEL_PER_SECOND, [plugins] max_time_secs). Both deliberately left the Python budget alone.
Bug 2: entry points are looked up by the literal name plugin
execute_module / execute_plugin build a script that calls run_plugin(plugin, …) (runtime.rs ~189 and ~264). The function is hard-coded as plugin, so any plugin following beancount's convention fails:
NameError: name 'plugin' is not defined
Beancount's loader (beancount/loader.py, run_transformations) imports the module and:
- skips it silently if it has no
__plugins__; - otherwise, for each item of
module.__plugins__, callsgetattr(module, name)when the item is a string, or the item itself when it is a callable, applying them in order to the entries.
Why CI did not catch it
The only Python tests (crates/rustledger-plugin/tests/python_compat_cost_round_trip.rs and others) run the compat layer under the host's python3, not through the CPython WASI runtime. No test runs a plugin through PythonRuntime end to end, so Python-gated cargo tests passes regardless.
Proposed fix (one PR)
- Size the budget from a measurement. Measure CPython startup plus a small plugin in fuel. Then size
PYTHON_FUELwith headroom, or better, express it as seconds ×sandbox::FUEL_PER_SECONDwith a Python-specific allowance, so the host's[plugins] max_time_secs/--plugin-max-time-secs(feat(plugin): let the host set the WASM plugin and importer time budget #2485) can raise it too. - Resolve entry points the way beancount does. Use
__plugins__: string names viagetattr, callables directly, in order. Match beancount on a module without__plugins__, or report it clearly if that is the more correct choice, and say which in the PR. A listed name the module doesn't define gets a clear error. - Add an end-to-end test that runs a
__plugins__-style plugin through the WASI runtime and checks its output. It is gated on the CPython wasm being available, and that gate is enforced in thepython-gated-cargo-testsjob so it cannot silently skip.
Found while reviewing #2496. Refs #2449, #2485.
🤖 Generated with Claude Code
Activity
fix(plugin): harden Python plugins and add a plugin conformance matrix
fix(plugin): bound Python plugins on the clock and make the memory ca…
fix(plugin): escape plugin text and keep Python plugin inputs private
fix(plugin): escape WASM module text in load errors and bidi controls
fix(plugin): escape the rest of the guest text that reaches output
test(plugin): cover the escaped hot-reload failure warning
fix(plugin): run Python plugins via __plugins__ under the host time b…