Skip to content

fix: use shared memory for virtual files when running with app isolation #9181

Open
akshayka wants to merge 2 commits intomainfrom
aka/isolate-apps-bug-fixes
Open

fix: use shared memory for virtual files when running with app isolation #9181
akshayka wants to merge 2 commits intomainfrom
aka/isolate-apps-bug-fixes

Conversation

@akshayka
Copy link
Copy Markdown
Contributor

@akshayka akshayka commented Apr 14, 2026

This change updates isolated apps to use shared memory for virtual
file storage.

Isolated apps run in different processes from the main server. This
means they need to place virtual file data in shared memory, but
historically run-mode has run in-process and used in-memory
storage.

The change looks large because we're lifting the choice of storage
out of the kernel context, letting the caller decide what storage
backend to use. This required touching many files. In practice the change is small.

This change updates isolated apps to use shared memory for virtual
file storage.

Isolated apps run in different processes from the main server. This
means they need to place virtual file data in shared memory, but
historically run-mode has used run in-process and used in-memory
storage.

The change looks large because we're lifting the choice of storage
out of the kernel context, letting the caller decide what storage
backend to use. This required touching many files.
Copilot AI review requested due to automatic review settings April 14, 2026 00:50
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Apr 14, 2026 0:51am

Request Review

Comment on lines -161 to 170
# Use shared memory in edit mode,
# in-memory storage in run mode (same process)
storage = (
# Storage is chosen explicitly by the caller. None means virtual files
# are not supported; we still construct an (inert) InMemoryStorage so
# the registry has a backend, but ctx.virtual_files_supported is False.
storage: VirtualFileStorage = (
SharedMemoryStorage()
if mode == SessionMode.EDIT
if virtual_file_storage == "shared_memory"
else InMemoryStorage()
)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the main change in this PR.

@akshayka akshayka added the bug Something isn't working label Apr 14, 2026
@akshayka akshayka requested a review from dmadisetti April 14, 2026 00:52
Copy link
Copy Markdown
Contributor

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 updates marimo’s virtual file subsystem to work correctly when apps are run in isolated subprocesses by making the virtual-file storage backend an explicit choice (shared memory vs in-process memory vs disabled).

Changes:

  • Replace the boolean virtual_files_supported flag with virtual_file_storage: "in_memory" | "shared_memory" | None and thread it through session/kernel/app-host/runtime creation paths.
  • Configure AppHost (isolated run-mode) kernels to use shared-memory virtual-file storage so the parent server can serve /@file/... requests.
  • Add a process-isolation smoke test for virtual files and update docs/tests to use the new parameter.

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
tests/conftest.py Updates kernel/test config to pass virtual_file_storage based on session mode.
tests/_session/app_host/test_main.py Switches app-host tests to pass virtual_file_storage="shared_memory".
tests/_session/app_host/test_app_host.py Switches app-host tests to pass virtual_file_storage="shared_memory".
tests/_server/test_sessions.py Updates session/kernel-manager tests to new virtual_file_storage arg.
tests/_runtime/test_runtime.py Updates runtime tests to use virtual_file_storage.
marimo/_smoke_tests/process_isolation/virtual_files.py Adds a smoke-test notebook for virtual files under process isolation.
marimo/_smoke_tests/process_isolation/README.md Documents how to run the process-isolation virtual-file smoke test.
marimo/_session/session.py Threads virtual_file_storage into the “original kernel” session creation path.
marimo/_session/managers/kernel.py KernelManager now stores and passes virtual_file_storage into runtime.launch_kernel.
marimo/_session/managers/ipc.py Removes virtual_files_supported from IPC manager args (IPC kernels always disable virtual files).
marimo/_session/managers/app_host.py Forces AppHost kernels to use virtual_file_storage="shared_memory".
marimo/_session/app_host/main.py App-host kernel launch now forwards virtual_file_storage.
marimo/_session/app_host/host.py AppHost create_kernel now accepts virtual_file_storage.
marimo/_session/app_host/commands.py Updates CreateKernelCmd to include virtual_file_storage.
marimo/_server/session_manager.py Chooses default virtual_file_storage based on session mode (edit vs run).
marimo/_server/export/init.py Export path disables virtual files via virtual_file_storage=None.
marimo/_runtime/virtual_file/storage.py Introduces VirtualFileStorageType and keeps storage backends.
marimo/_runtime/virtual_file/init.py Re-exports VirtualFileStorageType.
marimo/_runtime/runtime.py Updates launch_kernel signature to accept virtual_file_storage.
marimo/_runtime/context/kernel_context.py Selects virtual-file backend based on virtual_file_storage and sets virtual_files_supported.
marimo/_runtime/app/kernel_runner.py Uses shared-memory storage for embedded/edit-like runner context.
marimo/_pyodide/pyodide_session.py Disables virtual files via virtual_file_storage=None.
marimo/_ipc/types.py Removes virtual_files_supported from KernelArgs.
marimo/_ipc/launch_kernel.py IPC kernel launch disables virtual files via virtual_file_storage=None.

Comment on lines 1095 to 1099
config_manager=config_manager,
virtual_files_supported=True,
virtual_file_storage="shared_memory",
redirect_console_to_browser=False,
ttl_seconds=None,
auto_instantiate=auto_instantiate,
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

create_session() hardcodes virtual_file_storage="shared_memory" for both EDIT and RUN sessions. That means the RUN-mode branches in this test are not exercising the default in-process storage behavior. Consider selecting "in_memory" when mode == SessionMode.RUN (and "shared_memory" for EDIT) so the helper reflects real configuration.

Copilot uses AI. Check for mistakes.
Comment on lines +163 to 170
# Storage is chosen explicitly by the caller. None means virtual files
# are not supported; we still construct an (inert) InMemoryStorage so
# the registry has a backend, but ctx.virtual_files_supported is False.
storage: VirtualFileStorage = (
SharedMemoryStorage()
if mode == SessionMode.EDIT
if virtual_file_storage == "shared_memory"
else InMemoryStorage()
)
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

create_kernel_context treats any non-"shared_memory" value (including typos) as in-memory storage, while still marking virtual_files_supported as true for any non-None value. This can silently mask misconfiguration and lead to hard-to-debug cross-process 404s. Consider validating virtual_file_storage explicitly (e.g., handle "in_memory" and "shared_memory" and raise on unknown values) before selecting the backend.

Copilot uses AI. Check for mistakes.
app_metadata=app_metadata,
config_manager=get_default_config_manager(current_path=None),
virtual_files_supported=True,
virtual_file_storage="shared_memory",
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

This RUN-mode session test passes virtual_file_storage="shared_memory", but RUN mode kernels are in-process and should normally use "in_memory". Keeping the test aligned with the real configuration makes it more likely to catch accidental behavior changes and avoids unnecessary shared-memory usage.

Suggested change
virtual_file_storage="shared_memory",
virtual_file_storage="in_memory",

Copilot uses AI. Check for mistakes.
Comment on lines +361 to 362
virtual_file_storage="shared_memory",
redirect_console_to_browser=False,
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

This RUN-mode session test passes virtual_file_storage="shared_memory", but RUN mode kernels are in-process and should normally use "in_memory". Using shared memory here can reduce the test’s fidelity to production behavior.

Copilot uses AI. Check for mistakes.
app_metadata=app_metadata,
config_manager=get_default_config_manager(current_path=None),
virtual_files_supported=True,
virtual_file_storage="shared_memory",
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

This RUN-mode session test passes virtual_file_storage="shared_memory", but RUN mode kernels are in-process and should normally use "in_memory". Consider updating to in-memory storage to better reflect real run-mode configuration.

Suggested change
virtual_file_storage="shared_memory",
virtual_file_storage="in_memory",

Copilot uses AI. Check for mistakes.
marimo/_smoke_tests/process_isolation/app1.py
```

Multi-file `marimo run` auto-enables process isolation. If all three
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

This text says “all three sections below” but the notebook only has two sections (image + table). Update the wording or add the missing third section so the smoke test instructions are consistent.

Suggested change
Multi-file `marimo run` auto-enables process isolation. If all three
Multi-file `marimo run` auto-enables process isolation. If both

Copilot uses AI. Check for mistakes.
app_metadata=app_metadata,
config_manager=get_default_config_manager(current_path=None),
virtual_files_supported=True,
virtual_file_storage="shared_memory",
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

This test configures a RUN-mode (threaded) kernel with virtual_file_storage="shared_memory". Production RUN mode defaults to in-process storage, so using shared memory here can hide regressions and adds an unnecessary platform dependency. Consider switching this RUN-mode test to virtual_file_storage="in_memory" to match the intended behavior.

Suggested change
virtual_file_storage="shared_memory",
virtual_file_storage="in_memory",

Copilot uses AI. Check for mistakes.
Comment on lines +15 to +22
This notebook creates virtual files (HTML, image, Arrow) to verify they
are served correctly when the app runs in an isolated subprocess
(`isolate_apps=true`).

**Bug:** When `isolate_apps` is enabled, the kernel runs in a child
process that stores virtual files in process-local `InMemoryStorage`.
The server process cannot access them, causing `FileNotFoundError`
when the browser requests `/@file/...`.
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The intro markdown says this notebook creates virtual files for “HTML, image, Arrow” and later refers to “empty iframes”, but the cells below only create an image and a table (Arrow). Either add an HTML-based output that actually exercises the HTML virtual-file path, or update the text so the smoke test description matches what it covers.

Copilot uses AI. Check for mistakes.
```

Multi-file `marimo run` auto-enables process isolation. You should see a
blue square image, a green HTML heading, and a three-row table. If any of
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The expected-results text mentions “a green HTML heading”, but the referenced notebooks (app1 + virtual_files) appear to render markdown/callouts, not an HTML virtual file. Either adjust the expectation (e.g., “green PASS callout heading”) or add an explicit HTML virtual-file output to the smoke test so this instruction is accurate.

Suggested change
blue square image, a green HTML heading, and a three-row table. If any of
blue square image, a green PASS callout heading, and a three-row table. If any of

Copilot uses AI. Check for mistakes.
app_file_manager=app_file_manager,
config_manager=get_default_config_manager(current_path=None),
virtual_files_supported=True,
virtual_file_storage="shared_memory",
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

This session is created in RUN mode but is configured with virtual_file_storage="shared_memory". Since RUN mode kernels are in-process by default, using "in_memory" here would better match production behavior and avoid relying on shared-memory support when it isn’t needed.

Suggested change
virtual_file_storage="shared_memory",
virtual_file_storage="in_memory",

Copilot uses AI. Check for mistakes.
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.

2 participants