|
| 1 | +// Copyright 2025 Lablup Inc. and Jeongkyu Shin |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//! Signal handling for interactive mode |
| 16 | +
|
| 17 | +use anyhow::Result; |
| 18 | +use std::sync::Arc; |
| 19 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 20 | +use tokio::signal; |
| 21 | +use tracing::{debug, info}; |
| 22 | + |
| 23 | +/// Global flag for interrupt signal |
| 24 | +static INTERRUPTED: AtomicBool = AtomicBool::new(false); |
| 25 | + |
| 26 | +/// Check if an interrupt signal has been received |
| 27 | +pub fn is_interrupted() -> bool { |
| 28 | + INTERRUPTED.load(Ordering::Relaxed) |
| 29 | +} |
| 30 | + |
| 31 | +/// Reset the interrupt flag |
| 32 | +pub fn reset_interrupt() { |
| 33 | + INTERRUPTED.store(false, Ordering::Relaxed); |
| 34 | +} |
| 35 | + |
| 36 | +/// Set up signal handlers for interactive mode |
| 37 | +pub fn setup_signal_handlers() -> Result<Arc<AtomicBool>> { |
| 38 | + let shutdown = Arc::new(AtomicBool::new(false)); |
| 39 | + let shutdown_clone = Arc::clone(&shutdown); |
| 40 | + |
| 41 | + // Handle Ctrl+C |
| 42 | + ctrlc::set_handler(move || { |
| 43 | + info!("Received Ctrl+C signal"); |
| 44 | + INTERRUPTED.store(true, Ordering::Relaxed); |
| 45 | + shutdown_clone.store(true, Ordering::Relaxed); |
| 46 | + })?; |
| 47 | + |
| 48 | + Ok(shutdown) |
| 49 | +} |
| 50 | + |
| 51 | +/// Set up async signal handlers for tokio runtime |
| 52 | +pub async fn setup_async_signal_handlers(shutdown: Arc<AtomicBool>) { |
| 53 | + tokio::spawn(async move { |
| 54 | + // Handle SIGTERM (Unix only) |
| 55 | + #[cfg(unix)] |
| 56 | + { |
| 57 | + let mut sigterm = signal::unix::signal(signal::unix::SignalKind::terminate()) |
| 58 | + .expect("Failed to set up SIGTERM handler"); |
| 59 | + |
| 60 | + tokio::select! { |
| 61 | + _ = sigterm.recv() => { |
| 62 | + info!("Received SIGTERM signal"); |
| 63 | + shutdown.store(true, Ordering::Relaxed); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +/// Handle terminal resize signal (Unix only) |
| 71 | +#[cfg(unix)] |
| 72 | +pub async fn handle_terminal_resize() -> Result<tokio::sync::mpsc::UnboundedReceiver<(u16, u16)>> { |
| 73 | + let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); |
| 74 | + |
| 75 | + tokio::spawn(async move { |
| 76 | + let mut sigwinch = signal::unix::signal(signal::unix::SignalKind::window_change()) |
| 77 | + .expect("Failed to set up SIGWINCH handler"); |
| 78 | + |
| 79 | + loop { |
| 80 | + sigwinch.recv().await; |
| 81 | + |
| 82 | + if let Ok((width, height)) = crossterm::terminal::size() { |
| 83 | + debug!("Terminal resized to {}x{}", width, height); |
| 84 | + let _ = tx.send((width, height)); |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + Ok(rx) |
| 90 | +} |
| 91 | + |
| 92 | +/// Terminal state guard for automatic restoration |
| 93 | +pub struct TerminalGuard { |
| 94 | + original_hook: Option<Box<dyn Fn() + Send + Sync>>, |
| 95 | +} |
| 96 | + |
| 97 | +impl Default for TerminalGuard { |
| 98 | + fn default() -> Self { |
| 99 | + Self::new() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl TerminalGuard { |
| 104 | + /// Create a new terminal guard that will restore terminal state on drop |
| 105 | + pub fn new() -> Self { |
| 106 | + // Save the current panic hook |
| 107 | + let original_hook = std::panic::take_hook(); |
| 108 | + |
| 109 | + // Set a custom panic hook that restores terminal before panicking |
| 110 | + std::panic::set_hook(Box::new(move |panic_info| { |
| 111 | + // Try to restore terminal |
| 112 | + let _ = Self::restore_terminal(); |
| 113 | + |
| 114 | + // Call the original panic hook |
| 115 | + eprintln!("\n{panic_info}"); |
| 116 | + })); |
| 117 | + |
| 118 | + Self { |
| 119 | + original_hook: None, // We can't store the original hook due to lifetime issues |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /// Restore terminal to normal mode |
| 124 | + pub fn restore_terminal() -> Result<()> { |
| 125 | + use crossterm::{execute, terminal}; |
| 126 | + use std::io; |
| 127 | + |
| 128 | + // Disable raw mode if it was enabled |
| 129 | + let _ = terminal::disable_raw_mode(); |
| 130 | + |
| 131 | + // Show cursor |
| 132 | + let _ = execute!( |
| 133 | + io::stdout(), |
| 134 | + crossterm::cursor::Show, |
| 135 | + terminal::LeaveAlternateScreen |
| 136 | + ); |
| 137 | + |
| 138 | + Ok(()) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl Drop for TerminalGuard { |
| 143 | + fn drop(&mut self) { |
| 144 | + // Restore terminal state |
| 145 | + let _ = Self::restore_terminal(); |
| 146 | + |
| 147 | + // Note: We can't restore the original panic hook here due to lifetime issues |
| 148 | + // This is a limitation, but acceptable for our use case |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +#[cfg(test)] |
| 153 | +mod tests { |
| 154 | + use super::*; |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn test_interrupt_flag() { |
| 158 | + // Reset flag |
| 159 | + reset_interrupt(); |
| 160 | + assert!(!is_interrupted()); |
| 161 | + |
| 162 | + // Set flag |
| 163 | + INTERRUPTED.store(true, Ordering::Relaxed); |
| 164 | + assert!(is_interrupted()); |
| 165 | + |
| 166 | + // Reset again |
| 167 | + reset_interrupt(); |
| 168 | + assert!(!is_interrupted()); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_terminal_guard_creation() { |
| 173 | + let _guard = TerminalGuard::new(); |
| 174 | + // Guard should be created without panic |
| 175 | + } |
| 176 | +} |
0 commit comments