Skip to content

fix: report close errors from the asyncio transport as the selector does - #3208

Open
youdie006 wants to merge 2 commits into
dpkp:masterfrom
youdie006:asyncio-close-errors
Open

youdie006 wants to merge 2 commits into
dpkp:masterfrom
youdie006:asyncio-close-errors

Conversation

@youdie006

Copy link
Copy Markdown

Description

With the asyncio backend (net='asyncio', or the default net=None inside a running asyncio loop), a connection closed with an error looks like a clean close: in-flight requests fail with Cancelled instead of RequestTimedOutError or KafkaConnectionError, and close_future succeeds. TxnRequestHandler.on_complete retries only those two errors and makes anything else fatal (L859-L873), so on asyncio the retry from #3192 never applies, and a broker disconnect makes the transactional producer fatal.

Root Cause

A request timeout calls close(RequestTimedOutError(...)) (connection.py L191), which calls transport.abort(error) (L330-L339). KafkaTCPTransport.abort hands that error to protocol.connection_lost (transport.py L187-L199), and so does KafkaSSLTransport. _AsyncioTransport.abort drops it (asyncio_backend.py L428-L429), asyncio then calls connection_lost(None), and fail_in_flight_requests(None) uses Cancelled() (connection.py L248).

The selector transport also reports a peer EOF as KafkaConnectionError('socket disconnected') and wraps socket errors in KafkaConnectionError (transport.py L111-L123). The asyncio adapter passes None and raw OSErrors through.

Changes

  • _AsyncioTransport.abort keeps the error, unless the transport is already closing, and the protocol adapter passes it to connection_lost. A close() followed by abort(error) stays a clean close, as on the selector.
  • The adapter reports a peer EOF as KafkaConnectionError('socket disconnected') and wraps a non-Kafka exception in KafkaConnectionError. Kafka errors, such as a KafkaProtocolError raised from data_received, pass through unchanged.

The selector backend is unchanged. On asyncio, an error close now fails close_future, so it logs "Connection lost" and triggers the manager's metadata refresh as the selector does. An idle connection closed by the broker logs "Connection lost: socket disconnected".

A loopback listener that never answers, request_timeout_ms=300, two MetadataRequests, at fa42377:

before: selector  RequestTimedOutError x2, close_future failed RequestTimedOutError
        asyncio   Cancelled x2, close_future succeeded None
        peer closes: selector KafkaConnectionError('socket disconnected'), asyncio Cancelled
after:  asyncio   RequestTimedOutError x2, close_future failed RequestTimedOutError
        peer closes: asyncio KafkaConnectionError('socket disconnected')

Tests

The new tests in test/net/backend/test_asyncio_backend.py cover:

  • abort(error);
  • close() then abort(error);
  • a protocol error;
  • a peer EOF and a peer reset;
  • a KafkaConnection request timeout.

The abort, EOF, reset and request-timeout tests fail on master. pytest test --ignore=test/integration (2765 passed) and make lint pass on Python 3.12, and the backend tests pass on 3.8, 3.9, 3.10 and 3.14. I did not run the integration tests (no Kafka here) or TLS on asyncio.

Written with AI assistance (Claude); I have reviewed the change.

_AsyncioTransport.abort(error) dropped the error and asyncio then calls
connection_lost(None), so a request timeout or a broker disconnect failed
in-flight requests with Cancelled and resolved close_future as a clean
close. Keep the abort error for the protocol adapter, report a peer EOF
as KafkaConnectionError('socket disconnected') and wrap socket errors in
KafkaConnectionError, as KafkaTCPTransport does.
On a busy runner the listener could reset the socket before connect()
completed, so create_connection itself failed.
@youdie006

Copy link
Copy Markdown
Author

The four red jobs all failed on my new test_peer_close_reaches_connection_lost_as_connection_error[reset]. The listener could reset the socket before connect() finished, so create_connection itself failed. 83f307c makes the listener wait for the client's first bytes before it closes. Run 200 times each with every core busy, the old test failed 23 of 400 times and the new one passed all 400.

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.

1 participant