Fix the telnet console shutdown error after a failed start (#7910)

This commit is contained in:
Adrian 2026-08-09 11:03:57 +02:00 committed by GitHub
parent 1e92635a18
commit 56f4afd84e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

@ -52,6 +52,7 @@ class TelnetConsole(protocol.ServerFactory):
self.crawler: Crawler = crawler
self.noisy: bool = False
self.port: Port | None = None
self.portrange: list[int] = [
int(x) for x in crawler.settings.getlist("TELNETCONSOLE_PORT")
]
@ -71,7 +72,7 @@ class TelnetConsole(protocol.ServerFactory):
return cls(crawler)
def start_listening(self) -> None:
self.port: Port = listen_tcp(self.portrange, self.host, self)
self.port = listen_tcp(self.portrange, self.host, self)
h = self.port.getHost()
logger.info(
"Telnet console listening on %(host)s:%(port)d",
@ -80,7 +81,10 @@ class TelnetConsole(protocol.ServerFactory):
)
def stop_listening(self) -> None:
self.port.stopListening()
# The port is unset if start_listening() failed, e.g. because every
# port in TELNETCONSOLE_PORT was taken.
if self.port is not None:
self.port.stopListening()
def protocol(self) -> telnet.TelnetTransport:
class Portal:

View File

@ -1,5 +1,6 @@
from __future__ import annotations
import socket
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any
@ -91,6 +92,18 @@ def test_invalid_reversed_portrange() -> None:
console.start_listening()
@coroutine_test
async def test_unavailable_port(caplog: pytest.LogCaptureFixture) -> None:
"""Run a crawl where the console cannot bind any port."""
with socket.create_server(("127.0.0.1", 0)) as sock:
port = sock.getsockname()[1]
crawler = _get_crawler(settings_dict={"TELNETCONSOLE_PORT": [port]})
await crawler.crawl_async()
assert "CannotListenError" in caplog.text
assert "AttributeError" not in caplog.text
@coroutine_test
async def test_telnet_vars() -> None:
"""Log into the console of a running crawl, which is when the telnet