Fix windows signals support (#325)

This commit is contained in:
Nikolay Kim 2024-03-29 20:56:19 +01:00 committed by GitHub
parent 0e21f46210
commit 18d3516343
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 15 deletions

View file

@ -1,5 +1,9 @@
# Changes
## [1.0.3] - 2024-03-29
* Fix windows signals support
## [1.0.2] - 2024-03-25
* Add basic signals support

View file

@ -1,6 +1,6 @@
[package]
name = "ntex-server"
version = "1.0.2"
version = "1.0.3"
authors = ["ntex contributors <team@ntex.rs>"]
description = "Server for ntex framework"
keywords = ["network", "framework", "async", "futures"]

View file

@ -85,20 +85,49 @@ pub(crate) fn start<T: Send + 'static>(srv: Server<T>) {
/// Signals are handled by oneshots, you have to re-register
/// after each signal.
pub(crate) fn start<T: Send + 'static>(srv: Server<T>) {
let _ = thread::Builder::new()
.name("ntex-server signals".to_string())
.spawn(move || {
ctrlc::set_handler(move || {
srv.signal(Signal::Int);
use std::sync::Mutex;
System::current().arbiter().exec_fn(|| {
HANDLERS.with(|handlers| {
for tx in handlers.borrow_mut().drain(..) {
let _ = tx.send(Signal::Int);
use ntex_rt::spawn;
static CUR_SYS: Mutex<RefCell<Option<System>>> = Mutex::new(RefCell::new(None));
let guard = match CUR_SYS.lock() {
Ok(guard) => guard,
Err(_) => {
log::error!("Cannot lock mutex");
return;
}
};
let mut sys = guard.borrow_mut();
let started = sys.is_some();
*sys = Some(System::current());
let rx = signal();
let _ = spawn(async move {
if let Ok(sig) = rx.await {
srv.signal(sig);
}
});
if !started {
let _ = thread::Builder::new()
.name("ntex-server signals".to_string())
.spawn(move || {
ctrlc::set_handler(move || {
if let Ok(guard) = CUR_SYS.lock() {
if let Some(sys) = &*guard.borrow() {
sys.arbiter().exec_fn(|| {
HANDLERS.with(|handlers| {
for tx in handlers.borrow_mut().drain(..) {
let _ = tx.send(Signal::Int);
}
})
});
}
})
});
})
.expect("Error setting Ctrl-C handler");
});
}
})
.expect("Error setting Ctrl-C handler");
});
}
}