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
subint_spawner_backend
Gud Boi 2026-03-27 11:21:20 -04:00
parent 5f6e45e1d4
commit 490fac432c
2 changed files with 23 additions and 18 deletions

View File

@ -74,10 +74,11 @@ def test_mk_maddr_uds():
multiaddr containing the full socket path. multiaddr containing the full socket path.
''' '''
# NOTE, use a relative `filedir` since the multiaddr # NOTE, use an absolute `filedir` to match real runtime
# parser rejects the double-slash from absolute paths # UDS paths; `mk_maddr()` strips the leading `/` to avoid
# (i.e. `/unix//tmp/..` -> "empty protocol path"). # the double-slash `/unix//run/..` that py-multiaddr
filedir = 'tractor_test' # rejects as "empty protocol path".
filedir = '/tmp/tractor_test'
filename = 'test_sock.sock' filename = 'test_sock.sock'
addr = UDSAddress( addr = UDSAddress(
filedir=filedir, filedir=filedir,
@ -89,12 +90,14 @@ def test_mk_maddr_uds():
result_str: str = str(result) result_str: str = str(result)
assert result_str.startswith('/unix/') 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) sockpath_rel: str = str(
# NOTE, the multiaddr lib prepends a `/` to the Path(filedir) / filename
# unix protocol value when parsing back out. ).lstrip('/')
unix_val: str = result.value_for_protocol('unix') 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(): def test_mk_maddr_unsupported_proto_key():
@ -120,7 +123,7 @@ def test_mk_maddr_unsupported_proto_key():
), ),
pytest.param( pytest.param(
UDSAddress( UDSAddress(
filedir='tractor_rt', filedir='/tmp/tractor_rt',
filename='roundtrip.sock', filename='roundtrip.sock',
), ),
id='uds', id='uds',
@ -181,15 +184,16 @@ def test_parse_maddr_tcp_ipv6():
def test_parse_maddr_uds(): def test_parse_maddr_uds():
''' '''
`parse_maddr()` on a `/unix/...` multiaddr string `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) assert isinstance(result, UDSAddress)
filedir, filename = result.unwrap() filedir, filename = result.unwrap()
assert filename == 'test.sock' assert filename == 'test.sock'
assert 'tractor_test' in str(filedir) assert str(filedir) == '/tmp/tractor_test'
def test_parse_maddr_unsupported(): def test_parse_maddr_unsupported():
@ -214,7 +218,7 @@ def test_parse_maddr_unsupported():
), ),
pytest.param( pytest.param(
UDSAddress( UDSAddress(
filedir='tractor_rt', filedir='/tmp/tractor_rt',
filename='roundtrip.sock', filename='roundtrip.sock',
), ),
id='uds', id='uds',

View File

@ -114,13 +114,14 @@ def parse_maddr(
case ['unix']: case ['unix']:
# NOTE, the multiaddr lib prepends a `/` to the # NOTE, the multiaddr lib prepends a `/` to the
# unix protocol value; strip it to recover the # unix protocol value which effectively restores
# original relative path. # the absolute-path semantics that `mk_maddr()`
# strips when building the multiaddr string.
raw: str = maddr.value_for_protocol('unix') raw: str = maddr.value_for_protocol('unix')
sockpath = Path(raw.lstrip('/')) sockpath = Path(raw)
return UDSAddress( return UDSAddress(
filedir=str(sockpath.parent), filedir=sockpath.parent,
filename=str(sockpath.name), filename=sockpath.name,
) )
case _: case _: