@@ -132,28 +132,44 @@ mod tests {
132132 ) ;
133133 }
134134
135- /// Test cross-platform signal handling (works on all platforms)
135+ /// Test cross-platform signal handling with proper error handling (works on all platforms)
136136 #[ tokio:: test]
137137 async fn test_cross_platform_signal_handling ( ) {
138- // Test the pattern used in main.rs that works on all platforms
138+ // Test the new safer pattern used in main.rs that works on all platforms
139139 let shutdown_signal = async {
140140 #[ cfg( unix) ]
141- let mut sigterm = signal:: unix:: signal ( signal:: unix:: SignalKind :: terminate ( ) )
142- . expect ( "Failed to install SIGTERM handler" ) ;
141+ {
142+ // Test proper error handling for SIGTERM
143+ let sigterm_result = signal:: unix:: signal ( signal:: unix:: SignalKind :: terminate ( ) ) ;
144+ match sigterm_result {
145+ Ok ( mut sigterm) => {
146+ tokio:: select! {
147+ _ = sigterm. recv( ) => {
148+ println!( "=== TEST: SIGTERM received ===" ) ;
149+ }
150+ _ = signal:: ctrl_c( ) => {
151+ println!( "=== TEST: Ctrl+C received ===" ) ;
152+ }
153+ }
154+ }
155+ Err ( err) => {
156+ println ! ( "TEST: Failed to install SIGTERM handler: {}, falling back to SIGINT only" , err) ;
157+ // Fall back to just handling SIGINT/Ctrl+C
158+ if let Err ( ctrl_c_err) = signal:: ctrl_c ( ) . await {
159+ println ! ( "TEST: Failed to wait for Ctrl+C: {}" , ctrl_c_err) ;
160+ } else {
161+ println ! ( "=== TEST: Ctrl+C received ===" ) ;
162+ }
163+ }
164+ }
165+ }
143166
144167 #[ cfg( not( unix) ) ]
145- let sigterm = std:: future:: pending :: < ( ) > ( ) ; // Never resolves on non-Unix
146-
147- tokio:: select! {
148- #[ cfg( unix) ]
149- _ = sigterm. recv( ) => {
150- println!( "=== TEST: SIGTERM received ===" ) ;
151- }
152- #[ cfg( not( unix) ) ]
153- _ = sigterm => {
154- unreachable!( "SIGTERM handler should never resolve on non-Unix systems" ) ;
155- }
156- _ = signal:: ctrl_c( ) => {
168+ {
169+ // On non-Unix systems, only handle Ctrl+C - no unreachable!() panic
170+ if let Err ( err) = signal:: ctrl_c ( ) . await {
171+ println ! ( "TEST: Failed to wait for Ctrl+C: {}" , err) ;
172+ } else {
157173 println ! ( "=== TEST: Ctrl+C received ===" ) ;
158174 }
159175 }
@@ -168,4 +184,50 @@ mod tests {
168184 "Signal handlers should timeout when no signals are sent"
169185 ) ;
170186 }
187+
188+ /// Test that signal handler setup gracefully handles errors
189+ #[ tokio:: test]
190+ #[ cfg( unix) ]
191+ async fn test_signal_handler_error_handling ( ) {
192+ // Test that our error handling pattern works correctly
193+ let sigterm_result = signal:: unix:: signal ( signal:: unix:: SignalKind :: terminate ( ) ) ;
194+
195+ // This should succeed in normal circumstances
196+ match sigterm_result {
197+ Ok ( _sigterm) => {
198+ println ! ( "=== TEST: SIGTERM handler created successfully ===" ) ;
199+ // Success case - handler was created
200+ assert ! ( true , "SIGTERM handler should be created successfully" ) ;
201+ }
202+ Err ( err) => {
203+ println ! ( "TEST: SIGTERM handler creation failed: {}" , err) ;
204+ // Error case - should not panic, just log and continue
205+ // This tests that our error handling is robust
206+ assert ! ( true , "Error handling should not panic" ) ;
207+ }
208+ }
209+ }
210+
211+ /// Test that non-Unix systems handle signals gracefully without panicking
212+ #[ tokio:: test]
213+ #[ cfg( not( unix) ) ]
214+ async fn test_non_unix_signal_handling ( ) {
215+ // Test that non-Unix systems can handle Ctrl+C without any unreachable!() panics
216+ let shutdown_signal = async {
217+ if let Err ( err) = signal:: ctrl_c ( ) . await {
218+ println ! ( "TEST: Failed to wait for Ctrl+C: {}" , err) ;
219+ } else {
220+ println ! ( "=== TEST: Ctrl+C received on non-Unix system ===" ) ;
221+ }
222+ } ;
223+
224+ // Use a very short timeout since we're not actually sending signals
225+ let result = timeout ( Duration :: from_millis ( 10 ) , shutdown_signal) . await ;
226+
227+ // The timeout should occur since we're not actually sending signals
228+ assert ! (
229+ result. is_err( ) ,
230+ "Signal handler should timeout when no signal is sent"
231+ ) ;
232+ }
171233}
0 commit comments