@@ -315,17 +315,6 @@ async def ws_handler(
315315 connection : websockets .asyncio .server .ServerConnection ,
316316 ) -> None :
317317 """Handler for websocket connections."""
318-
319- # <Hack>
320- # Suppress errors for: https://github.com/python-websockets/websockets/issues/1513
321- # TODO: remove this when websockets behavior changes upstream.
322- class NoHttpErrors (logging .Filter ):
323- def filter (self , record ):
324- return not record .getMessage () == "opening handshake failed"
325-
326- connection .logger .logger .addFilter (NoHttpErrors ()) # type: ignore
327- # </Hack>
328-
329318 async with count_lock :
330319 nonlocal connection_count
331320 client_id = ClientId (connection_count )
@@ -414,10 +403,26 @@ def handle_incoming(message: Message) -> None:
414403 file_cache : dict [Path , bytes ] = {}
415404 file_cache_gzipped : dict [Path , bytes ] = {}
416405
406+ filter_added = False
407+
417408 def viser_http_server (
418409 connection : ServerConnection ,
419410 request : Request ,
420411 ) -> Response | None :
412+ # <Hack>
413+ # Suppress errors for: https://github.com/python-websockets/websockets/issues/1513
414+ # TODO: remove this when websockets behavior changes upstream.
415+ nonlocal filter_added
416+ if not filter_added :
417+
418+ class NoHttpErrors (logging .Filter ):
419+ def filter (self , record ):
420+ return not record .getMessage () == "opening handshake failed"
421+
422+ connection .logger .logger .addFilter (NoHttpErrors ()) # type: ignore
423+ filter_added = True
424+ # </Hack>
425+
421426 # Ignore websocket packets.
422427 if request .headers .get ("Upgrade" ) == "websocket" :
423428 return None
0 commit comments