Skip to content

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

Open
simongdavies wants to merge 3 commits 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 3 commits 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.

@simongdavies simongdavies added the bug Something isn't working label May 19, 2026
Comment thread src/wasm_sandbox/tests/python_state_persistence.rs Outdated
Comment thread src/wasm_sandbox/tests/python_state_persistence.rs Outdated
jsturtevant
jsturtevant previously approved these changes May 19, 2026
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.

@jsturtevant jsturtevant force-pushed the fix/python-guest-persist-globals branch 2 times, most recently from 44b4bbf to 4e04c81 Compare May 29, 2026 20:09
simongdavies and others added 3 commits May 29, 2026 13:14
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.

Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
@jsturtevant jsturtevant force-pushed the fix/python-guest-persist-globals branch from 4e04c81 to a4008d4 Compare May 29, 2026 20:14
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