Skip to content

Commit d96bea3

Browse files
committed
Check for errors when connecting to client
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 <[email protected]>
1 parent 1851aa0 commit d96bea3

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::result;
1919
use thiserror::Error;
2020

2121
/// The error type for ttrpc.
22-
#[derive(Error, Debug, Clone)]
22+
#[derive(Error, Debug, Clone, PartialEq)]
2323
pub enum Error {
2424
#[error("socket err: {0}")]
2525
Socket(String),

src/sync/sys/windows/net.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,14 @@ impl ClientConnection {
321321
opts.read(true)
322322
.write(true)
323323
.custom_flags(FILE_FLAG_OVERLAPPED);
324-
let file = opts.open(self.address.as_str());
325-
326-
return PipeConnection::new(file.unwrap().into_raw_handle() as isize)
324+
match opts.open(self.address.as_str()) {
325+
Ok(file) => {
326+
return PipeConnection::new(file.into_raw_handle() as isize)
327+
}
328+
Err(e) => {
329+
return Err(Error::Windows(e.raw_os_error().unwrap()))
330+
}
331+
}
327332
}
328333

329334
pub fn close_receiver(&self) -> Result<()> {
@@ -336,3 +341,22 @@ impl ClientConnection {
336341
Ok(())
337342
}
338343
}
344+
345+
#[cfg(test)]
346+
mod test {
347+
use super::*;
348+
use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND;
349+
350+
#[test]
351+
fn test_pipe_connection() {
352+
let client = ClientConnection::new("non_existent_pipe");
353+
match client.get_pipe_connection() {
354+
Ok(_) => {
355+
assert!(false, "should not be able to get a connection to a non existent pipe");
356+
}
357+
Err(e) => {
358+
assert_eq!(e, Error::Windows(ERROR_FILE_NOT_FOUND as i32));
359+
}
360+
}
361+
}
362+
}

0 commit comments

Comments
 (0)