diff --git a/src/anycorn/config.py b/src/anycorn/config.py index 057cf92..7f7ece1 100644 --- a/src/anycorn/config.py +++ b/src/anycorn/config.py @@ -267,16 +267,26 @@ def ssl_enabled(self) -> bool: def create_sockets(self) -> Sockets: """Create and return the server sockets.""" - if self.ssl_enabled: - secure_sockets = self._create_sockets(self.bind) - insecure_sockets = self._create_sockets(self.insecure_bind) - quic_sockets = self._create_sockets(self.quic_bind, socket.SOCK_DGRAM) - self._set_quic_addresses(quic_sockets) - else: - secure_sockets = [] - insecure_sockets = self._create_sockets(self.bind) - quic_sockets = [] - return Sockets(secure_sockets, insecure_sockets, quic_sockets) + with contextlib.ExitStack() as cleanup: + + def create(binds: list[str], type_: int = socket.SOCK_STREAM) -> list[socket.socket]: + sockets = self._create_sockets(binds, type_) + for bind, sock in zip(binds, sockets, strict=True): + cleanup.callback(sock.detach if bind.startswith("fd://") else sock.close) + return sockets + + if self.ssl_enabled: + secure_sockets = create(self.bind) + insecure_sockets = create(self.insecure_bind) + quic_sockets = create(self.quic_bind, socket.SOCK_DGRAM) + self._set_quic_addresses(quic_sockets) + else: + secure_sockets = [] + insecure_sockets = create(self.bind) + quic_sockets = [] + result = Sockets(secure_sockets, insecure_sockets, quic_sockets) + cleanup.pop_all() + return result def _set_quic_addresses(self, sockets: list[socket.socket]) -> None: self._quic_addresses = [] @@ -291,66 +301,73 @@ def _set_quic_addresses(self, sockets: list[socket.socket]) -> None: stacklevel=2, ) - def _create_sockets( # noqa: C901, PLR0912 + def _create_sockets( # noqa: C901, PLR0912, PLR0915 self, binds: list[str], type_: int = socket.SOCK_STREAM ) -> list[socket.socket]: sockets: list[socket.socket] = [] - for bind in binds: - binding: Any = None - if bind.startswith("unix:"): - sock = socket.socket(socket.AF_UNIX, type_) - binding = bind[5:] - with contextlib.suppress(FileNotFoundError): - if stat.S_ISSOCK(pathlib.Path(binding).stat().st_mode): - pathlib.Path(binding).unlink() - elif bind.startswith("fd://"): - sock = socket.socket(fileno=int(bind[5:])) - actual_type = sock.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE) - if actual_type != type_: - raise SocketTypeError(type_, actual_type) - else: - bind = bind.replace("[", "").replace("]", "") # noqa: PLW2901 - try: - value = bind.rsplit(":", 1) - host, port = value[0], int(value[1]) - except (ValueError, IndexError): - host, port = bind, 8000 - sock = socket.socket(socket.AF_INET6 if ":" in host else socket.AF_INET, type_) - - if type_ == socket.SOCK_STREAM: - sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) - - if self.workers > 1: - with contextlib.suppress(AttributeError): - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) - binding = (host, port) - - _set_reuse_socket_option(sock) - - if bind.startswith("unix:"): - assert binding is not None - # os.umask changes process-global state, so restore it in a finally: - # a bind() or chown() that raises would otherwise leave the worker's - # umask altered for every later socket and file it creates. - if self.umask is not None: - current_umask = os.umask(self.umask) - try: - sock.bind(binding) - if self.user is not None and self.group is not None: - os.chown(binding, self.user, self.group) # type: ignore[attr-defined] - finally: + with contextlib.ExitStack() as cleanup: + for bind in binds: + binding: Any = None + if bind.startswith("unix:"): + sock = socket.socket(socket.AF_UNIX, type_) + cleanup.callback(sock.close) + binding = bind[5:] + with contextlib.suppress(FileNotFoundError): + if stat.S_ISSOCK(pathlib.Path(binding).stat().st_mode): + pathlib.Path(binding).unlink() + elif bind.startswith("fd://"): + sock = socket.socket(fileno=int(bind[5:])) + # On failure, preserve the caller-owned descriptor. + cleanup.callback(sock.detach) + actual_type = sock.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE) + if actual_type != type_: + raise SocketTypeError(type_, actual_type) + else: + bind = bind.replace("[", "").replace("]", "") # noqa: PLW2901 + try: + value = bind.rsplit(":", 1) + host, port = value[0], int(value[1]) + except (ValueError, IndexError): + host, port = bind, 8000 + sock = socket.socket(socket.AF_INET6 if ":" in host else socket.AF_INET, type_) + + cleanup.callback(sock.close) + + if type_ == socket.SOCK_STREAM: + sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + + if self.workers > 1: + with contextlib.suppress(AttributeError): + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + binding = (host, port) + + _set_reuse_socket_option(sock) + + if bind.startswith("unix:"): + assert binding is not None + # os.umask changes process-global state, so restore it in a finally: + # a bind() or chown() that raises would otherwise leave the worker's + # umask altered for every later socket and file it creates. if self.umask is not None: - os.umask(current_umask) - elif bind.startswith("fd://"): - pass - else: - assert binding is not None - sock.bind(binding) + current_umask = os.umask(self.umask) + try: + sock.bind(binding) + if self.user is not None and self.group is not None: + os.chown(binding, self.user, self.group) # type: ignore[attr-defined] + finally: + if self.umask is not None: + os.umask(current_umask) + elif bind.startswith("fd://"): + pass + else: + assert binding is not None + sock.bind(binding) - sock.setblocking(False) # noqa: FBT003 - with contextlib.suppress(AttributeError): - sock.set_inheritable(True) - sockets.append(sock) + sock.setblocking(False) # noqa: FBT003 + with contextlib.suppress(AttributeError): + sock.set_inheritable(True) + sockets.append(sock) + cleanup.pop_all() return sockets def response_headers(self, protocol: str) -> list[tuple[bytes, bytes]]: diff --git a/tests/test_config.py b/tests/test_config.py index a62c14c..2bfc923 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -256,3 +256,86 @@ def test_response_headers(monkeypatch: MonkeyPatch) -> None: ] config.include_server_header = False assert config.response_headers("test") == [(b"date", b"Sat, 02 Dec 2017 15:43:15 GMT")] + + +@pytest.mark.parametrize("operation", ["setsockopt", "bind", "setblocking", "set_inheritable"]) +@pytest.mark.parametrize("error_type", [OSError, KeyboardInterrupt]) +def test_create_sockets_closes_all_on_failure( + monkeypatch: MonkeyPatch, operation: str, error_type: type[BaseException] +) -> None: + first, failing = Mock(), Mock() + error = error_type("setup failed") + getattr(failing, operation).side_effect = error + monkeypatch.setattr(socket, "socket", Mock(side_effect=[first, failing])) + config = Config() + config.bind = ["127.0.0.1:0", "127.0.0.1:0"] + + with pytest.raises(error_type, match="setup failed") as caught: + config.create_sockets() + + assert caught.value is error + first.close.assert_called_once_with() + failing.close.assert_called_once_with() + + +@pytest.mark.parametrize("failing_group", ["insecure_bind", "quic_bind", "addresses"]) +def test_create_sockets_closes_previous_groups( + monkeypatch: MonkeyPatch, failing_group: str +) -> None: + created: list[Mock] = [] + + def make_socket(*_args: object) -> Mock: + sock = Mock() + created.append(sock) + return sock + + monkeypatch.setattr(socket, "socket", make_socket) + config = Config() + config.certfile = config.keyfile = "unused" + config.bind = config.insecure_bind = config.quic_bind = ["127.0.0.1:0"] + error = OSError("setup failed") + if failing_group == "addresses": + monkeypatch.setattr(config, "_set_quic_addresses", Mock(side_effect=error)) + else: + original = config._create_sockets + + def create(binds: list[str], type_: int) -> list[socket.socket]: + if len(created) == (1 if failing_group == "insecure_bind" else 2): + raise error + return original(binds, type_) + + monkeypatch.setattr(config, "_create_sockets", create) + + with pytest.raises(OSError, match="setup failed"): + config.create_sockets() + + assert created + for sock in created: + sock.close.assert_called_once_with() + + +@pytest.mark.parametrize("failure", ["type", "setup", "later_bind", "later_group"]) +def test_create_sockets_preserves_fd_on_failure(monkeypatch: MonkeyPatch, failure: str) -> None: + inherited, failing = Mock(), Mock() + inherited.getsockopt.return_value = ( + socket.SOCK_DGRAM if failure == "type" else socket.SOCK_STREAM + ) + if failure == "setup": + inherited.setblocking.side_effect = OSError("setup failed") + failing.bind.side_effect = OSError("setup failed") + monkeypatch.setattr(socket, "socket", Mock(side_effect=[inherited, failing])) + config = Config() + config.bind = ["fd://2"] + if failure == "later_bind": + config.bind.append("127.0.0.1:0") + elif failure == "later_group": + config.certfile = config.keyfile = "unused" + config.insecure_bind = ["127.0.0.1:0"] + + with pytest.raises(anycorn.config.SocketTypeError if failure == "type" else OSError): + config.create_sockets() + + inherited.detach.assert_called_once_with() + inherited.close.assert_not_called() + if failure in {"later_bind", "later_group"}: + failing.close.assert_called_once_with()