A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Fix virtual files on Windows (#285)
Akshay Agrawalda03aa3506a2
diff --git a/marimo/_runtime/virtual_file.py b/marimo/_runtime/virtual_file.pyindex e1df1973fd..d02ecd994b 100644--- a/marimo/_runtime/virtual_file.py+++ b/marimo/_runtime/virtual_file.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport dataclassesimport randomimport string+import sysimport threadingfrom multiprocessing import shared_memoryfrom typing import TYPE_CHECKING, Optional@@ -135,12 +136,21 @@ class VirtualFileRegistry:shm.buf[: len(buffer)] = buffer# we can safely close this shm, since we don't need to access its# buffer; we do need to keep it around so we can unlink it later- shm.close()+ if sys.platform != "win32":+ # don't call close() on Windows, due to a bug in the Windows+ # Python implementation. On Windows, close() actually unlinks+ # (destroys) the shared_memory:+ # https://stackoverflow.com/questions/63713241/segmentation-fault-using-python-shared-memory/63717188#63717188+ shm.close()+ # We hav to keep a reference to the shared memory to prevent it from+ # being destroyed on Windowsself.registry[key] = shmdef remove(self, virtual_file: VirtualFile) -> None:key = virtual_file.filenameif key in self.registry:+ if sys.platform == "win32":+ self.registry[key].close()# destroy the shared memoryself.registry[key].unlink()del self.registry[key]@@ -155,6 +165,8 @@ class VirtualFileRegistry:try:self.shutting_down = Truefor _, shm in self.registry.items():+ if sys.platform == "win32":+ shm.close()shm.unlink()self.registry.clear()finally:diff --git a/marimo/_server/api/virtual_file.py b/marimo/_server/api/virtual_file.pyindex d6eebc3423..91857fb051 100644--- a/marimo/_server/api/virtual_file.py+++ b/marimo/_server/api/virtual_file.py@@ -19,6 +19,7 @@ class VirtualFileHandler(tornado.web.RequestHandler):"""Handler for virtual files."""def get(self, filename_and_length: str) -> None:+ LOGGER.debug("Getting virtual file: %s", filename_and_length)if filename_and_length == EMPTY_VIRTUAL_FILE.filename:self.write(b"")return@@ -33,6 +34,9 @@ class VirtualFileHandler(tornado.web.RequestHandler):shm = shared_memory.SharedMemory(name=key)buffer_contents = bytes(shm.buf)[: int(byte_length)]except FileNotFoundError as err:+ LOGGER.debug(+ "Error retrieving shared memory for virtual file: %s", err+ )raise tornado.web.HTTPError(HTTPStatus.NOT_FOUND,reason="File not found",diff --git a/marimo/_smoke_tests/third_party/plotly.py b/marimo/_smoke_tests/third_party/plotly.pyindex dfd560b4a7..1f29c6f29f 100644--- a/marimo/_smoke_tests/third_party/plotly.py+++ b/marimo/_smoke_tests/third_party/plotly.py@@ -1,3 +1,4 @@+# Copyright 2023 Marimo. All rights reserved.import marimo__generated_with = "0.1.39"diff --git a/marimo/_smoke_tests/ws.py b/marimo/_smoke_tests/ws.pyindex 5b1166e605..9a461e913a 100644--- a/marimo/_smoke_tests/ws.py+++ b/marimo/_smoke_tests/ws.py@@ -1,3 +1,4 @@+# Copyright 2023 Marimo. All rights reserved.import marimo__generated_with = "0.1.39"