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

Use a semaphore or pipe for shutdown notification (skeees, laanwj) #13211

Closed
wants to merge 2 commits into from

Conversation

laanwj
Copy link
Member

@laanwj laanwj commented May 10, 2018

Instead of polling, use a semaphore (on WIN32) or a pipe (on POSIX) for shutdown notification. This avoids the need for a polling loop in bitcoind.

Extends #13186.

This does not change the polilng of ShutdownRequested() in the GUI. This is more complicated: on POSIX it would be possible to import the pipe[0] handle into the event loop, and call a handler when a character is detected, on Windows the StartShutdown() could make a callback that queues a qt signal at the GUI side. But I think that is better left for a later PR.

Instead of polling, use a semaphore (on WIN32) or a pipe (on POSIX) for
shutdown notification. This avoids needing a polling loop in bitcoind.
@laanwj laanwj changed the title Use a semaphore or pipe for shutdown notification Use a semaphore or pipe for shutdown notification (skeees, laanwj) May 10, 2018
@skeees
Copy link
Contributor

skeees commented May 10, 2018

Oh cool - if you are going to split into platform dependent code - you can post to a pthreads semaphore from a unix signal handler.
Would also suggest that anyone waiting/reading from the semaphore/pipe immediately post/write back that way you could have multiple threads listening for shutdown as opposed to restricting it to only one as you do currently

@laanwj
Copy link
Member Author

laanwj commented May 10, 2018

you can post to a pthreads semaphore from a unix signal handler.

I'm fairly sure none of the pthreads-api is signal safe, at least on all platforms.

Would also suggest that anyone waiting/reading from the semaphore/pipe immediately post/write back that way you could have multiple threads listening for shutdown as opposed to restricting it to only one as you do currently

We only need to wait for shutdown in one thread, so as I see it that additional complexity is not needed. If it happens to be needed in the future it can be added then.

Edit: Travis issue is interesting though: looks like there is a race at shutdown where the HTTP server doesn't get to send back the reply to stop(). I'd be surprised if it is new in this pull, though it probably comes to the foreground due to the instant shutdown notification. I remember previous discussion about this in #11006 (esp. #11006 (comment)).

@laanwj
Copy link
Member Author

laanwj commented May 10, 2018

Added a commit that reverts 793667a (#11006) to hopefully make travis pass. Likely, this has to be conditional on libevent version to prevent adding a delay on shutdown in another place while fixing another.

Edit: that worked locally, but not on travis- weird.

@ken2812221
Copy link
Contributor

ken2812221 commented May 14, 2018

Maybe that shutdown is too quick, add 50ms delay? It failed on my machine occasionally

@laanwj
Copy link
Member Author

laanwj commented Jun 4, 2018

Maybe that shutdown is too quick, add 50ms delay? It failed on my machine occasionally

Adding a fixed delay is never a valid solution.

@ken2812221
Copy link
Contributor

ken2812221 commented Jun 16, 2018

Once StopHTTPServer has been called in main thread ,the event loop in http thread exit. But http workqueue thread needs to use the event loop to call evhttp_send_reply, this could happen after the http thread event loop has already exit.

HTTPEvent* ev = new HTTPEvent(eventBase, true, [req_copy, nStatus]{

@laanwj
Copy link
Member Author

laanwj commented Jun 24, 2018

Closing this for now, I don't think it's that important, the current way works fine.

@laanwj laanwj closed this Jun 24, 2018
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants