|
2 | 2 |
|
3 | 3 | use crate::native_host::_helpers::interrupt::INTERRUPTED; |
4 | 4 | use crate::native_host::_helpers::interrupt::SIGINT; |
| 5 | +use crate::native_host::_helpers::interrupt::restore_signal_handler; |
5 | 6 | use pyo3::exceptions::PyKeyboardInterrupt; |
6 | 7 | use pyo3::prelude::Bound; |
7 | 8 | use pyo3::prelude::Py; |
8 | 9 | use pyo3::prelude::PyAny; |
9 | 10 | use pyo3::prelude::PyAnyMethods; |
| 11 | +use pyo3::prelude::PyErr; |
10 | 12 | use pyo3::prelude::PyResult; |
11 | 13 | use pyo3::prelude::Python; |
12 | 14 | use pyo3::sync::PyOnceLock; |
@@ -107,22 +109,83 @@ impl InterruptHandler { |
107 | 109 | impl Drop for InterruptHandler { |
108 | 110 | fn drop(&mut self) { |
109 | 111 | Python::attach(|py| { |
110 | | - let restored: PyResult<()> = (|| { |
111 | | - let signal = py.import("signal")?; |
112 | | - if signal |
113 | | - .call_method1("getsignal", (SIGINT,))? |
114 | | - .is(self.installed.bind(py)) |
115 | | - { |
116 | | - signal.call_method1("signal", (SIGINT, self.previous.bind(py)))?; |
117 | | - } |
118 | | - Ok(()) |
119 | | - })(); |
120 | | - drop(restored); |
| 112 | + drop(restore_signal_handler( |
| 113 | + py, |
| 114 | + SIGINT, |
| 115 | + (&self.installed, &self.previous), |
| 116 | + )); |
121 | 117 | }); |
122 | 118 | INTERRUPTED.set(false); |
123 | 119 | } |
124 | 120 | } |
125 | 121 |
|
| 122 | +/// A command's handler on an otherwise unused signal that raises the command's error, so another thread can interrupt the main thread while SIGINT is ignored, without changing how SIGINT is handled. |
| 123 | +#[derive(Debug)] |
| 124 | +pub struct WakeHandler { |
| 125 | + pub signal: i64, |
| 126 | + pub(crate) previous: Py<PyAny>, |
| 127 | + pub(crate) installed: Py<PyAny>, |
| 128 | +} |
| 129 | + |
| 130 | +impl WakeHandler { |
| 131 | + /// The signal that interrupts the main thread: SIGINT where Python handles it, else the signal of a wake handler raising `error`, installed here; `None` when neither is available. |
| 132 | + pub fn interrupting_signal( |
| 133 | + py: Python<'_>, |
| 134 | + error: fn() -> PyErr, |
| 135 | + ) -> PyResult<(Option<i64>, Option<Self>)> { |
| 136 | + let signal = py.import("signal")?; |
| 137 | + if signal.call_method1("getsignal", (SIGINT,))?.is_callable() { |
| 138 | + return Ok((Some(SIGINT), None)); |
| 139 | + } |
| 140 | + let wake: Option<Self> = Self::install(py, error)?; |
| 141 | + Ok((wake.as_ref().map(|wake| wake.signal), wake)) |
| 142 | + } |
| 143 | + |
| 144 | + /// Install the handler on SIGRTMIN, or SIGUSR2 where there is none, while that signal takes its default action. |
| 145 | + fn install(py: Python<'_>, error: fn() -> PyErr) -> PyResult<Option<Self>> { |
| 146 | + let signal = py.import("signal")?; |
| 147 | + let name: &str = if signal.hasattr("SIGRTMIN")? { |
| 148 | + "SIGRTMIN" |
| 149 | + } else if signal.hasattr("SIGUSR2")? { |
| 150 | + "SIGUSR2" |
| 151 | + } else { |
| 152 | + return Ok(None); |
| 153 | + }; |
| 154 | + let number: i64 = signal.getattr(name)?.extract()?; |
| 155 | + let previous = signal.call_method1("getsignal", (number,))?; |
| 156 | + if !previous.is(signal.getattr("SIG_DFL")?) { |
| 157 | + return Ok(None); |
| 158 | + } |
| 159 | + let installed = PyCFunction::new_closure( |
| 160 | + py, |
| 161 | + None, |
| 162 | + None, |
| 163 | + move |_: &Bound<'_, PyTuple>, _: Option<&Bound<'_, PyDict>>| -> PyResult<()> { |
| 164 | + Err(error()) |
| 165 | + }, |
| 166 | + )?; |
| 167 | + signal.call_method1("signal", (number, &installed))?; |
| 168 | + Ok(Some(Self { |
| 169 | + signal: number, |
| 170 | + previous: previous.unbind(), |
| 171 | + installed: installed.into_any().unbind(), |
| 172 | + })) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +impl Drop for WakeHandler { |
| 177 | + /// Restore the previous handler. A wake still pending raises on the first attempt and is consumed, so the second attempt restores. |
| 178 | + fn drop(&mut self) { |
| 179 | + Python::attach(|py| { |
| 180 | + let handlers = (&self.installed, &self.previous); |
| 181 | + drop( |
| 182 | + restore_signal_handler(py, self.signal, handlers) |
| 183 | + .or_else(|_pending| restore_signal_handler(py, self.signal, handlers)), |
| 184 | + ); |
| 185 | + }); |
| 186 | + } |
| 187 | +} |
| 188 | + |
126 | 189 | impl std::fmt::Debug for AuditProjection { |
127 | 190 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
128 | 191 | formatter |
|
0 commit comments