fix(main): do not hang on exit (#150)

This commit is contained in:
Aram Drevekenin 2021-01-20 19:25:35 +01:00 committed by GitHub
parent 81af57b15d
commit decc38232b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 305 additions and 316 deletions

View File

@ -173,7 +173,6 @@ pub enum AppInstruction {
pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let mut app_state = AppState::default();
let mut active_threads = vec![];
let command_is_executing = CommandIsExecuting::new();
@ -225,8 +224,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
})
});
active_threads.push(
thread::Builder::new()
let pty_thread = thread::Builder::new()
.name("pty".to_string())
.spawn({
let mut command_is_executing = command_is_executing.clone();
@ -285,11 +283,9 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
}
}
})
.unwrap(),
);
.unwrap();
active_threads.push(
thread::Builder::new()
let screen_thread = thread::Builder::new()
.name("screen".to_string())
.spawn({
let mut command_is_executing = command_is_executing.clone();
@ -430,11 +426,9 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
}
}
})
.unwrap(),
);
.unwrap();
active_threads.push(
thread::Builder::new()
let wasm_thread = thread::Builder::new()
.name("wasm".to_string())
.spawn({
let mut send_pty_instructions = send_pty_instructions.clone();
@ -460,12 +454,8 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let plugin_dir = project_dirs.data_dir().join("plugins/");
let wasm_bytes = fs::read(&path)
.or_else(|_| fs::read(&path.with_extension("wasm")))
.or_else(|_| {
fs::read(&plugin_dir.join(&path).with_extension("wasm"))
})
.unwrap_or_else(|_| {
panic!("cannot find plugin {}", &path.display())
});
.or_else(|_| fs::read(&plugin_dir.join(&path).with_extension("wasm")))
.unwrap_or_else(|_| panic!("cannot find plugin {}", &path.display()));
// FIXME: Cache this compiled module on disk. I could use `(de)serialize_to_file()` for that
let module = Module::new(&store, &wasm_bytes).unwrap();
@ -498,8 +488,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
};
let mosaic = mosaic_imports(&store, &plugin_env);
let instance =
Instance::new(&module, &mosaic.chain_back(wasi)).unwrap();
let instance = Instance::new(&module, &mosaic.chain_back(wasi)).unwrap();
let start = instance.exports.get_function("_start").unwrap();
@ -560,10 +549,9 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
}
}
})
.unwrap(),
);
.unwrap();
// TODO: currently we don't push this into active_threads
// TODO: currently we don't wait for this to quit
// because otherwise the app will hang. Need to fix this so it both
// listens to the ipc-bus and is able to quit cleanly
#[cfg(not(test))]
@ -663,8 +651,11 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
}
AppInstruction::Error(backtrace) => {
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
let _ = screen_thread.join();
let _ = send_pty_instructions.send(PtyInstruction::Quit);
let _ = pty_thread.join();
let _ = send_plugin_instructions.send(PluginInstruction::Quit);
let _ = wasm_thread.join();
os_input.unset_raw_mode(0);
let goto_start_of_last_line = format!("\u{1b}[{};{}H", full_screen_ws.rows, 1);
let error = format!("{}\n{}", goto_start_of_last_line, backtrace);
@ -672,17 +663,17 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
.get_stdout_writer()
.write(error.as_bytes())
.unwrap();
for thread_handler in active_threads {
let _ = thread_handler.join();
}
std::process::exit(1);
}
}
}
for thread_handler in active_threads {
thread_handler.join().unwrap();
}
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
screen_thread.join().unwrap();
let _ = send_pty_instructions.send(PtyInstruction::Quit);
pty_thread.join().unwrap();
let _ = send_plugin_instructions.send(PluginInstruction::Quit);
wasm_thread.join().unwrap();
// cleanup();
let reset_style = "\u{1b}[m";

View File

@ -146,7 +146,9 @@ impl Screen {
.unwrap();
if self.tabs.is_empty() {
self.active_tab_index = None;
self.render();
self.send_app_instructions
.send(AppInstruction::Exit)
.unwrap();
}
}
pub fn render(&mut self) {
@ -156,10 +158,6 @@ impl Screen {
} else {
self.close_tab();
}
} else {
self.send_app_instructions
.send(AppInstruction::Exit)
.unwrap();
};
}