Skip to content

Commit 155ae4f

Browse files
committed
Guard against nested live displays
1 parent dbf0236 commit 155ae4f

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/_pytask/live.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
from _pytask.session import Session
3636

3737

38+
_LIVE_DISPLAY_OWNER: LiveManager | None = None
39+
40+
3841
@hookimpl
3942
def pytask_extend_command_line_interface(cli: click.Group) -> None:
4043
"""Extend command line interface."""
@@ -110,6 +113,18 @@ class LiveManager:
110113
)
111114

112115
def start(self) -> None:
116+
global _LIVE_DISPLAY_OWNER # noqa: PLW0603
117+
if _LIVE_DISPLAY_OWNER is not None and _LIVE_DISPLAY_OWNER is not self:
118+
msg = (
119+
"pytask tried to launch a second live display which is impossible. The "
120+
"issue occurs when you use pytask on the command line on a task module "
121+
"that uses the programmatic interface of pytask at the same time. "
122+
"Use either the command line or the programmatic interface."
123+
)
124+
raise Exit(msg)
125+
if self._live.is_started:
126+
_LIVE_DISPLAY_OWNER = self
127+
return
113128
try:
114129
self._live.start()
115130
except LiveError:
@@ -120,11 +135,15 @@ def start(self) -> None:
120135
"Use either the command line or the programmatic interface."
121136
)
122137
raise Exit(msg) from None
138+
_LIVE_DISPLAY_OWNER = self
123139

124140
def stop(self, transient: bool | None = None) -> None:
141+
global _LIVE_DISPLAY_OWNER # noqa: PLW0603
125142
if transient is not None:
126143
self._live.transient = transient
127144
self._live.stop()
145+
if _LIVE_DISPLAY_OWNER is self:
146+
_LIVE_DISPLAY_OWNER = None
128147

129148
def pause(self) -> None:
130149
self._live.transient = True

0 commit comments

Comments
 (0)