Skip to content

vision-demo: a late close no longer drops the robot's redialled stream - #433

Open
ericdelorefice wants to merge 1 commit into
pollen-robotics:mainfrom
ericdelorefice:fix/receiver-late-close
Open

ericdelorefice wants to merge 1 commit into
pollen-robotics:mainfrom
ericdelorefice:fix/receiver-late-close

Conversation

@ericdelorefice

Copy link
Copy Markdown

Problem

spaces/vision-demo/receiver.py files each robot's stream under (account, robot name), and the /frames route's finally removed that key when its socket closed. A redial into a Space that restarted already works, as the comment on the hello says. The case this fixes is a redial into a Space that is still up: if the robot has already reconnected when the old socket's teardown runs, the key belongs to the new connection, and the old teardown removes the live stream. Frames keep arriving into a Stream nothing can see, and the page says "no stream yet" until the robot happens to reconnect again.

Two ways the old close can come second, from reading mediad/src/stream.rs. I have no robot, so neither is observed:

  • A second media.stream replaces the first (pressing start streaming again, for example to change the rate). Streamer::start spawns the new pump straight away, while the old pump closes its socket only after its encoder thread next checks running, up to about one frame interval at the old rate (1 s at 1 fps). The new connection needs a handshake and one whoami-v2 call before its hello.
  • A link that drops without a close: the robot redials after its 2 s backoff, while this end only learns the old socket is gone when a read on it fails or times out.

The same finally fell back to the name "a duck" for a connection that never registered anything, so such a connection could remove a nameless robot's live stream.

Reproduction

test_receiver.py (new) drives the real /frames route with FastAPI's test client. whoami is replaced so a made-up token resolves to an account, and the order is forced: the old connection says hello and sends a frame, a second connection for the same robot says hello, the old one closes, the new one sends a frame. On main the account is left with no stream:

AssertionError: the old connection's close dropped the live stream: {}

Change

Frames.closed takes the stream and removes it only if it is still the one registered under its key, and the route calls it only when its connection registered a stream. +12/-3 in receiver.py. Intended behaviour change: an old connection's close leaves a newer stream for the same robot in place. Each connection still removes its own stream when it ends.

Testing

  • test_receiver.py, four tests, written like vetbook/test_traits.py: python test_receiver.py or pytest. The test client needs httpx, which the Space does not install.
    • main: 2 failed (the late close, and the connection that never said hello), 2 passed (a closed stream is forgotten, closing one duck leaves the other)
    • this branch: 4 passed, 10 runs in a row
  • macOS, Python 3.12, fastapi 0.159.1, starlette 1.9.0, numpy 2.5.4, opencv-python-headless 5.0.0, av 19.0.2. Nothing on the Rust side changed, so I did not run cargo test.

The receiver removed a stream by (account, robot name) when its socket
closed. A robot can redial before this end sees the previous connection
go - a second media.stream, whose old socket closes only when its
encoder thread next looks, or a link that dropped without a close - so
the old socket's teardown could run after the new one's hello and take
the live stream with it. The page then said "no stream yet" while frames
kept arriving.

Frames.closed now takes the stream and removes it only if it is still
the one registered under its key, and a connection that registered
nothing removes nothing (it used to fall back to the name "a duck").
test_receiver.py drives the real /frames route with FastAPI's test
client.

Assisted-by: Claude:claude-opus-5-5

This branch has not been deployed

No deployments
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