fix(main): do not hang on exit (#150)
This commit is contained in:
parent
81af57b15d
commit
decc38232b
2 changed files with 305 additions and 316 deletions
47
src/main.rs
47
src/main.rs
|
|
@ -173,7 +173,6 @@ pub enum AppInstruction {
|
||||||
|
|
||||||
pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
|
pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
|
||||||
let mut app_state = AppState::default();
|
let mut app_state = AppState::default();
|
||||||
let mut active_threads = vec![];
|
|
||||||
|
|
||||||
let command_is_executing = CommandIsExecuting::new();
|
let command_is_executing = CommandIsExecuting::new();
|
||||||
|
|
||||||
|
|
@ -225,8 +224,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
active_threads.push(
|
let pty_thread = thread::Builder::new()
|
||||||
thread::Builder::new()
|
|
||||||
.name("pty".to_string())
|
.name("pty".to_string())
|
||||||
.spawn({
|
.spawn({
|
||||||
let mut command_is_executing = command_is_executing.clone();
|
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(
|
let screen_thread = thread::Builder::new()
|
||||||
thread::Builder::new()
|
|
||||||
.name("screen".to_string())
|
.name("screen".to_string())
|
||||||
.spawn({
|
.spawn({
|
||||||
let mut command_is_executing = command_is_executing.clone();
|
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(
|
let wasm_thread = thread::Builder::new()
|
||||||
thread::Builder::new()
|
|
||||||
.name("wasm".to_string())
|
.name("wasm".to_string())
|
||||||
.spawn({
|
.spawn({
|
||||||
let mut send_pty_instructions = send_pty_instructions.clone();
|
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 plugin_dir = project_dirs.data_dir().join("plugins/");
|
||||||
let wasm_bytes = fs::read(&path)
|
let wasm_bytes = fs::read(&path)
|
||||||
.or_else(|_| fs::read(&path.with_extension("wasm")))
|
.or_else(|_| fs::read(&path.with_extension("wasm")))
|
||||||
.or_else(|_| {
|
.or_else(|_| fs::read(&plugin_dir.join(&path).with_extension("wasm")))
|
||||||
fs::read(&plugin_dir.join(&path).with_extension("wasm"))
|
.unwrap_or_else(|_| panic!("cannot find plugin {}", &path.display()));
|
||||||
})
|
|
||||||
.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
|
// FIXME: Cache this compiled module on disk. I could use `(de)serialize_to_file()` for that
|
||||||
let module = Module::new(&store, &wasm_bytes).unwrap();
|
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 mosaic = mosaic_imports(&store, &plugin_env);
|
||||||
let instance =
|
let instance = Instance::new(&module, &mosaic.chain_back(wasi)).unwrap();
|
||||||
Instance::new(&module, &mosaic.chain_back(wasi)).unwrap();
|
|
||||||
|
|
||||||
let start = instance.exports.get_function("_start").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
|
// because otherwise the app will hang. Need to fix this so it both
|
||||||
// listens to the ipc-bus and is able to quit cleanly
|
// listens to the ipc-bus and is able to quit cleanly
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
|
|
@ -663,8 +651,11 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
|
||||||
}
|
}
|
||||||
AppInstruction::Error(backtrace) => {
|
AppInstruction::Error(backtrace) => {
|
||||||
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
|
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
|
||||||
|
let _ = screen_thread.join();
|
||||||
let _ = send_pty_instructions.send(PtyInstruction::Quit);
|
let _ = send_pty_instructions.send(PtyInstruction::Quit);
|
||||||
|
let _ = pty_thread.join();
|
||||||
let _ = send_plugin_instructions.send(PluginInstruction::Quit);
|
let _ = send_plugin_instructions.send(PluginInstruction::Quit);
|
||||||
|
let _ = wasm_thread.join();
|
||||||
os_input.unset_raw_mode(0);
|
os_input.unset_raw_mode(0);
|
||||||
let goto_start_of_last_line = format!("\u{1b}[{};{}H", full_screen_ws.rows, 1);
|
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);
|
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()
|
.get_stdout_writer()
|
||||||
.write(error.as_bytes())
|
.write(error.as_bytes())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
for thread_handler in active_threads {
|
|
||||||
let _ = thread_handler.join();
|
|
||||||
}
|
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for thread_handler in active_threads {
|
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
|
||||||
thread_handler.join().unwrap();
|
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();
|
// cleanup();
|
||||||
let reset_style = "\u{1b}[m";
|
let reset_style = "\u{1b}[m";
|
||||||
|
|
|
||||||
|
|
@ -146,7 +146,9 @@ impl Screen {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
if self.tabs.is_empty() {
|
if self.tabs.is_empty() {
|
||||||
self.active_tab_index = None;
|
self.active_tab_index = None;
|
||||||
self.render();
|
self.send_app_instructions
|
||||||
|
.send(AppInstruction::Exit)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn render(&mut self) {
|
pub fn render(&mut self) {
|
||||||
|
|
@ -156,10 +158,6 @@ impl Screen {
|
||||||
} else {
|
} else {
|
||||||
self.close_tab();
|
self.close_tab();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
self.send_app_instructions
|
|
||||||
.send(AppInstruction::Exit)
|
|
||||||
.unwrap();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue