diff options
| author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-12 18:46:21 +0530 |
|---|---|---|
| committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-15 22:14:34 +0530 |
| commit | f2c43ac577d9d2a93695564cb3185264e3492a68 (patch) | |
| tree | 47d19cb1e4ada87e1b9b8684f7f6e2637146dadf /src/common | |
| parent | Switch to multiprocess model: Not looking good (diff) | |
| download | sl0ppij-f2c43ac577d9d2a93695564cb3185264e3492a68.tar.gz sl0ppij-f2c43ac577d9d2a93695564cb3185264e3492a68.tar.xz sl0ppij-f2c43ac577d9d2a93695564cb3185264e3492a68.zip | |
Fix memory overflow error and add panic hook for server
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/errors.rs | 31 | ||||
| -rw-r--r-- | src/common/os_input_output.rs | 4 |
2 files changed, 28 insertions, 7 deletions
diff --git a/src/common/errors.rs b/src/common/errors.rs index c2caf19d..c92b9b7f 100644 --- a/src/common/errors.rs +++ b/src/common/errors.rs @@ -19,12 +19,29 @@ const MAX_THREAD_CALL_STACK: usize = 6; use super::thread_bus::SenderWithContext; #[cfg(not(test))] use std::panic::PanicInfo; + +pub trait ErrorInstruction { + fn error(err: String) -> Self; +} + +impl ErrorInstruction for ClientInstruction { + fn error(err: String) -> Self { + ClientInstruction::Error(err) + } +} + +impl ErrorInstruction for ServerInstruction { + fn error(err: String) -> Self { + ServerInstruction::Error(err) + } +} + /// Custom panic handler/hook. Prints the [`ErrorContext`]. #[cfg(not(test))] -pub fn handle_panic( - info: &PanicInfo<'_>, - send_app_instructions: &SenderWithContext<ClientInstruction>, -) { +pub fn handle_panic<T>(info: &PanicInfo<'_>, sender: &SenderWithContext<T>) +where + T: ErrorInstruction + Clone, +{ use backtrace::Backtrace; use std::{process, thread}; let backtrace = Backtrace::new(); @@ -70,7 +87,7 @@ pub fn handle_panic( println!("{}", backtrace); process::exit(1); } else { - let _ = send_app_instructions.send(ClientInstruction::Error(backtrace)); + let _ = sender.send(T::error(backtrace)); } } @@ -333,6 +350,7 @@ pub enum ClientContext { Error, UnblockInputThread, Render, + ServerError, } impl From<&ClientInstruction> for ClientContext { @@ -340,6 +358,7 @@ impl From<&ClientInstruction> for ClientContext { match *client_instruction { ClientInstruction::Exit => ClientContext::Exit, ClientInstruction::Error(_) => ClientContext::Error, + ClientInstruction::ServerError(_) => ClientContext::ServerError, ClientInstruction::Render(_) => ClientContext::Render, ClientInstruction::UnblockInputThread => ClientContext::UnblockInputThread, } @@ -355,6 +374,7 @@ pub enum ServerContext { TerminalResize, UnblockInputThread, ClientExit, + Error, } impl From<&ServerInstruction> for ServerContext { @@ -366,6 +386,7 @@ impl From<&ServerInstruction> for ServerContext { ServerInstruction::Render(_) => ServerContext::Render, ServerInstruction::UnblockInputThread => ServerContext::UnblockInputThread, ServerInstruction::ClientExit => ServerContext::ClientExit, + ServerInstruction::Error(_) => ServerContext::Error, } } } diff --git a/src/common/os_input_output.rs b/src/common/os_input_output.rs index 2a9a14e0..a8d67478 100644 --- a/src/common/os_input_output.rs +++ b/src/common/os_input_output.rs @@ -302,7 +302,7 @@ pub trait ClientOsApi: Send + Sync { fn set_raw_mode(&mut self, fd: RawFd); /// Set the terminal associated to file descriptor `fd` to /// [cooked mode](https://en.wikipedia.org/wiki/Terminal_mode). - fn unset_raw_mode(&mut self, fd: RawFd); + fn unset_raw_mode(&self, fd: RawFd); /// Returns the writer that allows writing to standard output. fn get_stdout_writer(&self) -> Box<dyn io::Write>; /// Returns the raw contents of standard input. @@ -326,7 +326,7 @@ impl ClientOsApi for ClientOsInputOutput { fn set_raw_mode(&mut self, fd: RawFd) { into_raw_mode(fd); } - fn unset_raw_mode(&mut self, fd: RawFd) { + fn unset_raw_mode(&self, fd: RawFd) { let orig_termios = self.orig_termios.lock().unwrap(); unset_raw_mode(fd, orig_termios.clone()); } |
