Skip to content

Commit e75011a

Browse files
jakekaplanclaude
andauthored
Reduce noisy websocket reconnection warnings in events client (#19814)
Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent 9618402 commit e75011a

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/prefect/events/clients.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ async def __aenter__(self) -> Self:
281281
# Don't handle any errors in the initial connection, because these are most
282282
# likely a permission or configuration issue that should propagate
283283
await super().__aenter__()
284-
await self._reconnect()
284+
try:
285+
await self._reconnect()
286+
except Exception as e:
287+
self._log_connection_error(e)
288+
raise
285289
return self
286290

287291
async def __aexit__(
@@ -298,6 +302,18 @@ def _log_debug(self, message: str, *args: Any, **kwargs: Any) -> None:
298302
message = f"EventsClient(id={id(self)}): " + message
299303
logger.debug(message, *args, **kwargs)
300304

305+
def _log_connection_error(self, error: Exception) -> None:
306+
logger.warning(
307+
"Unable to connect to %r. "
308+
"Please check your network settings to ensure websocket connections "
309+
"to the API are allowed. Otherwise event data (including task run data) may be lost. "
310+
"Reason: %s. "
311+
"Set PREFECT_DEBUG_MODE=1 to see the full error.",
312+
self._events_socket_url,
313+
str(error),
314+
exc_info=PREFECT_DEBUG_MODE.value(),
315+
)
316+
301317
async def _reconnect(self) -> None:
302318
logger.debug("Reconnecting websocket connection.")
303319

@@ -315,15 +331,10 @@ async def _reconnect(self) -> None:
315331
await pong
316332
logger.debug("Pong received. Websocket connected.")
317333
except Exception as e:
318-
# The client is frequently run in a background thread
319-
# so we log an additional warning to ensure
320-
# surfacing the error to the user.
321-
logger.warning(
322-
"Unable to connect to %r. "
323-
"Please check your network settings to ensure websocket connections "
324-
"to the API are allowed. Otherwise event data (including task run data) may be lost. "
325-
"Reason: %s. "
326-
"Set PREFECT_DEBUG_MODE=1 to see the full error.",
334+
# Log at debug level during reconnection attempts - the warning will
335+
# only be logged if all reconnection attempts fail (in _emit)
336+
logger.debug(
337+
"Unable to connect to %r, will retry. Reason: %s",
327338
self._events_socket_url,
328339
str(e),
329340
exc_info=PREFECT_DEBUG_MODE.value(),
@@ -391,10 +402,11 @@ async def _emit(self, event: Event) -> None:
391402
await self._checkpoint()
392403

393404
return
394-
except ConnectionClosed:
405+
except ConnectionClosed as e:
395406
self._log_debug("Got ConnectionClosed error.")
396407
if i == self._reconnection_attempts:
397-
# this was our final chance, raise the most recent error
408+
# this was our final chance, log warning and raise
409+
self._log_connection_error(e)
398410
raise
399411

400412
if i > 2:

0 commit comments

Comments
 (0)