Skip to content

Commit

Permalink
Warn about communication attempts with disconnected clients (#3123)
Browse files Browse the repository at this point in the history
* refactoring

* check existence before communicating with client

* Revert "refactoring"

This reverts commit bf089c2.

* add stack trace
  • Loading branch information
falkoschindler committed May 27, 2024
1 parent 81db6d7 commit bdc3a14
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def __init__(self, page: page, *, shared: bool = False) -> None:
self.shared = shared
self.on_air = False
self._disconnect_task: Optional[asyncio.Task] = None
self._deleted = False
self._has_warned_about_deleted_client = False
self.tab_id: Optional[str] = None

self.outbox = Outbox(self)
Expand Down Expand Up @@ -320,6 +322,15 @@ def delete(self) -> None:
self.remove_all_elements()
self.outbox.stop()
del Client.instances[self.id]
self._deleted = True

def check_existence(self) -> None:
"""Check if the client still exists and print a warning if it doesn't."""
if self._deleted and not self._has_warned_about_deleted_client:
log.warning('Client has been deleted but is still being used. This is most likely a bug in your application code. '
'See https://github.com/zauberzeug/nicegui/issues/3028 for more information.',
stack_info=True)
self._has_warned_about_deleted_client = True

@contextmanager
def individual_target(self, socket_id: str) -> Iterator[None]:
Expand Down
3 changes: 3 additions & 0 deletions nicegui/outbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ def _set_enqueue_event(self) -> None:

def enqueue_update(self, element: Element) -> None:
"""Enqueue an update for the given element."""
self.client.check_existence()
self.updates[element.id] = element
self._set_enqueue_event()

def enqueue_delete(self, element: Element) -> None:
"""Enqueue a deletion for the given element."""
self.client.check_existence()
self.updates[element.id] = None
self._set_enqueue_event()

def enqueue_message(self, message_type: MessageType, data: Any, target_id: ClientId) -> None:
"""Enqueue a message for the given client."""
self.client.check_existence()
self.messages.append((target_id, message_type, data))
self._set_enqueue_event()

Expand Down

0 comments on commit bdc3a14

Please sign in to comment.