@@ -100,12 +100,8 @@ impl PipeListener {
100
100
trace ! ( "listening for connection" ) ;
101
101
let result = unsafe { ConnectNamedPipe ( np. named_pipe , ol. as_mut_ptr ( ) ) } ;
102
102
if result != 0 {
103
- if self . shutting_down . load ( Ordering :: SeqCst ) {
104
- np. close ( ) . unwrap_or_else ( |err| trace ! ( "Failed to close the pipe {:?}" , err) ) ;
105
- return Err ( io:: Error :: new (
106
- io:: ErrorKind :: Other ,
107
- "closing pipe" ,
108
- ) ) ;
103
+ if let Some ( error) = self . handle_shutdown ( & np) {
104
+ return Err ( error) ;
109
105
}
110
106
return Err ( io:: Error :: last_os_error ( ) ) ;
111
107
}
@@ -119,24 +115,16 @@ impl PipeListener {
119
115
return Err ( io:: Error :: last_os_error ( ) ) ;
120
116
}
121
117
_ => {
122
- if self . shutting_down . load ( Ordering :: SeqCst ) {
123
- np. close ( ) . unwrap_or_else ( |err| trace ! ( "Failed to close the pipe {:?}" , err) ) ;
124
- return Err ( io:: Error :: new (
125
- io:: ErrorKind :: Other ,
126
- "closing pipe" ,
127
- ) ) ;
118
+ if let Some ( shutdown_signal) = self . handle_shutdown ( & np) {
119
+ return Err ( shutdown_signal) ;
128
120
}
129
121
Ok ( Some ( np) )
130
122
}
131
123
}
132
124
}
133
125
e if e. raw_os_error ( ) == Some ( ERROR_PIPE_CONNECTED as i32 ) => {
134
- if self . shutting_down . load ( Ordering :: SeqCst ) {
135
- np. close ( ) . unwrap_or_else ( |err| trace ! ( "Failed to close the pipe {:?}" , err) ) ;
136
- return Err ( io:: Error :: new (
137
- io:: ErrorKind :: Other ,
138
- "closing pipe" ,
139
- ) ) ;
126
+ if let Some ( error) = self . handle_shutdown ( & np) {
127
+ return Err ( error) ;
140
128
}
141
129
Ok ( Some ( np) )
142
130
}
@@ -149,6 +137,17 @@ impl PipeListener {
149
137
}
150
138
}
151
139
140
+ fn handle_shutdown ( & self , np : & PipeConnection ) -> Option < io:: Error > {
141
+ if self . shutting_down . load ( Ordering :: SeqCst ) {
142
+ np. close ( ) . unwrap_or_else ( |err| trace ! ( "Failed to close the pipe {:?}" , err) ) ;
143
+ return Some ( io:: Error :: new (
144
+ io:: ErrorKind :: Other ,
145
+ "closing pipe" ,
146
+ ) ) ;
147
+ }
148
+ None
149
+ }
150
+
152
151
fn new_instance ( & self ) -> io:: Result < isize > {
153
152
let name = OsStr :: new ( & self . address . as_str ( ) )
154
153
. encode_wide ( )
@@ -386,7 +385,8 @@ mod test {
386
385
387
386
#[ test]
388
387
fn should_accept_new_client ( ) {
389
- let listener = Arc :: new ( PipeListener :: new ( r"\\.\pipe\ttrpc-test-accept" ) . unwrap ( ) ) ;
388
+ let address = r"\\.\pipe\ttrpc-test-accept" ;
389
+ let listener = Arc :: new ( PipeListener :: new ( address) . unwrap ( ) ) ;
390
390
391
391
let listener_server = listener. clone ( ) ;
392
392
let thread = std:: thread:: spawn ( move || {
@@ -404,7 +404,7 @@ mod test {
404
404
}
405
405
} ) ;
406
406
407
- wait_socket_working ( r"\\.\pipe\ttrpc-test-accept" , 10 , 5 ) . unwrap ( ) ;
407
+ wait_socket_working ( address , 10 , 5 ) . unwrap ( ) ;
408
408
thread. join ( ) . unwrap ( ) ;
409
409
}
410
410
0 commit comments