ntex-rs / ntex

framework for composable networking services
Apache License 2.0
1.84k stars 105 forks source link

[ntex-server](Bug) Duplicates ctrlc_handler registration under win causing process crash #322

Closed canxin121 closed 3 months ago

canxin121 commented 3 months ago

Platform

Windows

This bug seems to be platform related, because ctrlc has different behavior in unix and windows, and the start function has different definitions for win and unix.

Description

Bug appeared during an attempt to use ntex-mqtt. I tried to make mqtt_server shutdown and restart with configuration changes.

But after repeated calls to Server::run(), the run function called ntex-server's signals::start function repeatedly, which resulted in duplicate registrations of ctrlc_handler and a direct panic crash.

The process of reproducing the bug:

lazy_static! {
    static ref SERVER_HANDLE: Arc<Mutex<Option<Server>>> = Arc::new(Mutex::new(None));
}
async fn start_mqtt_server() {
    let server = ntex::server::build()
        .bind("mqtt", "127.0.0.1:1883", |_| {
            MqttServer::new()
                .v3(v3::MqttServer::new(handshake_v3)
                    .control(control_service_factory_v3())
                    .publish(fn_factory_with_config(
                        |session: v3::Session<V3ClientSession>| {
                            Ready::Ok::<_, MyServerError>(fn_service(move |req| {
                                publish_v3(session.clone(), req)
                            }))
                        },
                    )))
                .v5(v5::MqttServer::new(handshake_v5)
                    .control(control_service_factory_v5())
                    .publish(fn_factory_with_config(
                        |session: v5::Session<V5ClientSession>| {
                            Ready::Ok::<_, MyServerError>(fn_service(move |req| {
                                publish_v5(session.clone(), req)
                            }))
                        },
                    )))
        })
        .unwrap()
        .run();
    let mut server_handle = SERVER_HANDLE.lock().await;
    *server_handle = Some(server);
}

async fn stop_mqtt_server() {
    let server_option = SERVER_HANDLE.lock().await;
    let server = server_option.as_ref().unwrap();
    server.stop(true).await;
}

#[ntex::main]
async fn main() -> std::io::Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::TRACE)
        .init();
    start_mqtt_server().await;
    stop_mqtt_server().await;
    start_mqtt_server().await;
    Ok(())
}

Log

thread 'ntex-server signals' panicked at C:\Users\canxin.LAPTOP\.cargo\registry\src\index.crates.io-6f17d22bba15001f\ntex-server-1.0.2\src\signals.rs:102:14:
Error setting Ctrl-C handler: MultipleHandlers
canxin121 commented 3 months ago

This could be fixed by

async fn start_mqtt_server() {
    let server = ntex::server::build()
        .disable_signals()
    ·····

But this simply bypasses signal control.

If you still want to use signal control, I feel it's really difficult to really solve the closed loop of run and stop. I'm afraid that's something that needs to be left to someone who knows more about ntex but not me.

fafhrd91 commented 3 months ago

could you try master