proc-daemon 1.0.0

A foundational framework for building high-performance, resilient daemon services in Rust. Handles all the boilerplate for signal handling, graceful shutdown, logging, and configuration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//! Cross-platform signal handling for graceful daemon shutdown.
//!
//! This module provides a unified interface for handling shutdown signals
//! across different platforms, abstracting away the platform-specific
//! differences between Unix signals and Windows console events.

use std::sync::atomic::{AtomicBool, Ordering};
#[allow(unused_imports)]
use tracing::{debug, info, warn};

use crate::ShutdownReason;

// No need for these imports
// use crate::config::Config;
// use crate::daemon::Daemon;
use crate::error::{Error, Result};
use crate::shutdown::ShutdownCoordinator;

/// Cross-platform signal handler that coordinates shutdown.
#[derive(Debug)]
pub struct SignalHandler {
    #[allow(dead_code)]
    shutdown_coordinator: ShutdownCoordinator,
    handling_signals: AtomicBool,
}

impl SignalHandler {
    /// Create a new signal handler.
    #[must_use]
    pub const fn new(shutdown_coordinator: ShutdownCoordinator) -> Self {
        Self {
            shutdown_coordinator,
            handling_signals: AtomicBool::new(false),
        }
    }

    /// Start handling signals for graceful shutdown.
    /// This will register signal handlers and wait for shutdown signals.
    ///
    /// # Errors
    ///
    /// Returns an error if signal handling is already active or if there's a problem
    /// registering signal handlers on the platform.
    pub async fn handle_signals(&self) -> Result<()> {
        if self
            .handling_signals
            .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
            .is_err()
        {
            return Err(Error::invalid_state("Signal handling already started"));
        }

        info!("Starting signal handler");

        // Platform-specific signal handling
        #[cfg(unix)]
        {
            return self.handle_unix_signals().await;
        }

        #[cfg(windows)]
        {
            return self.handle_windows_signals().await;
        }
    }

    /// Stop signal handling.
    pub fn stop(&self) {
        self.handling_signals.store(false, Ordering::Release);
        debug!("Signal handling stopped");
    }

    /// Check if signal handling is active.
    pub fn is_handling(&self) -> bool {
        self.handling_signals.load(Ordering::Acquire)
    }
}

// Unix-specific signal handling implementation
#[cfg(unix)]
impl SignalHandler {
    async fn handle_unix_signals(&self) -> Result<()> {
        #[cfg(all(feature = "tokio", not(feature = "async-std")))]
        {
            return self.handle_unix_signals_tokio().await;
        }

        #[cfg(all(feature = "async-std", not(feature = "tokio")))]
        {
            return self.handle_unix_signals_async_std().await;
        }

        #[cfg(not(any(feature = "tokio", feature = "async-std")))]
        {
            return Err(Error::runtime_with_code(
                crate::error::ErrorCode::MissingRuntime,
                "No runtime available for signal handling",
            ));
        }

        #[cfg(all(feature = "tokio", feature = "async-std"))]
        {
            // Default to tokio when both are present
            return self.handle_unix_signals_tokio().await;
        }
    }

    #[cfg(feature = "tokio")]
    async fn handle_unix_signals_tokio(&self) -> Result<()> {
        use tokio::signal::unix::{signal, SignalKind};

        // Set up signal handlers for graceful shutdown
        let mut sigterm = signal(SignalKind::terminate()).map_err(|e| {
            Error::signal_with_number(format!("Failed to register SIGTERM handler: {e}"), 15)
        })?;

        let mut sigint = signal(SignalKind::interrupt()).map_err(|e| {
            Error::signal_with_number(format!("Failed to register SIGINT handler: {e}"), 2)
        })?;

        let mut sigquit = signal(SignalKind::quit()).map_err(|e| {
            Error::signal_with_number(format!("Failed to register SIGQUIT handler: {e}"), 3)
        })?;

        let mut sighup = signal(SignalKind::hangup()).map_err(|e| {
            Error::signal_with_number(format!("Failed to register SIGHUP handler: {e}"), 1)
        })?;

        info!("Unix signal handlers registered (SIGTERM, SIGINT, SIGQUIT, SIGHUP)");

        loop {
            tokio::select! {
                _ = sigterm.recv() => {
                    info!("Received SIGTERM, initiating graceful shutdown");
                    if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(15)) {
                        break;
                    }
                }
                _ = sigint.recv() => {
                    info!("Received SIGINT (Ctrl+C), initiating graceful shutdown");
                    if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(2)) {
                        break;
                    }
                }
                _ = sigquit.recv() => {
                    warn!("Received SIGQUIT, initiating immediate shutdown");
                    if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(3)) {
                        break;
                    }
                }
                _ = sighup.recv() => {
                    info!("Received SIGHUP, could be used for config reload (initiating shutdown for now)");
                    if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(1)) {
                        break;
                    }
                }
            }

            // Check if we should stop handling signals
            if !self.is_handling() {
                debug!("Signal handling stopped by request");
                break;
            }
        }

        Ok(())
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    async fn handle_unix_signals_async_std(&self) -> Result<()> {
        use futures::stream::StreamExt;
        use signal_hook::consts::{SIGHUP, SIGINT, SIGQUIT, SIGTERM};
        use signal_hook_async_std::Signals;

        let mut signals = Signals::new([SIGTERM, SIGINT, SIGQUIT, SIGHUP])
            .map_err(|e| Error::signal(format!("Failed to register Unix signal handlers: {e}")))?;

        info!("Unix signal handlers registered (SIGTERM, SIGINT, SIGQUIT, SIGHUP)");

        while self.is_handling() {
            if let Some(signal) = signals.next().await {
                match signal {
                    SIGTERM => {
                        info!("Received SIGTERM, initiating graceful shutdown");
                        if self
                            .shutdown_coordinator
                            .initiate_shutdown(ShutdownReason::Signal(15))
                        {
                            break;
                        }
                    }
                    SIGINT => {
                        info!("Received SIGINT (Ctrl+C), initiating graceful shutdown");
                        if self
                            .shutdown_coordinator
                            .initiate_shutdown(ShutdownReason::Signal(2))
                        {
                            break;
                        }
                    }
                    SIGQUIT => {
                        warn!("Received SIGQUIT, initiating immediate shutdown");
                        if self
                            .shutdown_coordinator
                            .initiate_shutdown(ShutdownReason::Signal(3))
                        {
                            break;
                        }
                    }
                    SIGHUP => {
                        info!("Received SIGHUP, could be used for config reload (initiating shutdown for now)");
                        if self
                            .shutdown_coordinator
                            .initiate_shutdown(ShutdownReason::Signal(1))
                        {
                            break;
                        }
                    }
                    _ => {}
                }
            }
        }

        Ok(())
    }
}

// Windows-specific signal handling implementation
#[cfg(windows)]
impl SignalHandler {
    async fn handle_windows_signals(&self) -> Result<()> {
        #[cfg(feature = "tokio")]
        {
            self.handle_windows_signals_tokio().await
        }

        #[cfg(all(feature = "async-std", not(feature = "tokio")))]
        {
            self.handle_windows_signals_async_std().await
        }
    }

