From cca3a70de4b8939527d08b34d86b19d607ee653a Mon Sep 17 00:00:00 2001 From: goodboy Date: Fri, 14 Aug 2026 09:50:27 -0400 Subject: [PATCH] Add `Address.rebind_from_sockname` opt-out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Gate `Endpoint.start_listener()`s `getsockname()`-vs-`.addr` reconciliation on a new per-addr-type `ClassVar[bool]`, set `True` on both `TCPAddress` and `UDSAddress` so existing behaviour is bit-for-bit unchanged. That reconciliation exists ONLY to learn a kernel-assigned port from a `port=0` tcp bind (its own comment says so). The incoming `tipc` backend (gh #378) has no late-binding analogue AND its `getsockname()` answers a `TIPC_ADDR_ID` port-id rather than the name-seq it published — rebinding from that would swap a dialable service name for an un-dialable, un-reconstructable port id. So opting out is semantically right rather than a hack. (this patch was generated in some part by `claude-code` using `claude-opus-5` (`anthropic`)) --- tractor/discovery/_addr.py | 10 ++++++++++ tractor/ipc/_server.py | 9 +++++++++ tractor/ipc/_tcp.py | 4 ++++ tractor/ipc/_uds.py | 6 ++++++ 4 files changed, 29 insertions(+) diff --git a/tractor/discovery/_addr.py b/tractor/discovery/_addr.py index 2697c5c9..f0d36c2e 100644 --- a/tractor/discovery/_addr.py +++ b/tractor/discovery/_addr.py @@ -82,6 +82,16 @@ UnwrappedAddress = ( class Address(Protocol): proto_key: ClassVar[str] unwrapped_type: ClassVar[UnwrappedAddress] + # whether `.ipc._server.Endpoint.start_listener()` should + # reconcile a bound `.addr` against its listener's + # `socket.getsockname()`. + # + # XXX NOTE, that reconciliation exists ONLY to learn the + # kernel-*assigned* port from a `port=0` tcp bind; a backend + # whose `getsockname()` reports a categorically different thing + # than what was `.bind()`ed must opt out with `False`, else the + # ep's addr gets clobbered by an un-dialable one. + rebind_from_sockname: ClassVar[bool] # TODO, i feel like an `.is_bound()` is a better thing to # support? diff --git a/tractor/ipc/_server.py b/tractor/ipc/_server.py index 31f4c6b0..9570b674 100644 --- a/tractor/ipc/_server.py +++ b/tractor/ipc/_server.py @@ -661,7 +661,16 @@ class Endpoint(Struct): # NOTE, for handling the resolved non-0 port for # TCP/UDP network sockets. + # + # XXX, gated on the addr-type's opt-in since for some + # backends `getsockname()` does NOT answer "the addr you + # bound"; `tipc` reports a `TIPC_ADDR_ID` port-id instead + # of the published name-seq, so rebinding from it would + # replace a dialable service-name with an un-dialable + # (and un-reconstructable) port-id. if ( + self.addr.rebind_from_sockname + and (unwrapped := lstnr.socket.getsockname()) != self.addr.unwrap() diff --git a/tractor/ipc/_tcp.py b/tractor/ipc/_tcp.py index 293ae4be..3180b411 100644 --- a/tractor/ipc/_tcp.py +++ b/tractor/ipc/_tcp.py @@ -65,6 +65,10 @@ class TCPAddress( unwrapped_type: ClassVar[type] = tuple[str, int] def_bindspace: ClassVar[str] = '127.0.0.1' + # XXX, REQUIRED here since a `port=0` bind means the kernel + # picks and `getsockname()` is the only way we learn it. + rebind_from_sockname: ClassVar[bool] = True + # ?TODO, actually validate ipv4/6 with stdlib's `ipaddress` @property def is_valid(self) -> bool: diff --git a/tractor/ipc/_uds.py b/tractor/ipc/_uds.py index d1e9d2f6..bd1a44dd 100644 --- a/tractor/ipc/_uds.py +++ b/tractor/ipc/_uds.py @@ -117,6 +117,12 @@ class UDSAddress( unwrapped_type: ClassVar[type] = tuple[str, int] def_bindspace: ClassVar[Path] = get_rt_dir() + # NOTE, `getsockname()` answers the sock-file path as a `str` + # which never `==` our 2-tuple `.unwrap()`, so the round-trip + # always fires; it's a no-op modulo `.maybe_pid` and is kept + # `True` to preserve pre-existing behaviour exactly. + rebind_from_sockname: ClassVar[bool] = True + @property def bindspace(self) -> Path: '''