Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
canxin121 opened this issue Mar 29, 2024 · 2 comments

Comments

@canxin121
Copy link

canxin121 commented Mar 29, 2024

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
Copy link
Author

canxin121 commented Mar 29, 2024

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
Copy link
Member

could you try master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants