Skip to content

Commit e9813d3

Browse files
committed
Fix error about acquiring future from wrong loop
1 parent 6c0b374 commit e9813d3

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

shiny/reactive/_core.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,24 @@ def __init__(self) -> None:
119119
)
120120
self._next_id: int = 0
121121
self._pending_flush_queue: PriorityQueueFIFO[Context] = PriorityQueueFIFO()
122-
self.lock = asyncio.Lock()
122+
self._lock: Optional[asyncio.Lock] = None
123123
self._flushed_callbacks = _utils.AsyncCallbacks()
124124

125+
@property
126+
def lock(self) -> asyncio.Lock:
127+
"""
128+
Lock that protects this ReactiveEnvironment. It must be lazily created, because
129+
at the time the module is loaded, there generally isn't a running asyncio loop
130+
yet. This causes the asyncio.Lock to be created with a different loop than it
131+
will be invoked from later; when that happens, acquire() will succeed if there's
132+
no contention, but throw a "hey you're on the wrong loop" error if there is.
133+
"""
134+
if self._lock is None:
135+
# Ensure we have a loop; get_running_loop() throws an error if we don't
136+
asyncio.get_running_loop()
137+
self._lock = asyncio.Lock()
138+
return self._lock
139+
125140
def next_id(self) -> int:
126141
"""Return the next available id"""
127142
id = self._next_id

0 commit comments

Comments
 (0)