Skip to content

Release the condition mutex while detaching condition variable - #142

Closed
dhood wants to merge 2 commits into
masterfrom
release_condition_mutex_asap
Closed

dhood wants to merge 2 commits into
masterfrom
release_condition_mutex_asap

Conversation

@dhood

@dhood dhood commented Aug 18, 2017

Copy link
Copy Markdown
Member

Otherwise if something triggers while we're detaching, it will deadlock waiting for the condition mutex

In particular, for the local_parameter tests in test_rclcpp, triggering of the graph guard condition can happen while detaching conditions.

I haven't seen an explanation for why the mutex was being unlocked while calling detachCondition. it was added in 4da558f#diff-8225b098f1ec4debcdcee03d65ae95d5R1579 but from what I can see the condition mutex is not used by any detachCondition. Perhaps that was just a way to prevent it from happening most of the time.

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Otherwise if something triggers while we're detaching, it will deadlock waiting for the condition mutex

@dirk-thomas dirk-thomas left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen an explanation for why the mutex was being unlocked while calling detachCondition.

When rmw_wait tries to call detachCondition it must not hold the lock on conditionMutex because another thread might have just triggered a listener which therefore holds a lock on its internalMutex_ and waits for a lock on the conditionMutex (hence deadlock since the detachCondition() functions needs to obtain a lock on the internalMutex_ - this is a classic lock inversion).

Comment thread rmw_fastrtps_cpp/src/rmw_wait.cpp Outdated
// We're done with the condition variable now, but there's still a chance that something it was
// attached to will trigger and deadlock trying to lock the condition mutex.
// To avoid that we release it explicitly ASAP.
conditionVariable = NULL;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the local variable should have any effect. The condition variable is still attached to all the listeners. And locally it is not being used after this line.

@dhood

dhood commented Aug 21, 2017 •

Copy link
Copy Markdown
Member Author

yep, that matches the situation as I observed/understand. thank you for the explanation still, but I suppose my point was actually "I haven't seen an explanation for why the mutex was only being unlocked while calling detachCondition, as opposed to just unlocking it once before detaching all conditions." As in, I don't see why it had to be locked the rest of the time. From what I can see it wasn't necessary, but I wanted to highlight my uncertainty for reviewers.

@dirk-thomas

Copy link
Copy Markdown
Member

The current code which unlock the condition before the call of detachCondition and then re-locks it again definitely looks fishy. I would assume that if the lock is necessary that approach opens the door for a race since the condition is not locked in between these lines.

The main question is when does conditionMutex need to be locked? Each listener uses the conditionMutex_ to manage the access to the data_ variable. It only needs to do that while rmw_wait is also trying to access the flag. When rmw_wait is not running the listener falls back to "unprotected" access of the data_ member since it is the only entity using it. Therefore I think the rmw_wait functions needs to keep the lock while it is going through the loops which detach the conditions since each loop calls hasData on the listener. Based on that rational I don't think the unlock call can be move up as in the current patch since that would leave all hasData calls unprotected.

@dhood

dhood commented Aug 21, 2017

Copy link
Copy Markdown
Member Author

Based on that rational I don't think the unlock call can be move up as in the current patch since that would leave all hasData calls unprotected.

Re-stating to check my understanding: we don't want the listener's data to change between the time we finish waiting and the time we're checking it with hasData. To prevent that, we keep the conditionMutex locked so that listeners will not be able to update their data.

We must want to make sure that only the listener that notified the condition variable gets set to 0, is that right? If so, that's the part I didn't realise. I thought that it was OK to not have the conditionMutex locked in rmw_wait when we're calling hasData on listeners, provided that they're locking their internal mutex / using atomics, if appropriate: worst case we end up with multiple listeners having hasData return true. But now it seems we need only one to return true, yeah?

How about this: we keep the conditionMutex locked while we're doing all of the hasData checks. Then, we release the lock for good, and we detach all conditions. I think that this should satisfy (1) having the conditionMutex locked until we're done checking hasData for all listeners, and (2) having the conditionMutex unlocked while calling detachCondition to prevent the deadlock

@dirk-thomas

dirk-thomas commented Aug 21, 2017 •

Copy link
Copy Markdown
Member

How about this: we keep the conditionMutex locked while we're doing all of the hasData checks. Then, we release the lock for good, and we detach all conditions. I think that this should satisfy (1) having the conditionMutex locked until we're done checking hasData for all listeners, and (2) having the conditionMutex unlocked while calling detachCondition to prevent the deadlock

That sounds good to me and it doesn't leave any short term gaps where the mutex is unlocked and re-locked. 👍

@dirk-thomas dirk-thomas mentioned this pull request Aug 22, 2017
@dirk-thomas

Copy link
Copy Markdown
Member

Likely superseded by #147.

@dhood

dhood commented Aug 22, 2017

Copy link
Copy Markdown
Member Author

update for posterity:

We must want to make sure that only the listener that notified the condition variable gets set to 0, is that right? If so, that's the part I didn't realise. I thought that it was OK to not have the conditionMutex locked in rmw_wait when we're calling hasData on listeners, provided that they're locking their internal mutex / using atomics, if appropriate: worst case we end up with multiple listeners having hasData return true. But now it seems we need only one to return true, yeah?

I was initially correct that there's no issue with listeners updating their data after the wait() call, and there's no need to protect those calls with the condition mutex.

#147 includes this change in addition to changes to listeners to make the hasData calls threadsafe

@dhood dhood closed this Aug 22, 2017
@dhood
dhood deleted the release_condition_mutex_asap branch August 22, 2017 17:15
@dhood dhood removed the in review Waiting for review (Kanban column) label Aug 22, 2017
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.

2 participants