Skip to content

HttpServer.on_connect callback fails to execute on new TCP connections #3684

@moolet

Description

@moolet

The on_connect closure, provided to HttpServer via the builder pattern, is not being invoked when a new TCP connection is established. This behavior has been confirmed using a minimal, self-contained example where a panic! inside the callback never triggers, and no associated side effects (e.g., printing to stderr, creating a file) occur. The server continues to run and eventually times out the connection as if the callback were never registered.

Cargo.toml

[package]
name = "actix_on_connect_test"
version = "0.1.0"
edition = "2021"
[dependencies]
actix-web = "4"
env_logger = "0.11"
futures-util = "0.3"

src/main.rs

use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};

// A simple handler that will be called for a GET request
async fn index(_req: HttpRequest) -> HttpResponse {
    println!(">>> Handler 'index' triggered.");
    HttpResponse::Ok().body("Hello from the handler!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

    println!("Starting server on 0.0.0.0:8080...");

    HttpServer::new(|| App::new().route("/", web::get().to(index)))
        .bind(("0.0.0.0", 8080))?
        .on_connect(|_,_| {
            // This code is expected to execute immediately upon a TCP connection being accepted.
            eprintln!("\n!!! on_connect is about to panic! If you see this, the callback is executing. !!!");
            panic!("Intentional panic from on_connect to prove execution.");
        })
        .run()
        .await
}

Actual Behavior

The telnet client successfully connects (Connected to...) but the server does not crash.
No output from eprintln! is visible in the server's logs.
The connection remains open until a timeout occurs, at which point the client receives a 408 Request Timeout and disconnects.
For the curl request, the server successfully handles the HTTP request via the index handler, but the on_connect callback is never executed beforehand.
The server process continues to run without any sign that the on_connect closure was ever triggered.

Environment Details

Actix-Web Version: 4.x (actix-web = "4")
Rust Version: rustc 1.82.0 (f6e511eec 2024-10-15) (built from a source tarball)
Operating System: nixos-version
24.11.718051.9b5ac7ad4529 (Vicuna), Linux nixos 6.6.90 #1-NixOS SMP PREEMPT_DYNAMIC Fri May 9 07:44:08 UTC 2025 x86_64 GNU/Linux, inside a VM, within VirtManager, on a Linux host.
Client used for testing: telnet, curl, and netcat (with various shell functions).
Server run command: cargo run

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions