Skip to content

Commit

Permalink
Check for errors when connecting to client
Browse files Browse the repository at this point in the history
The unwrap() causes a panic.  This can happen if the file doesn't exist or there is a small chance when mutliple clients connect quickly that the pipe returns 'All pipe instances are busy.' also causing a panic.  This allows the caller to handle these errors and retry if necessary (as is the case with the pipe instances being busy).

Signed-off-by: James Sturtevant <jstur@microsoft.com>
  • Loading branch information
jsturtevant committed Apr 12, 2023
1 parent 1851aa0 commit 26c9c2f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::result;
use thiserror::Error;

/// The error type for ttrpc.
#[derive(Error, Debug, Clone)]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum Error {
#[error("socket err: {0}")]
Socket(String),
Expand Down
30 changes: 27 additions & 3 deletions src/sync/sys/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,14 @@ impl ClientConnection {
opts.read(true)
.write(true)
.custom_flags(FILE_FLAG_OVERLAPPED);
let file = opts.open(self.address.as_str());

return PipeConnection::new(file.unwrap().into_raw_handle() as isize)
match opts.open(self.address.as_str()) {
Ok(file) => {
return PipeConnection::new(file.into_raw_handle() as isize)
}
Err(e) => {
return Err(Error::Windows(e.raw_os_error().unwrap()))
}
}
}

pub fn close_receiver(&self) -> Result<()> {
Expand All @@ -336,3 +341,22 @@ impl ClientConnection {
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;
use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND;

#[test]
fn test_pipe_connection() {
let client = ClientConnection::new("non_existent_pipe");
match client.get_pipe_connection() {
Ok(_) => {
assert!(false, "should not be able to get a connection to a non existent pipe");
}
Err(e) => {
assert_eq!(e, Error::Windows(ERROR_FILE_NOT_FOUND as i32)); // 2 is th
}
}
}
}

0 comments on commit 26c9c2f

Please # to comment.