    #[cfg(feature = "tokio")]
    async fn handle_windows_signals_tokio(&self) -> Result<()> {
        use tokio::signal::windows::{ctrl_break, ctrl_c, ctrl_close, ctrl_shutdown};

        // Set up Windows console event handlers
        let mut ctrl_c_stream = ctrl_c()
            .map_err(|e| Error::signal(format!("Failed to register Ctrl+C handler: {e}")))?;

        let mut ctrl_break_stream = ctrl_break()
            .map_err(|e| Error::signal(format!("Failed to register Ctrl+Break handler: {e}")))?;

        let mut ctrl_close_stream = ctrl_close()
            .map_err(|e| Error::signal(format!("Failed to register Ctrl+Close handler: {e}")))?;

        let mut ctrl_shutdown_stream = ctrl_shutdown()
            .map_err(|e| Error::signal(format!("Failed to register shutdown handler: {e}")))?;

        info!("Windows console event handlers registered");

        loop {
            tokio::select! {
                _ = ctrl_c_stream.recv() => {
                    info!("Received Ctrl+C, initiating graceful shutdown");
                    if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(2)) {
                        break;
                    }
                }
                _ = ctrl_break_stream.recv() => {
                    info!("Received Ctrl+Break, initiating graceful shutdown");
                    if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(3)) {
                        break;
                    }
                }
                _ = ctrl_close_stream.recv() => {
                    warn!("Received console close event, initiating immediate shutdown");
                    if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(1)) {
                        break;
                    }
                }
                _ = ctrl_shutdown_stream.recv() => {
                    warn!("Received system shutdown event, initiating immediate shutdown");
                    if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(6)) {
                        break;
                    }
                }
            }

            // Check if we should stop handling signals
            if !self.is_handling() {
                debug!("Signal handling stopped by request");
                break;
            }
        }

        Ok(())
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    async fn handle_windows_signals_async_std(&self) -> Result<()> {
        use std::sync::atomic::{AtomicBool, Ordering};

        let shutdown_flag = Arc::new(AtomicBool::new(false));
        let shutdown_flag_clone = Arc::clone(&shutdown_flag);

        // For Windows with async-std, we'll use a simpler approach
        // Install a basic Ctrl+C handler
        ctrlc::set_handler(move || {
            shutdown_flag_clone.store(true, Ordering::Release);
        })
        .map_err(|e| Error::signal(format!("Failed to set Ctrl+C handler: {}", e)))?;

        info!("Windows Ctrl+C handler registered");

        // Simple polling approach for async-std
        while self.is_handling() && !shutdown_flag.load(Ordering::Acquire) {
            async_std::task::sleep(std::time::Duration::from_millis(100)).await;
        }

        if shutdown_flag.load(Ordering::Acquire) {
            info!("Received Windows console event, initiating graceful shutdown");
            self.shutdown_coordinator
                .initiate_shutdown(ShutdownReason::Signal(2));
        }

        Ok(())
    }
}

// Simple signal handler for async-std on Unix
#[cfg(all(unix, feature = "async-std", not(feature = "tokio")))]
#[allow(dead_code)]
#[allow(clippy::missing_const_for_fn)]
extern "C" fn handle_signal(_signal: libc::c_int) {
    // In a real implementation, we'd need to communicate back to the async task
    // For now, this is a placeholder
}

/// Helper function to get a human-readable description of a signal.
#[must_use]
pub const fn signal_description(signal: i32) -> &'static str {
    match signal {
        1 => "SIGHUP (Hangup)",
        2 => "SIGINT (Interrupt/Ctrl+C)",
        3 => "SIGQUIT (Quit)",
        6 => "SIGABRT (Abort)",
        9 => "SIGKILL (Kill - non-catchable)",
        15 => "SIGTERM (Terminate)",
        _ => "Unknown signal",
    }
}

/// Signal handling mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SignalHandling {
    /// Enabled - handle this signal
    Enabled,
    /// Disabled - do not handle this signal
    #[default]
    Disabled,
}

impl From<bool> for SignalHandling {
    fn from(value: bool) -> Self {
        if value {
            Self::Enabled
        } else {
            Self::Disabled
        }
    }
}

impl From<SignalHandling> for bool {
    fn from(value: SignalHandling) -> Self {
        match value {
            SignalHandling::Enabled => true,
            SignalHandling::Disabled => false,
        }
    }
}

/// Configuration for signal handling
#[derive(Debug, Clone)]
pub struct SignalConfig {
    /// SIGTERM handling for graceful shutdown
    pub term: SignalHandling,
    /// SIGINT (Ctrl+C) handling for graceful shutdown  
    pub interrupt: SignalHandling,
    /// SIGQUIT handling for immediate shutdown
    pub quit: SignalHandling,
    /// SIGHUP handling for configuration reload
    pub hangup: SignalHandling,
    /// SIGUSR1 handling for custom actions
    pub user1: SignalHandling,
    /// SIGUSR2 handling for custom actions
    pub user2: SignalHandling,
    /// Custom signal handlers with signal number and description
    pub custom_handlers: Vec<(i32, String)>,
}

