fix(update): restart no longer freezes the window or crashes on the locked database
- Swap and start of the new exe run off the UI thread; starting the 100 MB exe blocked the window for ~15 s while Windows scanned it, so clicks did nothing - The new exe is read once when the update is found, so the scan is done before the click - Status bar shows "Restarting..." and ignores further clicks - The new process waits for the old one to exit and retries opening the cache database; it crashed with "disk I/O error" when the old one still held it - Quit ends the process with TerminateProcess after a 3 s grace instead of unloading every DLL Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -48,6 +48,7 @@ windows = { version = "0.62", features = [
48
48
"Win32_Storage_FileSystem",
49
49
"Win32_System_Com",
50
50
"Win32_System_Pipes",
51
"Win32_System_Threading",
51
52
"Win32_UI_HiDpi",
52
53
"Win32_UI_Shell",
53
54
"Win32_UI_WindowsAndMessaging",
@@ -48,6 +48,7 @@ windows = { version = "0.62", features = [48
"Win32_Storage_FileSystem",48
"Win32_Storage_FileSystem",49
"Win32_System_Com",49
"Win32_System_Com",50
"Win32_System_Pipes",50
"Win32_System_Pipes",51
"Win32_System_Threading",51
"Win32_UI_HiDpi",52
"Win32_UI_HiDpi",52
"Win32_UI_Shell",53
"Win32_UI_Shell",53
"Win32_UI_WindowsAndMessaging",54
"Win32_UI_WindowsAndMessaging",@@ -167,11 +167,28 @@ fn transport(endpoint: Option<&str>, database: Option<&std::path::Path>) -> Arc<
167
167
))
168
168
}
169
169
170
const STORE_OPEN_ATTEMPTS: u32 = 20;
171
const STORE_OPEN_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500);
172
173
fn open_store(path: &std::path::Path) -> Store {
174
for _ in 1..STORE_OPEN_ATTEMPTS {
175
match Store::open(path) {
176
Ok(store) => return store,
177
Err(_) => std::thread::sleep(STORE_OPEN_INTERVAL),
178
}
179
}
180
Store::open(path).expect("cache database")
181
}
182
170
183
fn main() {
171
184
crash_log::install(data_path(crash_log::CRASH_LOG_FILE));
172
185
let arguments = parse_arguments();
173
186
if !arguments.demo && arguments.database.is_none() {
174
let relaunched = std::env::var_os(updater::RELAUNCH_ENV).is_some();
187
let relaunch_value = std::env::var(updater::RELAUNCH_ENV).ok();
188
if let Some(value) = &relaunch_value {
189
updater::wait_for_previous_exit(value);
190
}
191
let relaunched = relaunch_value.is_some();
175
192
if single_instance::claim(SINGLE_INSTANCE_NAME, relaunched)
176
193
== single_instance::Claim::Forwarded
177
194
{
@@ -194,7 +211,7 @@ fn main() {
194
211
.database
195
212
.clone()
196
213
.unwrap_or_else(|| data_path(DATABASE_FILE));
197
Store::open(&path).expect("cache database")
214
open_store(&path)
198
215
};
199
216
let store = Arc::new(store);
200
217
let receiver = (!arguments.demo)
@@ -212,6 +229,11 @@ fn main() {
212
229
.with_assets(assets::AppAssets)
213
230
.run(move |cx| {
214
231
gpui_kit::init(cx);
232
cx.on_app_quit(|_| {
233
updater::exit_after_grace();
234
async {}
235
})
236
.detach();
215
237
theme::load_fonts(cx);
216
238
theme::apply(cx);
217
239
bind_keys(cx);
@@ -264,4 +286,6 @@ fn main() {
264
286
.detach();
265
287
cx.activate(true);
266
288
});
289
#[cfg(windows)]
290
updater::exit_now();
267
291
}
@@ -167,11 +167,28 @@ fn transport(endpoint: Option<&str>, database: Option<&std::path::Path>) -> Arc<167
))167
))168
}168
}169
169
170
const STORE_OPEN_ATTEMPTS: u32 = 20;171
const STORE_OPEN_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500);172
173
fn open_store(path: &std::path::Path) -> Store {174
for _ in 1..STORE_OPEN_ATTEMPTS {175
match Store::open(path) {176
Ok(store) => return store,177
Err(_) => std::thread::sleep(STORE_OPEN_INTERVAL),178
}179
}180
Store::open(path).expect("cache database")181
}182
170
fn main() {183
fn main() {171
crash_log::install(data_path(crash_log::CRASH_LOG_FILE));184
crash_log::install(data_path(crash_log::CRASH_LOG_FILE));172
let arguments = parse_arguments();185
let arguments = parse_arguments();173
if !arguments.demo && arguments.database.is_none() {186
if !arguments.demo && arguments.database.is_none() {174
let relaunched = std::env::var_os(updater::RELAUNCH_ENV).is_some();187
let relaunch_value = std::env::var(updater::RELAUNCH_ENV).ok();188
if let Some(value) = &relaunch_value {189
updater::wait_for_previous_exit(value);190
}191
let relaunched = relaunch_value.is_some();175
if single_instance::claim(SINGLE_INSTANCE_NAME, relaunched)192
if single_instance::claim(SINGLE_INSTANCE_NAME, relaunched)176
== single_instance::Claim::Forwarded193
== single_instance::Claim::Forwarded177
{194
{@@ -194,7 +211,7 @@ fn main() {194
.database211
.database195
.clone()212
.clone()196
.unwrap_or_else(|| data_path(DATABASE_FILE));213
.unwrap_or_else(|| data_path(DATABASE_FILE));197
Store::open(&path).expect("cache database")214
open_store(&path)198
};215
};199
let store = Arc::new(store);216
let store = Arc::new(store);200
let receiver = (!arguments.demo)217
let receiver = (!arguments.demo)@@ -212,6 +229,11 @@ fn main() {212
.with_assets(assets::AppAssets)229
.with_assets(assets::AppAssets)213
.run(move |cx| {230
.run(move |cx| {214
gpui_kit::init(cx);231
gpui_kit::init(cx);232
cx.on_app_quit(|_| {233
updater::exit_after_grace();234
async {}235
})236
.detach();215
theme::load_fonts(cx);237
theme::load_fonts(cx);216
theme::apply(cx);238
theme::apply(cx);217
bind_keys(cx);239
bind_keys(cx);@@ -264,4 +286,6 @@ fn main() {264
.detach();286
.detach();265
cx.activate(true);287
cx.activate(true);266
});288
});289
#[cfg(windows)]290
updater::exit_now();267
}291
}@@ -15,6 +15,8 @@ const VERSION_FILE_NAME: &str = "version.txt";
15
15
pub const RELAUNCH_ENV: &str = "RUSTY_TEAMS_RELAUNCH";
16
16
const CLEANUP_ATTEMPTS: u32 = 30;
17
17
const CLEANUP_INTERVAL: Duration = Duration::from_secs(1);
18
const QUIT_GRACE: Duration = Duration::from_secs(3);
19
const PREVIOUS_EXIT_TIMEOUT: Duration = Duration::from_secs(30);
18
20
19
21
pub fn running_version() -> &'static str {
20
22
VERSION_MARKER.trim_start_matches("teams-build:")
@@ -24,6 +26,7 @@ pub fn running_version() -> &'static str {
24
26
pub enum UpdateStatus {
25
27
UpToDate,
26
28
Ready,
29
Installing,
27
30
Failed(String),
28
31
}
29
32
@@ -166,7 +169,7 @@ pub fn install_and_relaunch(paths: &UpdatePaths) -> Result<(), String> {
166
169
let completed = apply_plan(&files, &swap_plan(paths))?;
167
170
match Command::new(&paths.exe)
168
171
.args(std::env::args_os().skip(1))
169
.env(RELAUNCH_ENV, "1")
172
.env(RELAUNCH_ENV, std::process::id().to_string())
170
173
.spawn()
171
174
{
172
175
Ok(_) => Ok(()),
@@ -177,6 +180,69 @@ pub fn install_and_relaunch(paths: &UpdatePaths) -> Result<(), String> {
177
180
}
178
181
}
179
182
183
pub fn previous_process_id(relaunch_value: &str) -> Option<u32> {
184
relaunch_value.trim().parse().ok()
185
}
186
187
pub fn wait_for_previous_exit(relaunch_value: &str) {
188
if let Some(process_id) = previous_process_id(relaunch_value) {
189
platform::wait_for_exit(process_id, PREVIOUS_EXIT_TIMEOUT);
190
}
191
}
192
193
pub fn exit_after_grace() {
194
std::thread::spawn(|| {
195
std::thread::sleep(QUIT_GRACE);
196
exit_now();
197
});
198
}
199
200
/// Skips DLL unload, which takes seconds with WebRTC and WebView2 loaded and keeps the database locked.
201
pub fn exit_now() -> ! {
202
platform::terminate_current();
203
std::process::exit(0)
204
}
205
206
#[cfg(windows)]
207
mod platform {
208
use std::time::Duration;
209
210
use windows::Win32::Foundation::CloseHandle;
211
use windows::Win32::System::Threading::{
212
GetCurrentProcess, OpenProcess, PROCESS_SYNCHRONIZE, TerminateProcess, WaitForSingleObject,
213
};
214
215
pub fn terminate_current() {
216
let _ = unsafe { TerminateProcess(GetCurrentProcess(), 0) };
217
}
218
219
pub fn wait_for_exit(process_id: u32, timeout: Duration) {
220
let Ok(handle) = (unsafe { OpenProcess(PROCESS_SYNCHRONIZE, false, process_id) }) else {
221
return;
222
};
223
unsafe {
224
WaitForSingleObject(handle, timeout.as_millis() as u32);
225
let _ = CloseHandle(handle);
226
}
227
}
228
}
229
230
#[cfg(not(windows))]
231
mod platform {
232
use std::time::Duration;
233
234
pub fn wait_for_exit(_process_id: u32, _timeout: Duration) {}
235
236
pub fn terminate_current() {}
237
}
238
239
/// Reading the new exe once lets the virus scanner finish before the restart, which otherwise blocks process start for seconds.
240
pub fn prescan(update_exe: &Path) {
241
if let Ok(mut file) = std::fs::File::open(update_exe) {
242
let _ = io::copy(&mut file, &mut io::sink());
243
}
244
}
245
180
246
pub fn clean_up_old_binary() {
181
247
let Some(paths) = current_paths() else {
182
248
return;
@@ -320,6 +386,12 @@ mod tests {
320
386
assert!(paths.version_file.exists());
321
387
}
322
388
389
#[test]
390
fn relaunch_value_carries_the_previous_process_id() {
391
assert_eq!(previous_process_id("4242"), Some(4242));
392
assert_eq!(previous_process_id(""), None);
393
}
394
323
395
#[test]
324
396
fn running_version_has_no_marker_prefix() {
325
397
assert!(!running_version().starts_with("teams-build:"));
@@ -15,6 +15,8 @@ const VERSION_FILE_NAME: &str = "version.txt";15
pub const RELAUNCH_ENV: &str = "RUSTY_TEAMS_RELAUNCH";15
pub const RELAUNCH_ENV: &str = "RUSTY_TEAMS_RELAUNCH";16
const CLEANUP_ATTEMPTS: u32 = 30;16
const CLEANUP_ATTEMPTS: u32 = 30;17
const CLEANUP_INTERVAL: Duration = Duration::from_secs(1);17
const CLEANUP_INTERVAL: Duration = Duration::from_secs(1);18
const QUIT_GRACE: Duration = Duration::from_secs(3);19
const PREVIOUS_EXIT_TIMEOUT: Duration = Duration::from_secs(30);18
20
19
pub fn running_version() -> &'static str {21
pub fn running_version() -> &'static str {20
VERSION_MARKER.trim_start_matches("teams-build:")22
VERSION_MARKER.trim_start_matches("teams-build:")@@ -24,6 +26,7 @@ pub fn running_version() -> &'static str {24
pub enum UpdateStatus {26
pub enum UpdateStatus {25
UpToDate,27
UpToDate,26
Ready,28
Ready,29
Installing,27
Failed(String),30
Failed(String),28
}31
}29
32
@@ -166,7 +169,7 @@ pub fn install_and_relaunch(paths: &UpdatePaths) -> Result<(), String> {166
let completed = apply_plan(&files, &swap_plan(paths))?;169
let completed = apply_plan(&files, &swap_plan(paths))?;167
match Command::new(&paths.exe)170
match Command::new(&paths.exe)168
.args(std::env::args_os().skip(1))171
.args(std::env::args_os().skip(1))169
.env(RELAUNCH_ENV, "1")172
.env(RELAUNCH_ENV, std::process::id().to_string())170
.spawn()173
.spawn()171
{174
{172
Ok(_) => Ok(()),175
Ok(_) => Ok(()),@@ -177,6 +180,69 @@ pub fn install_and_relaunch(paths: &UpdatePaths) -> Result<(), String> {177
}180
}178
}181
}179
182
183
pub fn previous_process_id(relaunch_value: &str) -> Option<u32> {184
relaunch_value.trim().parse().ok()185
}186
187
pub fn wait_for_previous_exit(relaunch_value: &str) {188
if let Some(process_id) = previous_process_id(relaunch_value) {189
platform::wait_for_exit(process_id, PREVIOUS_EXIT_TIMEOUT);190
}191
}192
193
pub fn exit_after_grace() {194
std::thread::spawn(|| {195
std::thread::sleep(QUIT_GRACE);196
exit_now();197
});198
}199
200
/// Skips DLL unload, which takes seconds with WebRTC and WebView2 loaded and keeps the database locked.201
pub fn exit_now() -> ! {202
platform::terminate_current();203
std::process::exit(0)204
}205
206
#[cfg(windows)]207
mod platform {208
use std::time::Duration;209
210
use windows::Win32::Foundation::CloseHandle;211
use windows::Win32::System::Threading::{212
GetCurrentProcess, OpenProcess, PROCESS_SYNCHRONIZE, TerminateProcess, WaitForSingleObject,213
};214
215
pub fn terminate_current() {216
let _ = unsafe { TerminateProcess(GetCurrentProcess(), 0) };217
}218
219
pub fn wait_for_exit(process_id: u32, timeout: Duration) {220
let Ok(handle) = (unsafe { OpenProcess(PROCESS_SYNCHRONIZE, false, process_id) }) else {221
return;222
};223
unsafe {224
WaitForSingleObject(handle, timeout.as_millis() as u32);225
let _ = CloseHandle(handle);226
}227
}228
}229
230
#[cfg(not(windows))]231
mod platform {232
use std::time::Duration;233
234
pub fn wait_for_exit(_process_id: u32, _timeout: Duration) {}235
236
pub fn terminate_current() {}237
}238
239
/// Reading the new exe once lets the virus scanner finish before the restart, which otherwise blocks process start for seconds.240
pub fn prescan(update_exe: &Path) {241
if let Ok(mut file) = std::fs::File::open(update_exe) {242
let _ = io::copy(&mut file, &mut io::sink());243
}244
}245
180
pub fn clean_up_old_binary() {246
pub fn clean_up_old_binary() {181
let Some(paths) = current_paths() else {247
let Some(paths) = current_paths() else {182
return;248
return;@@ -320,6 +386,12 @@ mod tests {320
assert!(paths.version_file.exists());386
assert!(paths.version_file.exists());321
}387
}322
388
389
#[test]390
fn relaunch_value_carries_the_previous_process_id() {391
assert_eq!(previous_process_id("4242"), Some(4242));392
assert_eq!(previous_process_id(""), None);393
}394
323
#[test]395
#[test]324
fn running_version_has_no_marker_prefix() {396
fn running_version_has_no_marker_prefix() {325
assert!(!running_version().starts_with("teams-build:"));397
assert!(!running_version().starts_with("teams-build:"));@@ -189,7 +189,7 @@ impl AppShell {
189
189
190
190
#[cfg_attr(not(windows), allow(dead_code))]
191
191
fn poll_update(&mut self, window: &mut Window, cx: &mut Context<Self>) {
192
if matches!(self.update, UpdateStatus::Failed(_)) {
192
if matches!(self.update, UpdateStatus::Failed(_) | UpdateStatus::Installing) {
193
193
return;
194
194
}
195
195
let Some(paths) = updater::current_paths() else {
@@ -200,6 +200,9 @@ impl AppShell {
200
200
return;
201
201
}
202
202
self.update = UpdateStatus::Ready;
203
let update_exe = paths.update_exe.clone();
204
cx.background_spawn(async move { updater::prescan(&update_exe) })
205
.detach();
203
206
}
204
207
let conversation = self.conversation.read(cx);
205
208
let idle = updater::is_idle(IdleInputs {
@@ -214,14 +217,27 @@ impl AppShell {
214
217
}
215
218
216
219
fn restart_into_update(&mut self, cx: &mut Context<Self>) {
220
if self.update == UpdateStatus::Installing {
221
return;
222
}
217
223
let Some(paths) = updater::current_paths() else {
218
224
return;
219
225
};
220
match updater::install_and_relaunch(&paths) {
221
Ok(()) => cx.quit(),
222
Err(message) => self.update = UpdateStatus::Failed(message),
223
}
226
self.update = UpdateStatus::Installing;
224
227
cx.notify();
228
cx.spawn(async move |this, cx| {
229
let result = cx
230
.background_spawn(async move { updater::install_and_relaunch(&paths) })
231
.await;
232
let _ = this.update(cx, |this, cx| {
233
match result {
234
Ok(()) => cx.quit(),
235
Err(message) => this.update = UpdateStatus::Failed(message),
236
}
237
cx.notify();
238
});
239
})
240
.detach();
225
241
}
226
242
227
243
fn apply_open_target(&mut self, cx: &mut Context<Self>) {
@@ -189,7 +189,7 @@ impl AppShell {189
189
190
#[cfg_attr(not(windows), allow(dead_code))]190
#[cfg_attr(not(windows), allow(dead_code))]191
fn poll_update(&mut self, window: &mut Window, cx: &mut Context<Self>) {191
fn poll_update(&mut self, window: &mut Window, cx: &mut Context<Self>) {192
if matches!(self.update, UpdateStatus::Failed(_)) {192
if matches!(self.update, UpdateStatus::Failed(_) | UpdateStatus::Installing) {193
return;193
return;194
}194
}195
let Some(paths) = updater::current_paths() else {195
let Some(paths) = updater::current_paths() else {@@ -200,6 +200,9 @@ impl AppShell {200
return;200
return;201
}201
}202
self.update = UpdateStatus::Ready;202
self.update = UpdateStatus::Ready;203
let update_exe = paths.update_exe.clone();204
cx.background_spawn(async move { updater::prescan(&update_exe) })205
.detach();203
}206
}204
let conversation = self.conversation.read(cx);207
let conversation = self.conversation.read(cx);205
let idle = updater::is_idle(IdleInputs {208
let idle = updater::is_idle(IdleInputs {@@ -214,14 +217,27 @@ impl AppShell {214
}217
}215
218
216
fn restart_into_update(&mut self, cx: &mut Context<Self>) {219
fn restart_into_update(&mut self, cx: &mut Context<Self>) {220
if self.update == UpdateStatus::Installing {221
return;222
}217
let Some(paths) = updater::current_paths() else {223
let Some(paths) = updater::current_paths() else {218
return;224
return;219
};225
};220
match updater::install_and_relaunch(&paths) {226
self.update = UpdateStatus::Installing;221
Ok(()) => cx.quit(),222
Err(message) => self.update = UpdateStatus::Failed(message),223
}224
cx.notify();227
cx.notify();228
cx.spawn(async move |this, cx| {229
let result = cx230
.background_spawn(async move { updater::install_and_relaunch(&paths) })231
.await;232
let _ = this.update(cx, |this, cx| {233
match result {234
Ok(()) => cx.quit(),235
Err(message) => this.update = UpdateStatus::Failed(message),236
}237
cx.notify();238
});239
})240
.detach();225
}241
}226
242
227
fn apply_open_target(&mut self, cx: &mut Context<Self>) {243
fn apply_open_target(&mut self, cx: &mut Context<Self>) {@@ -108,6 +108,9 @@ pub fn render_status_bar(
108
108
.on_click(on_restart),
109
109
)
110
110
}
111
UpdateStatus::Installing => {
112
bar = bar.child(div().text_color(theme::accent_text()).child("Restarting..."))
113
}
111
114
UpdateStatus::Failed(message) => {
112
115
bar = bar.child(
113
116
div()
@@ -108,6 +108,9 @@ pub fn render_status_bar(108
.on_click(on_restart),108
.on_click(on_restart),109
)109
)110
}110
}111
UpdateStatus::Installing => {112
bar = bar.child(div().text_color(theme::accent_text()).child("Restarting..."))113
}111
UpdateStatus::Failed(message) => {114
UpdateStatus::Failed(message) => {112
bar = bar.child(115
bar = bar.child(113
div()116
div()