Skip to content

feat: report the port a server is listening on - #6

Merged
sash-a merged 4 commits into
mainfrom
feat/server-port-getter
Aug 29, 2026
Merged

sash-a merged 4 commits into
mainfrom
feat/server-port-getter

Conversation

@sash-a

@sash-a sash-a commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Problem

TcpTransport takes a port and tells nobody what it did with it — the field is private on a frozen pyclass, and _Server never exposes its transport. So TcpTransport(port=0) bound an ephemeral port that nothing could discover.

Change

server = Server(example, batch_size=32, transport=TcpTransport(port=0))
server.start()
print(server.port)  # 42201 — publish this to your clients

Server.port raises RuntimeError before start() and without a transport. Transport::port on the Rust trait defaults to None for transports that don't listen on a TCP port.

The one non-obvious part: the listener now binds in start() instead of in the accept task it spawns. The bound address doesn't exist until something calls local_addr, and nothing could from inside a task that had already been handed to the runtime. Moving the bind is what makes the port knowable at all.

One behaviour change falls out of that: a failed bind (port in use) now raises from start(). It previously only reached stderr, with start() returning successfully on a server that was never listening.

`TcpTransport` took a port and told nobody what it did with it, so
`port=0` bound an ephemeral port that nothing could discover.

`Server.port` now reports it. To make the assigned port knowable, the
listener binds in `start()` rather than in the accept task it spawns —
the bound address doesn't exist until something calls `local_addr`, and
nothing could, from inside a task that had already been handed off.
@sash-a
sash-a force-pushed the feat/server-port-getter branch from 9391489 to 842deb1 Compare August 29, 2026 09:25
Comment thread python/echo/server.py Outdated
Comment thread src/transport/tcp.rs Outdated
Comment thread src/transport/mod.rs Outdated
Comment thread src/transport/tcp.rs Outdated
Comment thread src/transport/tcp.rs Outdated
@sash-a
sash-a merged commit ae3b061 into main Aug 29, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant