Skip to content

Commit 1e0bca2

Browse files
committed
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]) #429 (review) (this patch was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code
1 parent 3a7154b commit 1e0bca2

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

tests/discovery/test_multiaddr.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ def test_mk_maddr_uds():
7474
multiaddr containing the full socket path.
7575
7676
'''
77-
# NOTE, use a relative `filedir` since the multiaddr
78-
# parser rejects the double-slash from absolute paths
79-
# (i.e. `/unix//tmp/..` -> "empty protocol path").
80-
filedir = 'tractor_test'
77+
# NOTE, use an absolute `filedir` to match real runtime
78+
# UDS paths; `mk_maddr()` strips the leading `/` to avoid
79+
# the double-slash `/unix//run/..` that py-multiaddr
80+
# rejects as "empty protocol path".
81+
filedir = '/tmp/tractor_test'
8182
filename = 'test_sock.sock'
8283
addr = UDSAddress(
8384
filedir=filedir,
@@ -89,12 +90,14 @@ def test_mk_maddr_uds():
8990

9091
result_str: str = str(result)
9192
assert result_str.startswith('/unix/')
93+
# verify the leading `/` was stripped to avoid double-slash
94+
assert '/unix/tmp/tractor_test/' in result_str
9295

93-
sockpath: str = str(Path(filedir) / filename)
94-
# NOTE, the multiaddr lib prepends a `/` to the
95-
# unix protocol value when parsing back out.
96+
sockpath_rel: str = str(
97+
Path(filedir) / filename
98+
).lstrip('/')
9699
unix_val: str = result.value_for_protocol('unix')
97-
assert unix_val.endswith(sockpath)
100+
assert unix_val.endswith(sockpath_rel)
98101

99102

100103
def test_mk_maddr_unsupported_proto_key():
@@ -120,7 +123,7 @@ def test_mk_maddr_unsupported_proto_key():
120123
),
121124
pytest.param(
122125
UDSAddress(
123-
filedir='tractor_rt',
126+
filedir='/tmp/tractor_rt',
124127
filename='roundtrip.sock',
125128
),
126129
id='uds',
@@ -181,15 +184,16 @@ def test_parse_maddr_tcp_ipv6():
181184
def test_parse_maddr_uds():
182185
'''
183186
`parse_maddr()` on a `/unix/...` multiaddr string
184-
produce a `UDSAddress` with the correct dir and filename.
187+
produce a `UDSAddress` with the correct dir and filename,
188+
preserving absolute path semantics.
185189
186190
'''
187-
result = parse_maddr('/unix/tractor_test/test.sock')
191+
result = parse_maddr('/unix/tmp/tractor_test/test.sock')
188192

189193
assert isinstance(result, UDSAddress)
190194
filedir, filename = result.unwrap()
191195
assert filename == 'test.sock'
192-
assert 'tractor_test' in str(filedir)
196+
assert str(filedir) == '/tmp/tractor_test'
193197

194198

195199
def test_parse_maddr_unsupported():
@@ -214,7 +218,7 @@ def test_parse_maddr_unsupported():
214218
),
215219
pytest.param(
216220
UDSAddress(
217-
filedir='tractor_rt',
221+
filedir='/tmp/tractor_rt',
218222
filename='roundtrip.sock',
219223
),
220224
id='uds',

tractor/discovery/_multiaddr.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ def parse_maddr(
114114

115115
case ['unix']:
116116
# NOTE, the multiaddr lib prepends a `/` to the
117-
# unix protocol value; strip it to recover the
118-
# original relative path.
117+
# unix protocol value which effectively restores
118+
# the absolute-path semantics that `mk_maddr()`
119+
# strips when building the multiaddr string.
119120
raw: str = maddr.value_for_protocol('unix')
120-
sockpath = Path(raw.lstrip('/'))
121+
sockpath = Path(raw)
121122
return UDSAddress(
122-
filedir=str(sockpath.parent),
123-
filename=str(sockpath.name),
123+
filedir=sockpath.parent,
124+
filename=sockpath.name,
124125
)
125126

126127
case _:

0 commit comments

Comments
 (0)