From 58d969e77ceff0ce910910a21ef9d4eee27a5c3d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 10 Jan 2022 20:31:01 -0700 Subject: [PATCH] gui: Avoid 450ms delay on startup(!) In the case that the published symlink is stale, our default client connection logic was to retry connecting with backoff to give a newly spawned server a chance to startup. In the context of a newly launched gui process checking to see if an existing gui process can serve the same request, we don't need to give it any grace: it will either answer immediately or be deemed not useful. This commit limits us to a single connection attempt in the case where we're not automatically starting the server, which in turn constrains the overhead to something in the order of microseconds rather than nearly 0.5 seconds. While we're in here, I noticed that if we thought we had a socket to try and that failed, we'd always try to publish a new symlink. However, if we failed due to mismatched version info, we shouldn't publish over the top of the already running instance. refs: #1529 --- wezterm-client/src/client.rs | 30 +++++++++++++++++++++--------- wezterm-gui/src/main.rs | 6 ++++-- wezterm/src/main.rs | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/wezterm-client/src/client.rs b/wezterm-client/src/client.rs index 73c1126b0..e2c274453 100644 --- a/wezterm-client/src/client.rs +++ b/wezterm-client/src/client.rs @@ -164,12 +164,19 @@ fn process_unilateral( local_domain_id: Option, decoded: DecodedPdu, ) -> anyhow::Result<()> { - let local_domain_id = local_domain_id.ok_or_else(|| { - anyhow::anyhow!( - "client doesn't have a real local domain, \ - so unilateral message cannot be processed by it" - ) - })?; + let local_domain_id = match local_domain_id { + Some(id) => id, + None => { + // FIXME: We currently get a bunch of these; we'll need + // to do something to advise the server when we want them. + // For now, we just ignore them. + log::trace!( + "client doesn't have a real local domain, \ + so unilateral message cannot be processed by it" + ); + return Ok(()); + } + }; if let Some(pane_id) = decoded.pdu.pane_id() { promise::spawn::spawn_into_main_thread(async move { process_unilateral_inner(pane_id, local_domain_id, decoded) @@ -281,6 +288,7 @@ async fn client_thread_async( pub fn unix_connect_with_retry( target: &UnixTarget, just_spawned: bool, + max_attempts: Option, ) -> anyhow::Result { let mut error = None; @@ -288,7 +296,9 @@ pub fn unix_connect_with_retry( std::thread::sleep(std::time::Duration::from_millis(200)); } - for iter in 0..10 { + let max_attempts = max_attempts.unwrap_or(10); + + for iter in 0..max_attempts { if iter > 0 { std::thread::sleep(std::time::Duration::from_millis(iter * 10)); } @@ -561,7 +571,9 @@ impl Reconnectable { ui.output_str(&format!("Connect to {:?}\n", target)); log::trace!("connect to {:?}", target); - let stream = match unix_connect_with_retry(&target, false) { + let max_attempts = if no_auto_start { Some(1) } else { None }; + + let stream = match unix_connect_with_retry(&target, false, max_attempts) { Ok(stream) => stream, Err(e) => { if no_auto_start || unix_dom.no_serve_automatically || !initial { @@ -603,7 +615,7 @@ impl Reconnectable { } }); - unix_connect_with_retry(&target, true).with_context(|| { + unix_connect_with_retry(&target, true, None).with_context(|| { format!("(after spawning server) failed to connect to {:?}", target) })? } diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index 18568e7b0..98b31c960 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -417,7 +417,7 @@ impl Publish { } pub fn try_spawn( - &self, + &mut self, cmd: Option, config: &ConfigHandle, ) -> anyhow::Result { @@ -437,12 +437,14 @@ impl Publish { let vers = client.verify_version_compat(&mut ui).await?; if vers.executable_path != std::env::current_exe().context("resolve executable path")? { + *self = Publish::NoConnectNoPublish; anyhow::bail!( "Running GUI is a different executable from us, will start a new one"); } if vers.config_file_path != std::env::var_os("WEZTERM_CONFIG_FILE").map(Into::into) { + *self = Publish::NoConnectNoPublish; anyhow::bail!( "Running GUI has different config from us, will start a new one" ); @@ -567,7 +569,7 @@ fn run_terminal_gui(opts: StartCommand) -> anyhow::Result<()> { // First, let's see if we can ask an already running wezterm to do this. // We must do this before we start the gui frontend as the scheduler // requirements are different. - let publish = Publish::resolve(&mux, &config, opts.always_new_process); + let mut publish = Publish::resolve(&mux, &config, opts.always_new_process); log::trace!("{:?}", publish); if publish.try_spawn(cmd.clone(), &config)? { return Ok(()); diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index 9363d35fd..01196b40a 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -578,7 +578,7 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow: Mux::set_mux(&mux); let unix_dom = config.unix_domains.first().unwrap(); let target = unix_dom.target(); - let stream = unix_connect_with_retry(&target, false)?; + let stream = unix_connect_with_retry(&target, false, None)?; // Spawn a thread to pull data from the socket and write // it to stdout