Merge pull request #2669 from arl1984/fix/telnet-proxy-reader-cleanup-2344

Fix telnet console silent-proxy hang on non-ConnectionError exit (#2344)
This commit is contained in:
Jeremy Grossmann 2026-05-08 15:42:51 +08:00 committed by GitHub
commit cd03abb7ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -215,18 +215,32 @@ class AsyncioTelnetServer:
await self._write_intro(network_writer, echo=self._echo, binary=self._binary, naws=self._naws)
await connection.connected()
await self._process(network_reader, network_writer, connection)
except (ConnectionError, OSError):
except (ConnectionError, OSError, asyncio.CancelledError):
pass
except Exception:
# Catch any unexpected exception so the cleanup below still runs.
# Without the try/finally, an uncaught exception here would leave
# _reader_process pinned to a dead reader, and subsequent client
# connections would see _get_reader() return None and never
# receive node output -- the "silent proxy" hang (issue #2344).
log.exception("Unexpected error in telnet proxy; cleaning up client connection...")
finally:
async with self._lock:
try:
network_writer.close()
# await network_writer.wait_closed() # this doesn't work in Python 3.6
finally:
if self._reader_process == network_reader:
self._reader_process = None
# Cancel current read from this reader
if self._current_read is not None:
self._current_read.cancel()
self._current_read = None
try:
await connection.disconnected()
del self._connections[network_writer]
finally:
# Use pop() to avoid KeyError if connection was already removed
# elsewhere (e.g. by the broadcast loop's timeout handler)
self._connections.pop(network_writer, None)
async def close(self):
for writer, connection in self._connections.items():
@ -318,7 +332,7 @@ class AsyncioTelnetServer:
except (OSError, ConnectionError, asyncio.TimeoutError) as e:
log.debug(f"Error sending data to client {client_info}: {e}, closing and removing from connection table.")
connection.close()
del self._connections[connection_key]
self._connections.pop(connection_key, None)
async def _read(self, cmd, buffer, location, reader):
""" Reads next op from the buffer or reader"""