Skip to content

Commit

Permalink
Print thread pool errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Aug 9, 2023
1 parent 35b63f6 commit a39a64f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
6 changes: 5 additions & 1 deletion viser/_message_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,11 @@ def _queue(self, message: _messages.Message) -> None:
"""Wrapped method for sending messages safely."""
# This implementation will retain message ordering because _queue_thread has
# just 1 worker.
self._queue_thread.submit(lambda: self._queue_blocking(message))
from .infra._infra import error_print_wrapper

self._queue_thread.submit(
error_print_wrapper(lambda: self._queue_blocking(message))
)

def _queue_blocking(self, message: _messages.Message) -> None:
"""Wrapped method for sending messages safely. Blocks until ready to send."""
Expand Down
26 changes: 23 additions & 3 deletions viser/infra/_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,15 @@ async def serve(websocket: WebSocketServerProtocol) -> None:

def handle_incoming(message: Message) -> None:
self._thread_executor.submit(
lambda: self._handle_incoming_message(client_id, message)
error_print_wrapper(
lambda: self._handle_incoming_message(client_id, message)
)
)
self._thread_executor.submit(
lambda: client_connection._handle_incoming_message(
client_id, message
error_print_wrapper(
lambda: client_connection._handle_incoming_message(
client_id, message
)
)
)

Expand Down Expand Up @@ -392,3 +396,19 @@ async def _consumer(
assert isinstance(raw, bytes)
message = message_class.deserialize(raw)
handle_message(message)


def error_print_wrapper(inner: Callable[[], Any]) -> Callable[[], None]:
"""Wrap a Callable to print error messages when they happen.
This can be helpful for jobs submitted to ThreadPoolExecutor instances, which, by
default, will suppress error messages until returned futures are awaited.
"""

def wrapped() -> None:
try:
inner()
except Exception as e:
print(e)

return wrapped

0 comments on commit a39a64f

Please sign in to comment.