use nix::sys::signal::{self, SigSet};
#[tokio::test]
async fn test_shutdown() {
let server = TestServer::new().await;
let proxy = ProxyUnderTest::new(server.listen_port).await;
sleep(std::time::Duration::from_millis(50));
let current_thread_id = unsafe { libc::pthread_self() };
println!("current_thread_id :{:?}", current_thread_id);
// Send the SIGTERM signal to the current thread.
nix::sys::signal::pthread_kill(current_thread_id, signal::Signal::SIGTERM).unwrap();
// Block the SIGTERM signal in the main thread.
let mut sigset = SigSet::empty();
sigset.add(signal::SIGTERM);
signal::pthread_sigmask(signal::SigmaskHow::SIG_BLOCK, Some(&sigset), None).unwrap();
// Wait for the proxy to shutdown.
let reason_opt = proxy.handle.await.unwrap();
assert_eq!(reason_opt, Some(ShutdownReason::GracefulShutdown));}
我想仅向 Rust 测试中的调用线程发送
SIGTERM
信号,但目前,该信号由 cargo test
生成的所有其他线程接收。我该如何解决这个问题?
我尝试使用
pthread_kill
发送信号,但它似乎会影响所有线程。此外,我尝试使用 SIGTERM
和 SigSet
来阻止主线程中的 pthread_sigmask
信号,但这并没有解决问题。
如何确保
SIGTERM
信号仅被当前线程接收到?任何帮助将不胜感激。
pthread_cancel
来终止特定线程。线程取消很难使用,通常需要进程中所有库的支持。此外,在 GNU/Linux 上,pthread_cancel
涉及堆栈展开,而当前的 Rust 实现不支持跨 FFI 边界的展开。有一个建议添加C-unwind
ABI。即使这个问题得到解决,pthread_cancel
仍然难以正确使用。