Fix: Resolve race condition in HSI generator worker thread startup - #80
Open
MRiganSUSX wants to merge 1 commit into
Open
Fix: Resolve race condition in HSI generator worker thread startup#80MRiganSUSX wants to merge 1 commit into
MRiganSUSX wants to merge 1 commit into
Conversation
8 tasks
Contributor
|
I have been unable to reproduce this issue, and I haven't been able to understand Michal's proposed changes yet, so we'll defer this, for now. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
A race condition exists during the startup of the
FakeHSIEventGeneratorModule. Thedo_startmethod initializes the TimeSync receiver and then immediately starts thedo_hsi_workworker thread.The worker thread's first action is to wait for a TimeSync message. However, there is no guarantee that the receiver is fully configured and ready by the time the worker thread starts waiting. In a scenario where the worker thread starts faster than the receiver can initialize, the first TimeSync message can be missed, potentially leading to a deadlock.
The existence of this race condition was confirmed by adding a temporary sleep at the beginning of the worker thread was sufficient to prevent the deadlock, proving it is a timing issue.
Solution
This change introduces an explicit synchronization mechanism using
std::promiseandstd::futureto resolve the race condition.The new workflow is as follows:
std::promiseis created before any setup begins.do_hsi_workworker thread is started. Its first action is to get thestd::futurefrom the promise andwait()on it, which blocks the thread from proceeding.do_start, completing all initialization (setting up the timestamp estimator, adding the receiver callback, etc.).set_value()on the promise.This action fulfills the future and unblocks the worker thread, which can now safely proceed, guaranteed that all resources are fully initialized.