Skip to content

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.

Use weakref.finalize instead of __del__ in Html destruction (#1591)

Akshay Agrawal198af428ba92
diff --git a/marimo/_output/hypertext.py b/marimo/_output/hypertext.py
index 6ac76dd353..bc9054bbec 100644
--- a/marimo/_output/hypertext.py
+++ b/marimo/_output/hypertext.py
@@ -1,13 +1,13 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from typing import TYPE_CHECKING, Any, Callable, Literal, final
+import weakref
+from typing import TYPE_CHECKING, Any, Literal, final
from marimo._messaging.mimetypes import KnownMimeType
from marimo._output.mime import MIME
from marimo._output.rich_help import mddoc
from marimo._output.utils import flatten_string
-from marimo._utils.exiting import python_exiting
if TYPE_CHECKING:
from marimo._plugins.core.web_component import JSONType
@@ -15,6 +15,23 @@ if TYPE_CHECKING:
from marimo._plugins.ui._impl.batch import batch as batch_plugin
+def _hypertext_cleanup(virtual_filenames: list[str]) -> None:
+ """Cleanup side-effects related to initialization of Html."""
+ from marimo._runtime.context import (
+ ContextNotInitializedError,
+ get_context,
+ )
+
+ try:
+ ctx = get_context()
+ except ContextNotInitializedError:
+ return
+
+ if ctx is not None and ctx.virtual_files_supported:
+ for f in virtual_filenames:
+ ctx.virtual_file_registry.dereference(f)
+
+
@mddoc
class Html(MIME):
"""A wrapper around HTML text that can be used as an output.
@@ -75,9 +92,9 @@ class Html(MIME):
# Virtual File Refcounting
#
# HTML elements are responsible for maintaining the reference counts
- # of virtual files: virtual files cannot be disposed when HTML elements
- # reference them. For example, a user might cache HTML referencing a
- # virtual file if they create it using functools.cache.
+ # of virtual files: virtual files cannot be disposed while HTML
+ # elements reference them. For example, a user might cache HTML
+ # referencing a virtual file if they create it using functools.cache.
#
# flatten the text to make sure searching isn't broken by newlines
flat_text = flatten_string(self._text)
@@ -86,34 +103,11 @@ class Html(MIME):
ctx.virtual_file_registry.reference(virtual_filename)
self._virtual_filenames.append(virtual_filename)
- # bind the function python_exiting to ensure it still exists at Python
- # destruction time; for graceful exits when running as a script
- def __del__(
- self, _python_exiting: Callable[..., bool] = python_exiting
- ) -> None:
- """Cleanup side-effects related to initialization.
-
- Subclasses MUST implement a __del__ method that ends by calling
- this method.
- """
- if _python_exiting():
- # imports can fail when python is exiting; clean-up
- # is not important when exiting anyway
- return
-
- from marimo._runtime.context import (
- ContextNotInitializedError,
- get_context,
+ # Dereference virtual files on object destruction
+ finalizer = weakref.finalize(
+ self, _hypertext_cleanup, self._virtual_filenames
)
-
- try:
- ctx = get_context()
- except ContextNotInitializedError:
- return
-
- if ctx is not None and ctx.virtual_files_supported:
- for f in self._virtual_filenames:
- ctx.virtual_file_registry.dereference(f)
+ finalizer.atexit = False
@property
def text(self) -> str:
diff --git a/marimo/_plugins/stateless/status/_progress.py b/marimo/_plugins/stateless/status/_progress.py
index 6905f23432..81a9a37122 100644
--- a/marimo/_plugins/stateless/status/_progress.py
+++ b/marimo/_plugins/stateless/status/_progress.py
@@ -5,7 +5,6 @@ import time
from typing import (
TYPE_CHECKING,
Any,
- Callable,
Iterable,
Optional,
TypeVar,
@@ -17,7 +16,6 @@ from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
from marimo._plugins.core.web_component import build_stateless_plugin
from marimo._utils.debounce import debounce
-from marimo._utils.exiting import python_exiting
if TYPE_CHECKING:
from collections.abc import Collection
@@ -53,14 +51,6 @@ class _Progress(Html):
self.start_time = time.time()
super().__init__(self._get_text())
- def __del__(
- self, _python_exiting: Callable[..., bool] = python_exiting
- ) -> None:
- if _python_exiting():
- return
-
- super().__del__()
-
def update_progress(
self,
increment: int = 1,