impl Default for SignalConfig {
    fn default() -> Self {
        Self {
            term: SignalHandling::Enabled,
            interrupt: SignalHandling::Enabled,
            quit: SignalHandling::Enabled,
            hangup: SignalHandling::Disabled, // Disabled by default
            user1: SignalHandling::Disabled,
            user2: SignalHandling::Disabled,
            // Pre-allocate with a reasonable capacity to avoid reallocation
            custom_handlers: Vec::with_capacity(4),
        }
    }
}

impl SignalConfig {
    /// Create a new signal configuration with defaults.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable SIGHUP handling.
    #[must_use]
    pub const fn with_sighup(mut self) -> Self {
        self.hangup = SignalHandling::Enabled;
        self
    }

    /// Enable SIGUSR1 handling.
    #[must_use]
    pub const fn with_sigusr1(mut self) -> Self {
        self.user1 = SignalHandling::Enabled;
        self
    }

    /// Enable SIGUSR2 handling.
    #[must_use]
    pub const fn with_sigusr2(mut self) -> Self {
        self.user2 = SignalHandling::Enabled;
        self
    }

    /// Add a custom signal handler.
    #[must_use]
    pub fn with_custom_handler<S: Into<String>>(mut self, signal: i32, description: S) -> Self {
        self.custom_handlers.push((signal, description.into()));
        self
    }

    /// Disable SIGINT handling.
    #[must_use]
    pub const fn without_sigint(mut self) -> Self {
        self.interrupt = SignalHandling::Disabled;
        self
    }

    /// Disable SIGTERM handling.
    #[must_use]
    pub const fn without_sigterm(mut self) -> Self {
        self.term = SignalHandling::Disabled;
        self
    }

    /// Disable SIGQUIT handling.
    #[must_use]
    pub const fn without_sigquit(mut self) -> Self {
        self.quit = SignalHandling::Disabled;
        self
    }
}

/// Advanced signal handler with configurable signal handling.
#[derive(Debug)]
pub struct ConfigurableSignalHandler {
    #[allow(dead_code)]
    shutdown_coordinator: ShutdownCoordinator,
    config: SignalConfig,
    handling_signals: AtomicBool,
}

impl ConfigurableSignalHandler {
    /// Create a new configurable signal handler.
    #[must_use]
    pub const fn new(shutdown_coordinator: ShutdownCoordinator, config: SignalConfig) -> Self {
        Self {
            shutdown_coordinator,
            config,
            handling_signals: AtomicBool::new(false),
        }
    }

    /// Start handling configured signals.
    ///
    /// # Errors
    ///
    /// Returns an error if signal handling is already active or if there's a problem
    /// registering signal handlers on the platform.
    pub async fn handle_signals(&self) -> Result<()> {
        if self
            .handling_signals
            .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
            .is_err()
        {
            return Err(Error::invalid_state("Signal handling already started"));
        }

        info!("Starting configurable signal handler");

        // Log which signals will be handled
        // Pre-allocate vector with exact capacity needed based on configuration
        let mut handled_signals = Vec::with_capacity(6); // Max 6 standard signals
        if bool::from(self.config.term) {
            handled_signals.push("SIGTERM");
        }
        if bool::from(self.config.interrupt) {
            handled_signals.push("SIGINT");
        }
        if bool::from(self.config.quit) {
            handled_signals.push("SIGQUIT");
        }
        if bool::from(self.config.hangup) {
            handled_signals.push("SIGHUP");
        }
        if bool::from(self.config.user1) {
            handled_signals.push("SIGUSR1");
        }
        if bool::from(self.config.user2) {
            handled_signals.push("SIGUSR2");
        }

        info!("Handling signals: {:?}", handled_signals);

        // Platform-specific implementation
        #[cfg(unix)]
        {
            self.handle_configured_unix_signals().await
        }

        #[cfg(windows)]
        {
            self.handle_configured_windows_signals().await
        }
    }

