Skip to content

Commit

Permalink
Fix issue with global output manager tree (#2062)
Browse files Browse the repository at this point in the history
* Preventure test leakage by resetting singleton

* Fix issue by making _tree an instance property
  • Loading branch information
erikbern committed Jul 31, 2024
1 parent 0c73d3e commit 2346aa9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
26 changes: 15 additions & 11 deletions modal/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ def finalize(self):

class OutputManager:
_instance: ClassVar[Optional["OutputManager"]] = None
_tree: ClassVar[Optional[Tree]] = None

_console: Console
_task_states: Dict[str, int]
Expand All @@ -168,6 +167,7 @@ class OutputManager:
_app_page_url: Optional[str]
_show_image_logs: bool
_status_spinner_live: Optional[Live]
_tree: Optional[Tree]

def __init__(
self,
Expand All @@ -188,6 +188,7 @@ def __init__(
self._app_page_url = None
self._show_image_logs = False
self._status_spinner_live = None
self._tree = None

@classmethod
def disable(cls):
Expand Down Expand Up @@ -389,23 +390,26 @@ def show_status_spinner(self):
@classmethod
@contextlib.contextmanager
def make_tree(cls):
# Note: If the output isn't enabled, don't actually show the tree.
cls._tree = Tree(step_progress("Creating objects..."), guide_style="gray50")

if output_mgr := OutputManager.get():
with output_mgr.make_live(cls._tree):
yield
cls._tree.label = step_completed("Created objects.")
output_mgr.print(output_mgr._tree)
tree = output_mgr._tree = Tree(step_progress("Creating objects..."), guide_style="gray50")
with output_mgr.make_live(tree):
try:
yield
finally:
output_mgr._tree = None
tree.label = step_completed("Created objects.")
output_mgr.print(tree)
else:
yield

@classmethod
def add_status_row(cls) -> "StatusRow":
# Return a status row to be used for object creation.
# If output isn't enabled, the status row might be invisible.
assert cls._tree, "Output manager has no tree yet"
return StatusRow(cls._tree)
# If output isn't enabled, just create a hidden tree
if cls._instance and cls._instance._tree:
return StatusRow(cls._instance._tree)
else:
return StatusRow(Tree(""))


class ProgressHandler:
Expand Down
6 changes: 6 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import modal._serialization
from modal import __version__, config
from modal._container_io_manager import _ContainerIOManager
from modal._output import OutputManager
from modal._serialization import serialize_data_format
from modal._utils.async_utils import asyncify, synchronize_api
from modal._utils.grpc_testing import patch_mock_servicer
Expand Down Expand Up @@ -1551,6 +1552,11 @@ async def reset_default_client():
Client.set_env_client(None)


@pytest_asyncio.fixture(scope="function", autouse=True)
async def reset_output_manager():
OutputManager._instance = None


@pytest.fixture(name="mock_dir", scope="session")
def mock_dir_factory():
"""Sets up a temp dir with content as specified in a nested dict
Expand Down

0 comments on commit 2346aa9

Please sign in to comment.