Introduce a watchdog thread to handle termination gracefully #6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This addresses issue #2 as well.
The source of the segfault was that, on
ctrl-c
, we would often find ourselves in situations where we can close theMidi.device
twice. (once in the signal handler, once at the termination of the main thread, that is also callingMidi.shutdown
.)Another source of segfault was having the sequencer thread in
stat_engine
write to a device that was closed by said signal handler.I also noticed that the
stat_engine
would not check thechild_alive
function to see if the instrumented program was still running or not, leading tocardio-crumble
running indefinitely trying to poll events that would never show up.I decided to implement a solution to all of these problems by adding a watchdog thread to be run alongside any threads the engine is using.
The idea being it is that it would introduce a
Watchdog.terminate
atomic variable, that each thread should check from time to time. IfWatchdog.terminate
istrue
, then said threads should be exiting gracefully.The
watchdog
domain itself is just checking thatchild_alive
isfalse
(meaning the instrumented program has stopped).If it detects it is the case, it will then set
terminate
totrue
, and exit.This may be an overkill solution, but I think it works quite nicely, let me know if you have any nicer idea. :-)