Skip to content

Commit 4c562de

Browse files
websocket.py: fix on_close() callback
1 parent f3f975c commit 4c562de

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

internal_filesystem/lib/websocket.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async def _process_callbacks_async():
4848
"""Process queued callbacks asynchronously."""
4949
import _thread
5050
while True: # this stops when "NWCWallet: manage_wallet_thread stopping, closing connections..."
51-
#print(f"thread {_thread.get_ident()}: _process_callbacks_async")
51+
#print(f"_process_callbacks_async thread {_thread.get_ident()}: _process_callbacks_async")
5252
while _callback_queue:
5353
_log_debug("Processing callbacks queue...")
5454
try:
@@ -260,7 +260,7 @@ async def _async_main(self):
260260
while self.running:
261261
_log_debug("Main loop iteration: self.running=True")
262262
try:
263-
await self._connect_and_run()
263+
await self._connect_and_run() # keep waiting for it, until finished
264264
except Exception as e:
265265
_log_error(f"_async_main got exception: {e}")
266266
self.has_errored = True
@@ -275,14 +275,15 @@ async def _async_main(self):
275275

276276
# Cleanup
277277
_log_debug("Initiating cleanup")
278+
_run_callback(self.on_close, self, None, None)
279+
await asyncio.sleep(1) # wait a bit for _process_callbacks_async to call on_close
278280
self.running = False
279281
callback_task.cancel() # Stop callback task
280282
try:
281283
await callback_task
282284
except asyncio.CancelledError:
283285
_log_debug("Callback task cancelled")
284286
await self._close_async()
285-
_run_callback(self.on_close, self, None, None)
286287
_log_debug("_async_main completed")
287288

288289
async def _connect_and_run(self):

0 commit comments

Comments
 (0)