    #[cfg(unix)]
    async fn handle_configured_unix_signals(&self) -> Result<()> {
        #[cfg(feature = "tokio")]
        {
            use tokio::signal::unix::{signal, SignalKind};

            // For now, just handle SIGTERM and SIGINT with basic approach
            if bool::from(self.config.term) || bool::from(self.config.interrupt) {
                let mut sigterm = signal(SignalKind::terminate())?;
                let mut sigint = signal(SignalKind::interrupt())?;

                loop {
                    tokio::select! {
                        _ = sigterm.recv(), if bool::from(self.config.term) => {
                            info!("Received SIGTERM, initiating graceful shutdown");
                            if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(15)) {
                                break;
                            }
                        }
                        _ = sigint.recv(), if bool::from(self.config.interrupt) => {
                            info!("Received SIGINT, initiating graceful shutdown");
                            if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(2)) {
                                break;
                            }
                        }
                    }

                    if !self.handling_signals.load(Ordering::Acquire) {
                        break;
                    }
                }
            }
        }

        #[cfg(all(feature = "async-std", not(feature = "tokio")))]
        {
            // Simplified async-std implementation
            while self.handling_signals.load(Ordering::Acquire) {
                async_std::task::sleep(std::time::Duration::from_millis(100)).await;
            }
        }

        Ok(())
    }

    #[cfg(windows)]
    async fn handle_configured_windows_signals(&self) -> Result<()> {
        // Simplified Windows implementation
        #[cfg(feature = "tokio")]
        {
            use tokio::signal::windows::{ctrl_break, ctrl_c};

            let mut ctrl_c_stream = ctrl_c()?;
            let mut ctrl_break_stream = ctrl_break()?;

            loop {
                tokio::select! {
                    _ = ctrl_c_stream.recv() => {
                        info!("Received Ctrl+C, initiating graceful shutdown");
                        if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(2)) {
                            break;
                        }
                    }
                    _ = ctrl_break_stream.recv() => {
                        info!("Received Ctrl+Break, initiating graceful shutdown");
                        if self.shutdown_coordinator.initiate_shutdown(ShutdownReason::Signal(3)) {
                            break;
                        }
                    }
                }

                if !self.handling_signals.load(Ordering::Acquire) {
                    break;
                }
            }
        }

        #[cfg(all(feature = "async-std", not(feature = "tokio")))]
        {
            while self.handling_signals.load(Ordering::Acquire) {
                async_std::task::sleep(std::time::Duration::from_millis(100)).await;
            }
        }

        Ok(())
    }

    /// Stop signal handling.
    pub fn stop(&self) {
        self.handling_signals.store(false, Ordering::Release);
        debug!("Configurable signal handling stopped");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::shutdown::ShutdownCoordinator;
    use std::time::Duration;

    #[test]
    fn test_signal_description() {
        assert_eq!(signal_description(15), "SIGTERM (Terminate)");
        assert_eq!(signal_description(2), "SIGINT (Interrupt/Ctrl+C)");
        assert_eq!(signal_description(999), "Unknown signal");
    }

    #[test]
    fn test_signal_config() {
        let config = SignalConfig::new()
            .with_sighup()
            .with_custom_handler(12, "Custom signal")
            .without_sigint();

        assert_eq!(config.interrupt, SignalHandling::Disabled);
        assert_eq!(config.term, SignalHandling::Enabled);
        assert_eq!(config.hangup, SignalHandling::Enabled);
        assert_eq!(config.custom_handlers.len(), 1);
        assert_eq!(config.custom_handlers[0].0, 12);
    }

    #[cfg(feature = "tokio")]
    #[cfg_attr(miri, ignore)]
    #[tokio::test]
    async fn test_signal_handler_creation() {
        // Add a test timeout to prevent freezing
        let test_result = tokio::time::timeout(Duration::from_secs(5), async {
            let coordinator = ShutdownCoordinator::new(5000, 10000, 15000);
            let handler = SignalHandler::new(coordinator);

            assert!(!handler.is_handling());

            // Note: We can't easily test the actual signal handling without
            // sending real signals, which would be complex in a test environment
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    #[async_std::test]
    async fn test_signal_handler_creation() {
        // Add a test timeout to prevent freezing
        let test_result = async_std::future::timeout(Duration::from_secs(5), async {
            let coordinator = ShutdownCoordinator::new(5000, 10000, 15000);
            let handler = SignalHandler::new(coordinator);

            assert!(!handler.is_handling());

            // Note: We can't easily test the actual signal handling without
            // sending real signals, which would be complex in a test environment
        })
        .await;

        assert!(test_result.is_ok(), "Test timed out after 5 seconds");
    }
}