rust 使用时雄::select!等待多个信号失败,借用错误时丢弃临时值

wfsdck30  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(158)

在一个时雄项目中,我想同时等待SIGTERMSIGINT
只需等待其中一个信号即可正常工作:

tracing::debug!("Waiting for the end of the world");
     signal(SignalKind::terminate())?.recv().await;

但当我尝试使用www.example.com中this answertokio::select!时users.rustlang.org

use tokio::signal::unix::{signal, SignalKind};
tokio::select! {
    _ = signal(SignalKind::interrupt())?.recv() => println!("SIGINT"),
    _ = signal(SignalKind::terminate())?.recv() => println!("SIGTERM"),
}
println!("terminating the process...");

我收到一个关于释放仍在使用中的临时变量的错误。

error[E0716]: temporary value dropped while borrowed
   --> src/main.rs:495:13
    |
494 |       let ans = tokio::select! {
    |  _______________-
495 | |         _ = signal(SignalKind::interrupt())?.recv() => println!("SIGINT"),
    | |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
496 | |         _ = signal(SignalKind::terminate())?.recv() => println!("SIGTERM"),
497 | |     };
    | |     -
    | |     |
    | |_____temporary value is freed at the end of this statement
    |       borrow might be used here, when `futures` is dropped and runs the destructor for type `(impl futures_util::Future<Output = std::option::Option<()>>, impl futures_util::Future<Output = std::option::Option<()>>)`

这还能用吗?

pftdvrlh

pftdvrlh1#

问题中引用的例子有一个小错误。如果Result<Signal>来自signal是在select!宏之外处理的,则没有临时变量可以超过其借用的作用域。

println!("Waiting for the end of the world");

    let mut sigterm = signal(SignalKind::terminate())?;
    let mut sigint = signal(SignalKind::interrupt())?;

    tokio::select! {
        _ = sigterm.recv() => {println!("SIGTERM Shuting down");},
        _ = sigint.recv() => {println!("SIGINT Shuting down");},
    };

相关问题