Skip to content

Commit ba0f4f5

Browse files
committed
fix: update disconnect method to close transport directly
1 parent de005de commit ba0f4f5

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/wokwi_client/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def __init__(self, token: str, server: Optional[str] = None):
4949
self._transport = Transport(token, server or DEFAULT_WS_URL)
5050
self.last_pause_nanos = 0
5151
self._transport.add_event_listener("sim:pause", self._on_pause)
52-
self._pause_queue = EventQueue(self._transport, "sim:pause")
52+
# Lazily create in an active event loop (important for py3.9 and sync client)
53+
self._pause_queue: Optional[EventQueue] = None
5354
self._serial_monitor_tasks: set[asyncio.Task[None]] = set()
5455

5556
async def connect(self) -> dict[str, Any]:
@@ -181,6 +182,8 @@ async def wait_until_simulation_time(self, seconds: float) -> None:
181182
await pause(self._transport)
182183
remaining_nanos = seconds * 1e9 - self.last_pause_nanos
183184
if remaining_nanos > 0:
185+
if self._pause_queue is None:
186+
self._pause_queue = EventQueue(self._transport, "sim:pause")
184187
self._pause_queue.flush()
185188
await resume(self._transport, int(remaining_nanos))
186189
await self._pause_queue.get()

src/wokwi_client/client_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def disconnect(self) -> None:
119119

120120
# (2) Disconnect transport
121121
with contextlib.suppress(Exception):
122-
self._call(self._async_client.disconnect())
122+
self._call(self._async_client._transport.close())
123123

124124
# (3) Stop loop / join thread
125125
if self._loop.is_running():

src/wokwi_client/event_queue.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ class EventQueue:
2323
"""A queue for events from a specific event type."""
2424

2525
def __init__(self, transport: Transport, event_type: str) -> None:
26+
# Bind the queue to the current running loop (important for py3.9)
27+
self._loop = asyncio.get_running_loop()
2628
self._queue: asyncio.Queue[EventMessage] = asyncio.Queue()
2729
self._transport = transport
2830
self._event_type = event_type
2931

3032
def listener(event: EventMessage) -> None:
31-
self._queue.put_nowait(event)
33+
# Ensure we enqueue on the loop that owns the queue, even if
34+
# the listener is invoked from a different loop/thread.
35+
self._loop.call_soon_threadsafe(self._queue.put_nowait, event)
3236

3337
self._listener = listener
3438
self._transport.add_event_listener(self._event_type, self._listener)

0 commit comments

Comments
 (0)