Skip to content

Commit

Permalink
improve error handling in route
Browse files Browse the repository at this point in the history
  • Loading branch information
naosense committed Oct 19, 2022
1 parent 5d0ff69 commit 2edeeeb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 3 additions & 1 deletion zellij-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use crate::{
wasm_vm::{wasm_thread_main, PluginInstruction},
};
use route::route_thread_main;
use zellij_utils::errors::prelude::Context;
use zellij_utils::{
channels::{self, ChannelWithContext, SenderWithContext},
cli::CliArgs,
Expand Down Expand Up @@ -277,7 +278,8 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
client_id,
)
})
.unwrap(),
.context("Failed to spawn server_router thread")
.fatal(),
);
},
Err(err) => {
Expand Down
19 changes: 9 additions & 10 deletions zellij-server/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ pub(crate) fn route_thread_main(
to_server: SenderWithContext<ServerInstruction>,
mut receiver: IpcReceiverWithContext<ClientToServerMsg>,
client_id: ClientId,
) {
) -> Result<()> {
let mut retry_queue = vec![];
'route_loop: loop {
match receiver.recv() {
Expand All @@ -539,7 +539,7 @@ pub(crate) fn route_thread_main(
let rlocked_sessions = session_data.read().unwrap();
let handle_instruction = |instruction: ClientToServerMsg,
mut retry_queue: Option<&mut Vec<ClientToServerMsg>>|
-> bool {
-> Result<bool> {
let mut should_break = false;
match instruction {
ClientToServerMsg::Action(action, maybe_client_id) => {
Expand All @@ -553,7 +553,7 @@ pub(crate) fn route_thread_main(
if send_res.is_err() {
let _ = to_server
.send(ServerInstruction::RemoveClient(client_id));
return true;
return Ok(true);
}
}
if route_action(
Expand All @@ -562,9 +562,7 @@ pub(crate) fn route_thread_main(
&*os_input,
&to_server,
client_id,
)
.unwrap()
{
)? {
should_break = true;
}
}
Expand Down Expand Up @@ -648,7 +646,7 @@ pub(crate) fn route_thread_main(
// we don't unwrap this because we don't really care if there's an error here (eg.
// if the main server thread exited before this router thread did)
let _ = to_server.send(ServerInstruction::RemoveClient(client_id));
return true;
return Ok(true);
},
ClientToServerMsg::KillSession => {
to_server.send(ServerInstruction::KillSession).unwrap();
Expand All @@ -665,16 +663,16 @@ pub(crate) fn route_thread_main(
let _ = to_server.send(ServerInstruction::ActiveClients(client_id));
},
}
should_break
Ok(should_break)
};
for instruction_to_retry in retry_queue.drain(..) {
log::warn!("Server ready, retrying sending instruction.");
let should_break = handle_instruction(instruction_to_retry, None);
let should_break = handle_instruction(instruction_to_retry, None)?;
if should_break {
break 'route_loop;
}
}
let should_break = handle_instruction(instruction, Some(&mut retry_queue));
let should_break = handle_instruction(instruction, Some(&mut retry_queue))?;
if should_break {
break 'route_loop;
}
Expand All @@ -691,4 +689,5 @@ pub(crate) fn route_thread_main(
},
}
}
Ok(())
}

0 comments on commit 2edeeeb

Please sign in to comment.