-
Notifications
You must be signed in to change notification settings - Fork 67
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
Fix race condition in main loop #520
Merged
Danielius1922
merged 1 commit into
master
from
adam/bugfix/519-fix-race-cond-in-mainloop
Sep 6, 2023
Merged
Fix race condition in main loop #520
Danielius1922
merged 1 commit into
master
from
adam/bugfix/519-fix-race-cond-in-mainloop
Sep 6, 2023
Conversation
This file contains 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
🎉 Thank you for your code contribution! To guarantee the change/addition is conformant to the OCF Specification, we would like to ask you to execute OCF Conformance Testing of your change ☝️ when your work is ready to be reviewed. ℹ️ To verify your latest change (277aab3), label this PR with |
Danielius1922
force-pushed
the
adam/bugfix/519-fix-race-cond-in-mainloop
branch
from
September 4, 2023 21:46
dda13ca
to
f5a57a6
Compare
Danielius1922
force-pushed
the
adam/bugfix/519-fix-race-cond-in-mainloop
branch
from
September 5, 2023 06:49
f5a57a6
to
ee2a4fe
Compare
ocf-conformance-test-tool
bot
removed
the
OCF Conformance Testing
OCF Conformance Testing required
label
Sep 5, 2023
Danielius1922
force-pushed
the
adam/bugfix/519-fix-race-cond-in-mainloop
branch
from
September 5, 2023 07:44
ee2a4fe
to
52fce5c
Compare
jkralik
reviewed
Sep 5, 2023
Danielius1922
force-pushed
the
adam/bugfix/519-fix-race-cond-in-mainloop
branch
from
September 5, 2023 17:12
52fce5c
to
ba547ac
Compare
ocf-conformance-test-tool
bot
removed
the
OCF Conformance Testing
OCF Conformance Testing required
label
Sep 5, 2023
Danielius1922
force-pushed
the
adam/bugfix/519-fix-race-cond-in-mainloop
branch
from
September 5, 2023 17:21
ba547ac
to
d7b7eac
Compare
Danielius1922
force-pushed
the
adam/bugfix/519-fix-race-cond-in-mainloop
branch
from
September 6, 2023 07:19
d7b7eac
to
979768f
Compare
ocf-conformance-test-tool
bot
removed
the
OCF Conformance Testing
OCF Conformance Testing required
label
Sep 6, 2023
jkralik
approved these changes
Sep 6, 2023
jkralik
reviewed
Sep 6, 2023
jkralik
approved these changes
Sep 6, 2023
**Fix Race Condition in Main Loop** This commit addresses a race condition in the main loop involving `pthread_cond_wait()` and `pthread_cond_signal()`. The issue occurs in multithreaded applications using condition variables in the main loop, as follows: **Previous code:** ```C while (quit != 1) { oc_clock_time_t next_event = oc_main_poll_v1(); pthread_mutex_lock(&g_mutex); ... pthread_cond_wait(&g_cv, &g_mutex); ... pthread_mutex_unlock(&g_mutex); } ``` ```C void signal_event_loop(void) { pthread_cond_signal(&g_cv); } ``` The problem arises when: 1. The `oc_main_poll_v1()` call in the main thread is completed. 2. The main thread is paused. 3. A worker thread requests polling and calls `signal_event_loop()`, expecting to wake up the main thread for `oc_main_poll_v1`. 4. The worker thread is paused. 5. The main thread resumes execution. 6. `pthread_cond_wait` is called, causing the main loop to wait on the condition variable, missing the requested polling from the worker thread. To resolve this issue, we introduced additional synchronization. We extended the public API with a new function, `bool oc_main_needs_poll()`, which returns `true` if polling was requested but not yet processed. By using `oc_main_needs_poll()` and correctly synchronizing `pthread_cond_wait` and `pthread_cond_signal`, we prevent the race condition. **Updated Code:** ```C while (quit != 1) { oc_clock_time_t next_event = oc_main_poll_v1(); pthread_mutex_lock(&g_mutex); if (oc_main_needs_poll()) { pthread_mutex_unlock(&g_mutex); continue; } ... pthread_cond_wait(&g_cv, &g_mutex); ... pthread_mutex_unlock(&g_mutex); } ``` ```C void signal_event_loop(void) { pthread_mutex_lock(&g_mutex); pthread_cond_signal(&g_cv); pthread_mutex_unlock(&g_mutex); } ``` This change ensures correct synchronization in the main loop, preventing race conditions when waiting for polling requests.
Danielius1922
force-pushed
the
adam/bugfix/519-fix-race-cond-in-mainloop
branch
from
September 6, 2023 11:57
a6ded41
to
63f3c5a
Compare
Kudos, SonarCloud Quality Gate passed! |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
No description provided.