From 490fac432c4cc119d49b9199f8334cd61d2378f7 Mon Sep 17 00:00:00 2001 From: goodboy Date: Fri, 27 Mar 2026 11:21:20 -0400 Subject: [PATCH] Preserve absolute UDS paths in `parse_maddr()` Drop the `.lstrip('/')` on the unix protocol value so the lib-prepended `/` restores the absolute-path semantics that `mk_maddr()` strips when encoding. Pass `Path` components (not `str`) to `UDSAddress`. Also, update all UDS test params to use absolute paths (`/tmp/tractor_test/...`, `/tmp/tractor_rt/...`) matching real runtime sockpath behavior; tighten `test_parse_maddr_uds` to assert exact `filedir`. Review: PR #429 (copilot-pull-request-reviewer[bot]) https://github.com/goodboy/tractor/pull/429#pullrequestreview-4018448152 (this patch was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code --- tests/discovery/test_multiaddr.py | 30 +++++++++++++++++------------- tractor/discovery/_multiaddr.py | 11 ++++++----- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/tests/discovery/test_multiaddr.py b/tests/discovery/test_multiaddr.py index 8329b033..83da84ea 100644 --- a/tests/discovery/test_multiaddr.py +++ b/tests/discovery/test_multiaddr.py @@ -74,10 +74,11 @@ def test_mk_maddr_uds(): multiaddr containing the full socket path. ''' - # NOTE, use a relative `filedir` since the multiaddr - # parser rejects the double-slash from absolute paths - # (i.e. `/unix//tmp/..` -> "empty protocol path"). - filedir = 'tractor_test' + # NOTE, use an absolute `filedir` to match real runtime + # UDS paths; `mk_maddr()` strips the leading `/` to avoid + # the double-slash `/unix//run/..` that py-multiaddr + # rejects as "empty protocol path". + filedir = '/tmp/tractor_test' filename = 'test_sock.sock' addr = UDSAddress( filedir=filedir, @@ -89,12 +90,14 @@ def test_mk_maddr_uds(): result_str: str = str(result) assert result_str.startswith('/unix/') + # verify the leading `/` was stripped to avoid double-slash + assert '/unix/tmp/tractor_test/' in result_str - sockpath: str = str(Path(filedir) / filename) - # NOTE, the multiaddr lib prepends a `/` to the - # unix protocol value when parsing back out. + sockpath_rel: str = str( + Path(filedir) / filename + ).lstrip('/') unix_val: str = result.value_for_protocol('unix') - assert unix_val.endswith(sockpath) + assert unix_val.endswith(sockpath_rel) def test_mk_maddr_unsupported_proto_key(): @@ -120,7 +123,7 @@ def test_mk_maddr_unsupported_proto_key(): ), pytest.param( UDSAddress( - filedir='tractor_rt', + filedir='/tmp/tractor_rt', filename='roundtrip.sock', ), id='uds', @@ -181,15 +184,16 @@ def test_parse_maddr_tcp_ipv6(): def test_parse_maddr_uds(): ''' `parse_maddr()` on a `/unix/...` multiaddr string - produce a `UDSAddress` with the correct dir and filename. + produce a `UDSAddress` with the correct dir and filename, + preserving absolute path semantics. ''' - result = parse_maddr('/unix/tractor_test/test.sock') + result = parse_maddr('/unix/tmp/tractor_test/test.sock') assert isinstance(result, UDSAddress) filedir, filename = result.unwrap() assert filename == 'test.sock' - assert 'tractor_test' in str(filedir) + assert str(filedir) == '/tmp/tractor_test' def test_parse_maddr_unsupported(): @@ -214,7 +218,7 @@ def test_parse_maddr_unsupported(): ), pytest.param( UDSAddress( - filedir='tractor_rt', + filedir='/tmp/tractor_rt', filename='roundtrip.sock', ), id='uds', diff --git a/tractor/discovery/_multiaddr.py b/tractor/discovery/_multiaddr.py index d9046e03..e310cf1c 100644 --- a/tractor/discovery/_multiaddr.py +++ b/tractor/discovery/_multiaddr.py @@ -114,13 +114,14 @@ def parse_maddr( case ['unix']: # NOTE, the multiaddr lib prepends a `/` to the - # unix protocol value; strip it to recover the - # original relative path. + # unix protocol value which effectively restores + # the absolute-path semantics that `mk_maddr()` + # strips when building the multiaddr string. raw: str = maddr.value_for_protocol('unix') - sockpath = Path(raw.lstrip('/')) + sockpath = Path(raw) return UDSAddress( - filedir=str(sockpath.parent), - filename=str(sockpath.name), + filedir=sockpath.parent, + filename=sockpath.name, ) case _: