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

Fix a race condition in DTD for the local termdet #698

Merged

Conversation

therault
Copy link
Contributor

DTD uses termination_detected() to destroy the taskpool, and on_leave_wait() to reset the taskpool and reinitialize the termination detector.

The 'local' termination detector module had a race condition: it would mark the taskpool as 'terminated', then call the termination_detected() callback.

As any thread can detect the termination, a worker thread could enter taskpool destruction in parallel with the main thread calling on_leave_wait() because taskpool_state() has returned TERMINATED already.

Thus, the taskpool could be destroyed after its termination detector is reset, leading to asserts.

Proposed solution consists of adding an intermediary state in the termination detector, 'TERMINATING'. TERMINATING is shown as 'BUSY' to the scheduler, so the main thread does not return from the main loop and does not call on_leave_wait() while the thread that detected termination is still executing termination_detected(). TERMINATING is still a necessary intermediary step in order to ensure that only one thread calls termination_detected().

Solves issue #634

DTD uses termination_detected() to destroy the taskpool,
and on_leave_wait() to reset the taskpool and reinitialize
the termination detector.

The 'local' termination detector module had a race condition:
it would mark the taskpool as 'terminated', then call the
termination_detected() callback.

As any thread can detect the termination, a worker thread could
enter taskpool destruction in parallel with the main thread
calling on_leave_wait() because taskpool_state() has returned
TERMINATED already.

Thus, the taskpool could be destroyed *after* its termination
detector is reset, leading to asserts.

Proposed solution consists of adding an intermediary state in
the termination detector, 'TERMINATING'. TERMINATING is shown
as 'BUSY' to the scheduler, so the main thread does not return
from the main loop and does not call on_leave_wait() while the
thread that detected termination is still executing
termination_detected(). TERMINATING is still a necessary
intermediary step in order to ensure that only one thread calls
termination_detected().

Solves issue ICLDisco#634
@therault therault requested a review from a team as a code owner November 13, 2024 18:32
@abouteiller abouteiller self-requested a review November 14, 2024 20:30
…eeping the taskpool alive until the termination detector is done doing its things. To do this, the termination detector keeps a reference on the taskpool, until it is effectively terminated. Taskpool destruction can then be delayed until termination completes.
…t the taskpool release within the termination detector doesn't cycle calling the destructor again
termination detector has been removed manually) to be unregistered.
@abouteiller abouteiller self-requested a review November 17, 2024 14:14
@abouteiller
Copy link
Contributor

passed ctests

@abouteiller abouteiller added this to the v4.0 milestone Nov 17, 2024
@abouteiller abouteiller merged commit 41c30b2 into ICLDisco:master Nov 17, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants