-
Notifications
You must be signed in to change notification settings - Fork 649
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
Terminate block production loop when shutting down witness plugin #1314 #1332
Conversation
Travis-CI build failed perhaps related to issue #1303 |
I've restarted Travis-CI build for this PR, just to see if it will fail again or not |
Restarted again. |
Thanks ! Now |
Thread handling in fc is still a mystery to me. That said, I think there's a race condition in your code. _block_production_task recreates itself by calling schedule_production_loop() from inside block_production_loop(). That means if block_production_loop is being executed while you call cancel, the cancel will only affect the currently running loop but will not prevent it from scheduling the next one. I may be wrong though. |
It will wait current block production execution and after that will cancel, so no race condition must be here. |
cancel_and_wait sends the cancel signal and waits for the task to be canceled. |
|
|
That change makes the window for the race condition smaller, but doesn't eliminate it. |
It just doesn't work like that. You must either use locking or an atomic check-and-set of some kind. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the code as written will greatly reduce the chance of a production loop executing after shutdown. I have not walked through all scenarios, but I think this covers many of the bases. The comments below are just some opinions.
IMO: locking / check and set are overkill here. A volatile boolean would work if in this plugin we do not want to support a startup, shutdown, and then another startup. I'm not saying atomic_flag doesn't do the job, it is just some unneeded overhead.
I am not approving this PR, as I believe @pmconrad may be thinking of a scenario that I am not. So I will defer to him.
I believe there is still a race condition. |
@jmjatlanta you are right in that the implementation so far eliminates most of the risk. Nevertheless I think we should do it properly. Overhead should not be a problem since it affects only actual witnesses, and only once per second. |
Actually I didn't get why we're making it so complicated. Why need another The production loop function is NOT a big task thus won't take much time to execute. So we can simply wait for it to exit if it is still running when trying to shutting it down. So my solution would be:
Thoughts? |
we need to use |
IMHO we don't care whether the loop runs one more time. We don't need a mutex for the new variable because there is only one thread that writes to it and another thread only reads it. (I'm talking about my solution mentioned above, not your code) |
perhaps |
There is at most one thread executing |
2e5e15b
to
109c7ce
Compare
IMO cancel_and_wait is still needed to prevent crash during shutdown. |
For my solution, better add a check for (BTW actually I don't know how |
9e361df
to
be44d37
Compare
@cogutvalera the code looks fine to me, but the commit message "review changes" doesn't help in case when revisiting the code/changes in the future. How to Write a Git Commit Message: https://chris.beams.io/posts/git-commit/ . I'm learning this as well. |
be44d37
to
b3dea54
Compare
@abitmore Thanks ! I've changed comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, looks good and simple test worked ok. Thanks!
Thanks ! |
Thanks ! |
PR for "Terminate block production loop when shutting down witness plugin #1314"