Skip to content

Commit

Permalink
Don't automatically fall back to automatic port if web socket port is…
Browse files Browse the repository at this point in the history
… already in use, only recommend using 0 instead (#6296)

### What

* Fixes #6222

Running this scrip twice:
```py
#!/usr/bin/env python3
import rerun as rr
import time

def main() -> None:
    rr.init("rerun_example_serve")
    rr.serve(web_port=57158, ws_port=57159)

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("Ctrl-C received. Exiting.")

if __name__ == "__main__":
    main()
```

Gives now this error:

```sh
Traceback (most recent call last):
  File "/Users/andreas/dev/rerun-io/rerun/test.py", line 18, in <module>
    main()
  File "/Users/andreas/dev/rerun-io/rerun/test.py", line 8, in main
    rr.serve(web_port=57158, ws_port=57159)
  File "/Users/andreas/dev/rerun-io/rerun/rerun_py/rerun_sdk/rerun/sinks.py", line 252, in serve
    bindings.serve(
RuntimeError: Failed to bind to WebSocket port 57159 since the address is already in use. Use port 0 to let the OS choose a free port.
```

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6296?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6296?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/6296)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
  • Loading branch information
Wumpf authored May 13, 2024
1 parent 3a42904 commit de3078c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
3 changes: 3 additions & 0 deletions crates/re_ws_comms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum RerunServerError {
#[error("Failed to bind to WebSocket port {0}: {1}")]
BindFailed(RerunServerPort, std::io::Error),

#[error("Failed to bind to WebSocket port {0} since the address is already in use. Use port 0 to let the OS choose a free port.")]
BindFailedAddrInUse(RerunServerPort),

#[error("Received an invalid message")]
InvalidMessagePrefix,

Expand Down
15 changes: 6 additions & 9 deletions crates/re_ws_comms/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,13 @@ impl RerunServer {
) -> Result<Self, RerunServerError> {
let bind_addr = format!("{bind_ip}:{port}");

let listener_socket = match TcpListener::bind(bind_addr) {
Ok(listener) => listener,
Err(err) if err.kind() == std::io::ErrorKind::AddrInUse => {
let bind_addr = format!("{bind_ip}:0");

TcpListener::bind(bind_addr)
.map_err(|err| RerunServerError::BindFailed(RerunServerPort(0), err))?
let listener_socket = TcpListener::bind(bind_addr).map_err(|err| {
if err.kind() == std::io::ErrorKind::AddrInUse {
RerunServerError::BindFailedAddrInUse(port)
} else {
RerunServerError::BindFailed(port, err)
}
Err(err) => return Err(RerunServerError::BindFailed(port, err)),
};
})?;

// Blocking listener socket seems much easier at first glance:
// No polling needed and as such no extra libraries!
Expand Down

0 comments on commit de3078c

Please sign in to comment.