Skip to content

Python plugins don't run: PYTHON_FUEL too small for CPython startup, and entry points ignore __plugins__ #2500

Closed

Description

@robcohen

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__, calls getattr(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)

  1. Size the budget from a measurement. Measure CPython startup plus a small plugin in fuel. Then size PYTHON_FUEL with headroom, or better, express it as seconds × sandbox::FUEL_PER_SECOND with 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.
  2. Resolve entry points the way beancount does. Use __plugins__: string names via getattr, 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.
  3. 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 the python-gated-cargo-tests job so it cannot silently skip.

Found while reviewing #2496. Refs #2449, #2485.

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions