fix(python-guest): persist module globals across run() calls#93
Open
simongdavies wants to merge 1 commit into
Open
fix(python-guest): persist module globals across run() calls#93simongdavies wants to merge 1 commit into
simongdavies wants to merge 1 commit into
Conversation
The Executor previously rebuilt the globals dict on every call to run() via an inline 'exec(code, {...})' literal. That silently discarded every 'def', 'class', and top-level assignment between runs on the same sandbox instance, breaking three documented contracts:
* WasmSandbox's snapshot/restore is the mechanism for rewinding guest state - a bare back-to-back run() boundary was never specified as a state-wipe.
* The python_basics example explicitly sets 'counter = 100' and only expects it to disappear after restore(); the prior implementation made restore() a no-op because the counter would have been wiped by the very next run() anyway.
* The JS guest preserves globalThis across run() calls; the Python guest had no equivalent persistence path.
Fix: construct the globals dict once in Executor.__init__ and pass the instance attribute to every exec(). Snapshot/restore continues to rewind the namespace because it lives in the guest's Wasm linear memory.
Adds tests/python_state_persistence.rs covering: top-level def reuse, bare assignment reuse, and snapshot/restore rewind of the persistent namespace.
jsturtevant
reviewed
May 19, 2026
| //! * the `python_basics` example's "state was rolled back" narrative; | ||
| //! * the JavaScript guest, which preserves `globalThis` across runs. | ||
| //! | ||
| //! The tests below would have failed on the prior implementation; they |
Contributor
There was a problem hiding this comment.
nit: I don't think we need to document what the "previous" implementation would have done
jsturtevant
reviewed
May 19, 2026
| .to_string() | ||
| } | ||
|
|
||
| /// A `def` at module top level in `run()` #1 must be callable in `run()` #2. |
jsturtevant
approved these changes
May 19, 2026
There was a problem hiding this comment.
Pull request overview
This PR fixes the Python Wasm guest executor so that module-level globals persist across multiple run() calls on the same sandbox instance (aligning behavior with the documented snapshot/restore semantics and the existing examples). It also adds an integration test suite to prevent regressions.
Changes:
- Persist Python guest module globals by constructing the
exec()globals dict once inExecutor.__init__and reusing it on everyrun(). - Add integration tests validating (1) top-level
defreuse, (2) top-level assignment reuse, and (3) snapshot/restore rewinding of the persistent namespace.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/wasm_sandbox/guests/python/sandbox_executor.py |
Reuses a single self._globals dict across run() calls instead of recreating it each time. |
src/wasm_sandbox/tests/python_state_persistence.rs |
Adds integration coverage for Python state persistence across runs and correct snapshot/restore rollback behavior. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Executor previously rebuilt the globals dict on every call to run() via an inline 'exec(code, {...})' literal. That silently discarded every 'def', 'class', and top-level assignment between runs on the same sandbox instance, breaking three documented contracts:
Fix: construct the globals dict once in Executor.init and pass the instance attribute to every exec(). Snapshot/restore continues to rewind the namespace because it lives in the guest's Wasm linear memory.
Adds tests/python_state_persistence.rs covering: top-level def reuse, bare assignment reuse, and snapshot/restore rewind of the persistent namespace.