Skip to content

fix(python-guest): persist module globals across run() calls#93

Open
simongdavies wants to merge 1 commit into
hyperlight-dev:mainfrom
simongdavies:fix/python-guest-persist-globals
Open

fix(python-guest): persist module globals across run() calls#93
simongdavies wants to merge 1 commit into
hyperlight-dev:mainfrom
simongdavies:fix/python-guest-persist-globals

Conversation

@simongdavies
Copy link
Copy Markdown
Member

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.

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.
@simongdavies simongdavies added the bug Something isn't working label 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think we need to document what the "previous" implementation would have done

.to_string()
}

/// A `def` at module top level in `run()` #1 must be callable in `run()` #2.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the #2 here?

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in Executor.__init__ and reusing it on every run().
  • Add integration tests validating (1) top-level def reuse, (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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants