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

Add unblock method for graceful shutdown #156

Closed
wants to merge 1 commit into from
Closed

Add unblock method for graceful shutdown #156

wants to merge 1 commit into from

Conversation

guillaumekoenig
Copy link
Contributor

I hit the following problem and saw it in the issues too (#146) : how do you gracefully shutdown the server ? There is effectively no way of breaking out of incoming_requests() other than receiving a client request.

Rust favors message passing. We can't kill threads or send them signals Unix style. We can only pass messages between threads via channels to tell them to shut down on their own (or with synchronization primitives such as condvar, or with shared memory under lock). After some research, it turns out the standard library does blocking IO (ie the accept method on TcpListener used by tiny-http). There really is no way to break out of it except with a client request (or on error). This is already dealt with by a previous commit by @moises-silva (cde7981) : on object destruction we briefly connect to the server to unblock the thread stuck on accept. (side note : for async IO look for mio)

But a for loop on incoming_requests it still blocking. It's not stuck on accept but inside the message queue waiting on condvar for new client requests. This commit adds a special request variant that says to unblock the loop and return no client request (since there is none we just want to shutdown). I slightly modified the message_queue API (ie the pop method now returns an Option), which is internal. There are no changes on the public side, ie on server, except the addition of the unblock method that effectively unblocks one thread stuck in incoming_requests (or recv).

Since there is no breaking change to the public API, all existing code work as previously. The addition of unblock() allows to implement graceful server shutdown by calling it from another thread. Hopefully the two tests added make its usage clear. Let me know what you think.

@moises-silva
Copy link
Contributor

Nice job, this may come handy for me soon-ish.

@guillaumekoenig
Copy link
Contributor Author

Nice job, this may come handy for me soon-ish.

Thanks!

Copy link
Member

@tomaka tomaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@asv7c2
Copy link

asv7c2 commented Sep 6, 2020

why it not accepted?

@rawler
Copy link
Collaborator

rawler commented Sep 22, 2020

I could use this as well.

@rawler
Copy link
Collaborator

rawler commented Sep 22, 2020

Replaced by a rebased version in #184

@rawler rawler closed this Sep 22, 2020
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

Successfully merging this pull request may close these issues.

None yet

5 participants