Skip to content

Handle telnet client socket resets gracefully#2076

Draft
liyimil wants to merge 1 commit into
prompt-toolkit:mainfrom
liyimil:agent/handle-telnet-rst
Draft

Handle telnet client socket resets gracefully#2076
liyimil wants to merge 1 commit into
prompt-toolkit:mainfrom
liyimil:agent/handle-telnet-rst

Conversation

@liyimil

@liyimil liyimil commented Jul 18, 2026

Copy link
Copy Markdown

What

  • move the telnet socket reader into a testable TelnetConnection method
  • treat OSError from recv() as a client disconnect and close the connection
  • cover regular data, EOF, and connection-reset paths with unit tests

Why

A client that closes its socket with TCP RST makes recv() raise from the event-loop reader callback. Because that callback had no exception boundary, asyncio reported an unhandled exception instead of cleaning up the telnet connection.

Impact

Normal reads and graceful EOF handling are unchanged. Socket read errors now remove the reader and close the connection through the existing cleanup path.

Fixes #2073.

Checks

  • python -m pytest tests/test_telnet.py -q — 3 passed
  • python -m pytest tests -q -k "not test_emacs_history_bindings and not test_emacs_reverse_search and not test_prompt_session_memory_leak and not test_print_container" — 150 passed, 5 skipped, 4 deselected
  • ruff check . — passed
  • ruff format --check . — passed
  • mypy --strict src/prompt_toolkit/contrib/telnet/server.py --platform {win32,linux,darwin} — passed

The four locally deselected tests require arrow-key input or a Win32 console screen buffer, which are unavailable in the non-interactive Windows validation host.

Upstream CI ran the unit suite successfully on all six Python targets (3.10–3.14t), including formatting and typo checks on 3.14. Each job then hit the same unrelated full-repository Mypy errors in application.py:1117, application.py:1121, and application.py:1124; the changed telnet module passes strict Mypy on all configured platforms.

@liyimil
liyimil force-pushed the agent/handle-telnet-rst branch from b74cf0c to 0dea328 Compare July 18, 2026 01:56
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.

TelnetServer recv error: Forcing an RST request to the telnet server can cause a crash

1 participant