Skip to content

Commit

Permalink
Remove stack traces from INFO and WARNING logs.
Browse files Browse the repository at this point in the history
Fix #1501.
  • Loading branch information
aaugustin committed Nov 6, 2024
1 parent 0a5a79c commit 9b3595d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/websockets/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import logging
import os
import traceback
import urllib.parse
from collections.abc import AsyncIterator, Generator, Sequence
from types import TracebackType
Expand Down Expand Up @@ -521,9 +522,10 @@ async def __aiter__(self) -> AsyncIterator[ClientConnection]:
delays = backoff()
delay = next(delays)
self.logger.info(
"connect failed; reconnecting in %.1f seconds",
"connect failed; reconnecting in %.1f seconds: %s",
delay,
exc_info=True,
# Remove first argument when dropping Python 3.9.
traceback.format_exception_only(type(exc), exc)[0].strip(),
)
await asyncio.sleep(delay)
continue
Expand Down
9 changes: 7 additions & 2 deletions src/websockets/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import random
import struct
import sys
import traceback
import uuid
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Iterable, Mapping
from types import TracebackType
Expand Down Expand Up @@ -1180,8 +1181,12 @@ def broadcast(
exceptions.append(exception)
else:
connection.logger.warning(
"skipped broadcast: failed to write message",
exc_info=True,
"skipped broadcast: failed to write message: %s",
traceback.format_exception_only(
# Remove first argument when dropping Python 3.9.
type(write_exception),
write_exception,
)[0].strip(),
)

if raise_exceptions and exceptions:
Expand Down
13 changes: 8 additions & 5 deletions src/websockets/legacy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import os
import random
import traceback
import urllib.parse
import warnings
from collections.abc import AsyncIterator, Generator, Sequence
Expand Down Expand Up @@ -597,22 +598,24 @@ async def __aiter__(self) -> AsyncIterator[WebSocketClientProtocol]:
try:
async with self as protocol:
yield protocol
except Exception:
except Exception as exc:
# Add a random initial delay between 0 and 5 seconds.
# See 7.2.3. Recovering from Abnormal Closure in RFC 6455.
if backoff_delay == self.BACKOFF_MIN:
initial_delay = random.random() * self.BACKOFF_INITIAL
self.logger.info(
"connect failed; reconnecting in %.1f seconds",
"connect failed; reconnecting in %.1f seconds: %s",
initial_delay,
exc_info=True,
# Remove first argument when dropping Python 3.9.
traceback.format_exception_only(type(exc), exc)[0].strip(),
)
await asyncio.sleep(initial_delay)
else:
self.logger.info(
"connect failed again; retrying in %d seconds",
"connect failed again; retrying in %d seconds: %s",
int(backoff_delay),
exc_info=True,
# Remove first argument when dropping Python 3.9.
traceback.format_exception_only(type(exc), exc)[0].strip(),
)
await asyncio.sleep(int(backoff_delay))
# Increase delay with truncated exponential backoff.
Expand Down
9 changes: 7 additions & 2 deletions src/websockets/legacy/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import struct
import sys
import time
import traceback
import uuid
import warnings
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Iterable, Mapping
Expand Down Expand Up @@ -1624,8 +1625,12 @@ def broadcast(
exceptions.append(exception)
else:
websocket.logger.warning(
"skipped broadcast: failed to write message",
exc_info=True,
"skipped broadcast: failed to write message: %s",
traceback.format_exception_only(
# Remove first argument when dropping Python 3.9.
type(write_exception),
write_exception,
)[0].strip(),
)

if raise_exceptions and exceptions:
Expand Down
5 changes: 4 additions & 1 deletion tests/asyncio/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,10 @@ async def test_broadcast_skips_connection_failing_to_send(self):

self.assertEqual(
[record.getMessage() for record in logs.records],
["skipped broadcast: failed to write message"],
[
"skipped broadcast: failed to write message: "
"RuntimeError: Cannot call write() after write_eof()"
],
)

@unittest.skipIf(
Expand Down
8 changes: 6 additions & 2 deletions tests/legacy/test_client_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,10 @@ async def run_client():
],
)
# Iteration 3
exc = (
"websockets.legacy.exceptions.InvalidStatusCode: "
"server rejected WebSocket connection: HTTP 503"
)
self.assertEqual(
[
re.sub(r"[0-9\.]+ seconds", "X seconds", record.getMessage())
Expand All @@ -1615,12 +1619,12 @@ async def run_client():
[
"connection rejected (503 Service Unavailable)",
"connection closed",
"connect failed; reconnecting in X seconds",
f"connect failed; reconnecting in X seconds: {exc}",
]
+ [
"connection rejected (503 Service Unavailable)",
"connection closed",
"connect failed again; retrying in X seconds",
f"connect failed again; retrying in X seconds: {exc}",
]
* ((len(logs.records) - 8) // 3)
+ [
Expand Down
4 changes: 2 additions & 2 deletions tests/legacy/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,14 +1547,14 @@ def test_broadcast_reports_connection_sending_fragmented_text(self):

def test_broadcast_skips_connection_failing_to_send(self):
# Configure mock to raise an exception when writing to the network.
self.protocol.transport.write.side_effect = RuntimeError
self.protocol.transport.write.side_effect = RuntimeError("BOOM")

with self.assertLogs("websockets", logging.WARNING) as logs:
broadcast([self.protocol], "café")

self.assertEqual(
[record.getMessage() for record in logs.records],
["skipped broadcast: failed to write message"],
["skipped broadcast: failed to write message: RuntimeError: BOOM"],
)

@unittest.skipIf(
Expand Down

0 comments on commit 9b3595d

Please sign in to comment.