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

Problem about 20.3 "Listing 20-25: Shut down the server after serving two requests by exiting the loop" #199

Open
muayiqi opened this issue Jul 8, 2024 · 1 comment

Comments

@muayiqi
Copy link

muayiqi commented Jul 8, 2024

my main function is :

fn main() {
    let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
    let pool = ThreadPool::new(4).expect("Init thread pool failed");

    for stream in listener.incoming().take(2) {
        let stream = stream.unwrap();
        pool.execute(move || {
            handle_connection(stream);
        });
    }

    println!("Shutting down.");
}

and other codes are the same as this book, when I run the server, then use the Edge browser to access "localhost:7878" only once, I get the output:

Worker 0 got a job; executing.
Worker 1 got a job; executing.
Shutting down.
Shutting down worker 0
Worker 2 got a job; executing.
Worker 3 disconnected; shutting down.
Worker 0 disconnected; shutting down.
Worker 1 disconnected; shutting down.
Shutting down worker 1
Shutting down worker 2

I mean that one request but get two jobs, when I use:
echo "hello" > /dev/tcp/127.0.0.1/7878
in Ubuntu, I get the proper output, one request, one job.
I really want to know the reason, thank you~

@bfedie518
Copy link

Edge is sending two requests. If you use another browser like Firefox, you'll get the behavior you expect.

If you want to see what the requests are, you could print them out like we did in the first version of handle_connection() (listing 20-2).

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

No branches or pull requests

2 participants