Skip to content

Commit

Permalink
Put open::that inside spawn_blocking to launch the browser
Browse files Browse the repository at this point in the history
`open::that` doews occasionally block. One example is when launching a
fresh instance of firefox on linux.

Another alternative is `open::that_detached`, but that reports a success
even if the browser exited with an error.
  • Loading branch information
ilyagr committed Mar 10, 2024
1 parent b06d1eb commit d81e03c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions backend-local-server/src/bin/diffedit3-web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ pub struct LocalServerCli {
/// Do not try to open the browser automatically
///
/// See https://crates.io/crates/open for a brief description of how the
/// default browser is chosen. The `BROWSER` environment variable may be
/// considered by `xdg-open` and similar commands.
/// default browser is chosen. The `BROWSER` environment variable is not
/// respected, unfortunately.
// TODO(ilyagr): One way to respect the BROWSER environment variable might
// be to use the `webbrowser` crate to get the browser command. It'd be
// better to use something else to actually launch the browser, as
// `webbrowser::open` has limited error handlind, like
// `open::that_detached`.
#[arg(long, short = 'N')]
no_browser: bool,
/// Make the server print debugging information
Expand Down
19 changes: 12 additions & 7 deletions backend-local-server/src/local_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,19 @@ pub async fn run_server(
// using `port` is to find out what port number the OS picked if `cli.port==0`.
let socket_addr = acceptor_to_socket_address(&acceptor)?;
// Now that the acceptor exists, the browser should be able to connect IIUC.
let http_address = format!("http://{socket_addr}");
eprintln!("Listening at {http_address}.");
eprintln!("Listening at {socket_addr}.");
if open_browser {
eprint!("Trying to launch a browser at {http_address}...");
match open::that(&http_address) {
Ok(_) => eprintln!(" Success!"),
Err(err) => eprintln!("\nFailed to launch a browser: {err}"),
}
let http_address = format!("http://{socket_addr}");
tokio::task::spawn_blocking(move || {
// Use `spawn_blocking` since `webbrowser::open` may block (for text-mode
// browsers. TODO: find out if it blocks when running a fresh instance of
// `firefox` on Linux.)
eprintln!("Trying to launch a browser at {http_address}...");
match open::that(&http_address) {
Ok(_) => eprintln!("Successfully launched browser."),
Err(err) => eprintln!("Failed to launch a browser: {err}"),
}
});
}

let mut result = Ok(());
Expand Down

0 comments on commit d81e03c

Please sign in to comment.