Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 22, 2026

Bot crashes with ClientOSError: [Errno 104] Connection reset by peer during nanapi calls. The client uses predicate-based backoff for HTTP status codes but lacks exception-based backoff for connection-level failures.

Changes

  • nanachan/utils/misc.py: Add ClientOSError to conn_backoff exception tuple; export conn_backoff and timeout_backoff for reuse

  • nanachan/nanapi/client.py: Compose timeout_backoff(conn_backoff) and apply to request chain in both load_bearer_token() and get_nanapi()

Retry Strategy

Request chain now handles (outermost to innermost):

  1. Connection errors (ClientOSError, ClientConnectorError, etc.) → exponential backoff, max 600s
  2. Timeout errors → exponential backoff, max 300s, 5 attempts
  3. Auth errors (401) → token refresh, max 2 attempts
  4. Status errors (502, 520, 522) → exponential backoff
# Before: only status-based retries
session._request = auth_backoff(session_backoff(wrap_request(session._request)))

# After: connection errors also retried
session._request = conn_exception_backoff(
    auth_backoff(session_backoff(wrap_request(session._request)))
)
Original prompt

This section details on the original issue you should resolve

<issue_title>[autoreport] aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer</issue_title>
<issue_description>## Traceback

Traceback (most recent call last):
  File "/usr/local/lib/python3.14/asyncio/selector_events.py", line 1009, in _read_ready__data_received
    data = self._sock.recv(self.max_size)
ConnectionResetError: [Errno 104] Connection reset by peer

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/.venv/lib/python3.14/site-packages/discord/client.py", line 504, in _run_event
    await coro(*args, **kwargs)
  File "/app/nanachan/extensions/waicolle.py", line 2515, in on_user_message
    resp = await get_nanapi().waicolle.waicolle_add_player_coins(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        str(ctx.author.id), AddPlayerCoinsBody(moecoins=moecoin_gain)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/app/nanachan/nanapi/_client.py", line 4548, in waicolle_add_player_coins
    async with self.session.post(
               ~~~~~~~~~~~~~~~~~^
        url,
        ^^^^
        params=params,
        ^^^^^^^^^^^^^^
        json=body,
        ^^^^^^^^^^
    ) as resp:
    ^
  File "/app/.venv/lib/python3.14/site-packages/aiohttp/client.py", line 1510, in __aenter__
    self._resp: _RetType = await self._coro
                           ^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/backoff/_async.py", line 76, in retry
    ret = await target(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/backoff/_async.py", line 76, in retry
    ret = await target(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/nanachan/nanapi/client.py", line 73, in _wrapped
    return await _request(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/aiohttp/client.py", line 779, in _request
    resp = await handler(req)
           ^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/aiohttp/client.py", line 757, in _connect_and_send_request
    await resp.start(conn)
  File "/app/.venv/lib/python3.14/site-packages/aiohttp/client_reqrep.py", line 539, in start
    message, payload = await protocol.read()  # type: ignore[union-attr]
                       ^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/aiohttp/streams.py", line 680, in read
    await self._waiter
aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer

Initial user interaction

None

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 22, 2026 09:21
Co-authored-by: NextFire <20094890+NextFire@users.noreply.github.com>
Co-authored-by: NextFire <20094890+NextFire@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix aiohttp.client_exceptions.ClientOSError connection reset issue Add ClientOSError retry handling to nanapi client Jan 22, 2026
Copilot AI requested a review from NextFire January 22, 2026 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[autoreport] aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer

2 participants