fix(threads): a race condition, build-times, and warnings (#79)

* Fix a race condition, build-times, and warnings

* Yeah, yeah... I know

* style(code): add comment to explain race condition

Co-authored-by: Aram Drevekenin <aram@poor.dev>
This commit is contained in:
Brooks Rady 2020-12-02 14:03:46 +00:00 committed by GitHub
parent c47fed927e
commit c3fa9fb40e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 22 deletions

View file

@ -20,12 +20,21 @@ bincode = "1.3.1"
structopt = "0.3" structopt = "0.3"
serde_yaml = "0.8" serde_yaml = "0.8"
serde_json = "1.0" serde_json = "1.0"
wasmer = { git = "https://github.com/wasmerio/wasmer.git" }
wasmer-wasi = { git = "https://github.com/wasmerio/wasmer.git" }
[dependencies.async-std] [dependencies.async-std]
version = "1.3.0" version = "1.3.0"
features = ["unstable"] features = ["unstable"]
[dependencies.wasmer]
git = "https://github.com/wasmerio/wasmer.git"
optional = true
[dependencies.wasmer-wasi]
git = "https://github.com/wasmerio/wasmer.git"
optional = true
[features]
wasm-wip = ["wasmer", "wasmer-wasi"]
[dev-dependencies] [dev-dependencies]
insta = "0.16.1" insta = "0.16.1"

View file

@ -11,19 +11,15 @@ mod screen;
mod terminal_pane; mod terminal_pane;
mod utils; mod utils;
use std::io::{self, Read, Write}; use std::io::Write;
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use structopt::StructOpt; use structopt::StructOpt;
use wasmer::{Exports, Function, Instance, Module, Store, Value};
use wasmer_wasi::WasiState;
use crate::command_is_executing::CommandIsExecuting; use crate::command_is_executing::CommandIsExecuting;
use crate::input::input_loop; use crate::input::input_loop;
use crate::layout::Layout; use crate::layout::Layout;
@ -257,13 +253,17 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
// Here be dragons! This is very much a work in progress, and isn't quite functional // Here be dragons! This is very much a work in progress, and isn't quite functional
// yet. It's being left out of the tests because is slows them down massively (by // yet. It's being left out of the tests because is slows them down massively (by
// recompiling a WASM module for every single test). Stay tuned for more updates! // recompiling a WASM module for every single test). Stay tuned for more updates!
#[cfg(not(test))] #[cfg(feature = "wasm-wip")]
active_threads.push( active_threads.push(
thread::Builder::new() thread::Builder::new()
.name("wasm".to_string()) .name("wasm".to_string())
.spawn(move || { .spawn(move || {
// TODO: Clone shared state here // TODO: Clone shared state here
move || -> Result<(), Box<dyn std::error::Error>> { move || -> Result<(), Box<dyn std::error::Error>> {
use std::io;
use std::sync::{Arc, Mutex};
use wasmer::{Exports, Function, Instance, Module, Store, Value};
use wasmer_wasi::WasiState;
let store = Store::default(); let store = Store::default();
println!("Compiling module..."); println!("Compiling module...");
@ -364,6 +364,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
let _ipc_thread = thread::Builder::new() let _ipc_thread = thread::Builder::new()
.name("ipc_server".to_string()) .name("ipc_server".to_string())
.spawn({ .spawn({
use std::io::Read;
let send_pty_instructions = send_pty_instructions.clone(); let send_pty_instructions = send_pty_instructions.clone();
let send_screen_instructions = send_screen_instructions.clone(); let send_screen_instructions = send_screen_instructions.clone();
move || { move || {

View file

@ -11,7 +11,7 @@ use std::path::PathBuf;
use crate::layout::Layout; use crate::layout::Layout;
use crate::os_input_output::OsApi; use crate::os_input_output::OsApi;
use crate::utils::logging::{debug_log_to_file, debug_to_file}; use crate::utils::logging::debug_to_file;
use crate::ScreenInstruction; use crate::ScreenInstruction;
pub struct ReadFromPid { pub struct ReadFromPid {

View file

@ -380,9 +380,15 @@ impl Screen {
} }
} }
pub fn handle_pty_event(&mut self, pid: RawFd, event: VteEvent) { pub fn handle_pty_event(&mut self, pid: RawFd, event: VteEvent) {
let terminal_output = self.terminals.get_mut(&pid).unwrap(); // if we don't have the terminal in self.terminals it's probably because
// of a race condition where the terminal was created in pty_bus but has not
// yet been created in Screen. These events are currently not buffered, so
// if you're debugging seemingly randomly missing stdout data, this is
// the reason
if let Some(terminal_output) = self.terminals.get_mut(&pid) {
terminal_output.handle_event(event); terminal_output.handle_event(event);
} }
}
pub fn write_to_active_terminal(&mut self, mut bytes: Vec<u8>) { pub fn write_to_active_terminal(&mut self, mut bytes: Vec<u8>) {
if let Some(active_terminal_id) = &self.get_active_terminal_id() { if let Some(active_terminal_id) = &self.get_active_terminal_id() {
self.os_api self.os_api

View file

@ -23,14 +23,9 @@ pub fn atomic_create_dir(dir_name: &str) -> io::Result<()> {
} }
} }
pub fn debug_log_to_file(message: String) -> io::Result<()> { pub fn debug_log_to_file(mut message: String) -> io::Result<()> {
atomic_create_file(MOSAIC_TMP_LOG_FILE); message.push('\n');
let mut file = fs::OpenOptions::new() debug_log_to_file_without_newline(message)
.append(true)
.create(true)
.open(MOSAIC_TMP_LOG_FILE)?;
file.write_all(message.as_bytes())?;
file.write_all(b"\n")
} }
pub fn debug_log_to_file_without_newline(message: String) -> io::Result<()> { pub fn debug_log_to_file_without_newline(message: String) -> io::Result<()> {
@ -39,9 +34,7 @@ pub fn debug_log_to_file_without_newline(message: String) -> io::Result<()> {
.append(true) .append(true)
.create(true) .create(true)
.open(MOSAIC_TMP_LOG_FILE)?; .open(MOSAIC_TMP_LOG_FILE)?;
file.write_all(message.as_bytes())?; file.write_all(message.as_bytes())
Ok(())
} }
pub fn debug_log_to_file_pid_0(message: String, pid: RawFd) -> io::Result<()> { pub fn debug_log_to_file_pid_0(message: String, pid: RawFd) -> io::Result<()> {