hyper icon indicating copy to clipboard operation
hyper copied to clipboard

Listening.close() does not close the server

Open timonv opened this issue 11 years ago • 24 comments

I'm trying to run a hyper server, but after closing the guard, I can't drop it and it hangs. Here's some example code where it happens (sorry for the muda, was trying a ton of things)

fn request_temp_code() -> TempCode {
    Command::new("xdg-open").arg(format!("https://slack.com/oauth/authorize?client_id={}", SLACK_CLIENT_ID)).output().unwrap();
    let server = Server::http(Ipv4Addr(127, 0, 0, 1), 9999);

    let (tx, rx) = channel();
    let mtx = Mutex::new(tx);

    let mut guard = server.listen(move |req: Request, res: Response| {
        match req.uri {
            AbsolutePath(path) => {
                // Trying to manually drop to see if this is the problem
                let tempcode = extract_temp_code(&path).unwrap();
                let gmtx = mtx.lock().unwrap();
                gmtx.send(tempcode).unwrap();
                drop(gmtx);
            },
            _ => ()
        }

        let mut res = res.start().unwrap();
        res.write_all(b"Thanks! Please return to Lax").unwrap();
        res.end().unwrap();
    }).unwrap();

    let tempcode = rx.recv().unwrap();
    guard.close().unwrap();
    println!("{}", "Auth code received!");
    println!("{}", "Server closed!");
    // Hangs here
    tempcode
} 

I haven't tried this with earlier versions of hyper (I think, not tracking numbers), however one of the last commits did involve changes related to the thread spawning into the acceptor. Have I found a possible bug?

timonv avatar Feb 24 '15 20:02 timonv

I also tried unparking the thread, to no avail. If I try to manually drop the guard after closing it, it hangs. I added some statements to check if multiple requests happen, nothing.

timonv avatar Feb 24 '15 20:02 timonv

Hmmm... It's likely that the guard returned by server.listen is incorrectly blocking for too long. I have to take another look at the code because this should work fine.

In order to facilitate fixing this, can you provide a standalone example that reproduces?

reem avatar Feb 24 '15 20:02 reem

Sure! Let's see if I can do that in under ten minutes :-)

timonv avatar Feb 25 '15 06:02 timonv

Ohh, close https://github.com/timonv/hyper_blocking_example

timonv avatar Feb 25 '15 06:02 timonv

From the code in your example I think it's very likely that the problem was some deadlock in your code mixing channels and mutexes, since there is no reason hyper would treat that function differently than any other.

Closing, unless you can reproduce (since I can't :/).

reem avatar Feb 25 '15 07:02 reem

You can't? The example I gave doesn't work and reproduces the problem. :(

timonv avatar Feb 25 '15 08:02 timonv

@reem Not sure if I need to mention for closed issues, but doing it anyway. Why would it deadlock? The mutex guard should be dropped right after the handler finishes it's shizzle. Is there anything else I can do to make this work? Maybe if Handler supported Copy I could just use an Arc.

I'd like to have this out of the way as its just the auth part of the project anyway :')

Edit: If its faster, I'm also on IRC.

timonv avatar Feb 25 '15 14:02 timonv

@timonv I looked at the example more closely and the issue is actually clear, I just didn't spot it before. The guard is created in get_message, so get_message is going to block until the entire server is done, have get_message also return the guard or create it in main to make things work.

reem avatar Feb 25 '15 22:02 reem

@reem So I can't just create a server, get the token and ditch the server again? I can have the guard in the main loop sure, but that seems like an ugly and unneeded solution, as it would essentially keep the entire server open for the duration of the app. If it blocks untill the entire server is done, then you'd kinda expect it to stop over time.

timonv avatar Feb 26 '15 07:02 timonv

You can close the server by calling .close() on the guard. You should just do this after you receive the message, if you want to close the server right then.

reem avatar Feb 26 '15 08:02 reem

However, know that hyper runs servers with a thread pool by default, so you should take care to set the number of threads to 1.

reem avatar Feb 26 '15 08:02 reem

@reem I was already closing the guard. I tried moving to a single thread for gigs yesterday but it didn't work. https://github.com/timonv/hyper_blocking_example/blob/master/src/main.rs

The weird thing is, if you print some giberish at the end of the handler, it gets printed. What could Thread::scoped's join guard be waiting for? No trouble dropping the mutex guard either.

timonv avatar Feb 26 '15 08:02 timonv

I will investigate this further, reopening.

reem avatar Feb 26 '15 09:02 reem

@reem I seem to be facing the same issue in a similar use case: very simplified version. I've narrowed it down to a problem when the Listening struct is dropped. Of note also is that the handler is still active after the call to close(). Is there anything I can help with to track down this problem?

ryansname avatar Mar 12 '15 07:03 ryansname

At the moment, std::net::TcpListener has lost the ability to closed, which is what used to power this functionality. In order fix this, we would likely need some sort of channel that would send(Close), and the ListenerPool would need to recv on this channel to determine if it should continue listening. It would also need to close the listening port.

seanmonstar avatar Apr 27 '15 18:04 seanmonstar

I just want to remark that I'd like to use close. A solution would be appreciated! :)

LukasKalbertodt avatar Jul 07 '15 18:07 LukasKalbertodt

I encountered this issue too. For reference, here's a simpler piece of code that shows Listening.close() doesn't work:

extern crate hyper;

use hyper::server::{ Server, Request, Response };
use std::thread;

fn hello(_: Request, res: Response) {
    res.send(b"Hello World!").unwrap();
}

fn main() {
    let mut listening = Server::http("127.0.0.1:8000").unwrap().handle(hello).unwrap();
    println!("SERVER STARTED");
    // Check with your OS that the port is open. For instance:
    // `watch -n 0.1 "lsof -Pan -i tcp  | grep 8000"`
    thread::sleep_ms(5000);
    listening.close();
    println!("SERVER STOPPED");
    // See the port is still open
    thread::sleep_ms(5000);
}

I wonder if we shouldn't update the documentation, until a fix gets merged.

JohanLorenzo avatar Feb 19 '16 10:02 JohanLorenzo

It now does do something on master.

seanmonstar avatar May 24 '16 18:05 seanmonstar

I'm sorry but I don't understand why this bug has been closed. The problem still exists right?

I am doing intergration test and spin up a new hyper server for each test. I use a pool of port number but the thing is that Hyper never release the port after being closed meaning that I can't run all my tests...

t-botz avatar Dec 01 '16 11:12 t-botz

I have the same use case as @thibaultdelor, and am seeing the same problem. Using hyper 0.9.12 and rustc 1.14.0-nightly (5665bdf3e 2016-11-02)

lilith avatar Dec 22 '16 09:12 lilith