You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before rcl_wait is called, an existing signal handler is stored so that it can be called from within rclpy's signal handler. The existing signal handler is restored after waking up.
This is not currently robust to rclpy_wait being called from multiple threads; each thread ought to store its own signal handler to restore.
Well, I'm not sure that's the right fix for this. Instead, maybe there should be a thread-agnostic signal handler in the Python layer that acts like the rclcpp signal handler which triggers the sigint guard condition for all wait sets:
So to recap, I think the issue is that rclpy_wait (which as you said calls rcl_wait) is not thread-safe and should be. I think the solution is to have one signal handler which is registered during "rclpy init" (either in C or Python, not sure which yet), rather than within rclpy_wait, and then multiple guard conditions, one for each wait set just like rclcpp.
I'm not sure it ever make sense to have one "stored signal handler to be restored" for each thread. I did mention that when we were talking in person, but after thinking about it more, I'm not sure that would work correctly.
I think we're getting burned by this right now. We have a python process with multiple nodes and multiple executors on different threads, but our process is still blowing up with
"Executor.wait_for_ready_callbacks() called concurrently in multiple threads"
We can work around this by having a single worker thread pump messages, but this will mean needing additional synchronization whenever we want to access data received by a subscriber from the 'main' thread
@wjwwood I've been working on this, but then I realized that I'd have to call rcl_wait twice, once with a WaitSet that only contains the GuardCondition and a timeout set to -1, and then for the existing rcl_wait call. Is that what you thought or is there more to it? Thanks.
@dejanpan I wanted to run it by @wjwwood first to make sure I understood the problem correctly, but I'll create a pull request with what I have just to have something tangible that we discuss over.
I don't fully follow your second comment about needing to call it twice, but from the bullet list proposal, I'd wonder if it actually solves the issue, because if a waitset is waiting and has not work, but another waitset is waiting on it to finish before actually waiting despite the fact that it has data available, then you'd starve a ready waitset with a blocking waitset. Which effectively doesn't let you wait from two threads at the same time, if I understand it correctly.
Perhaps I don't have a grasp on the solution you've proposed yet. I'm happy to keep discussing it, but a pr, even a partial one, might be a more concrete thing to discuss.
@esteve - I do not - I'm actually not exactly sure what subtle failures were seen before the big scary runtime error RuntimeError: Executor.wait_for_ready_callbacks() called concurrently in multiple threads' was added.
I'm actually not exactly sure what subtle failures were seen before the big scary runtime error
It is to do with the management of the signal handlers from within the C extension, as the description of the issue says:
Before rcl_wait is called, an existing signal handler is stored so that it can be called from within rclpy's signal handler. The existing signal handler is restored after waking up.
This is not currently robust to rclpy_wait being called from multiple threads; each thread ought to store its own signal handler to restore.
So, running it for a long time is not the issue, I think instead you'd need to run it, sigint it, run it again, in a loop until you see (or do not see) some failure or deadlock.
Activity
wjwwood commentedon Jun 19, 2018
Well, I'm not sure that's the right fix for this. Instead, maybe there should be a thread-agnostic signal handler in the Python layer that acts like the rclcpp signal handler which triggers the sigint guard condition for all wait sets:
So to recap, I think the issue is that
rclpy_wait(which as you said callsrcl_wait) is not thread-safe and should be. I think the solution is to have one signal handler which is registered during "rclpy init" (either in C or Python, not sure which yet), rather than withinrclpy_wait, and then multiple guard conditions, one for each wait set just likerclcpp.I'm not sure it ever make sense to have one "stored signal handler to be restored" for each thread. I did mention that when we were talking in person, but after thinking about it more, I'm not sure that would work correctly.
pbaughman commentedon Feb 1, 2019
@dejanpan
I think we're getting burned by this right now. We have a python process with multiple nodes and multiple executors on different threads, but our process is still blowing up with
Apparently in c++ it's OK to run multiple executors on multiple threads, but in python we're being prevented from doing this by the global g_wait_set_spinning bool defined in the module https://github.com/ros2/rclpy/blob/master/rclpy/rclpy/executors.py#L34
We can work around this by having a single worker thread pump messages, but this will mean needing additional synchronization whenever we want to access data received by a subscriber from the 'main' thread
esteve commentedon Feb 12, 2019
I think the following might work:
WaitSetis created, create aGuardConditionand add it as aPyCapsuleto the Python object for theWaitSetrclpy_wait, retrieve theGuardConditionfrom thePyCapsuleGuardConditionbefore callingrcl_wait, afterhttps://github.com/ros2/rclpy/blob/master/rclpy/src/rclpy/_rclpy.c#L2572
GuardConditiononcercl_waitfinishes to let otherrclpy_waitcalls runI'll go ahead and implement this, but feel free to add any feedback in
case there's something I'm missing.
esteve commentedon Feb 12, 2019
@wjwwood I've been working on this, but then I realized that I'd have to call
rcl_waittwice, once with aWaitSetthat only contains theGuardConditionand a timeout set to -1, and then for the existingrcl_waitcall. Is that what you thought or is there more to it? Thanks.First cut at fixing ros2#192
wjwwood commentedon Feb 15, 2019
@esteve sorry, I haven't had time to try and wrap my head around it. I'll get back to you asap.
dejanpan commentedon Mar 4, 2019
@esteve did you want to open a PR that @wjwwood or someone else can look at or how did we want to proceed with this one?
esteve commentedon Mar 4, 2019
@dejanpan I wanted to run it by @wjwwood first to make sure I understood the problem correctly, but I'll create a pull request with what I have just to have something tangible that we discuss over.
wjwwood commentedon Mar 5, 2019
I don't fully follow your second comment about needing to call it twice, but from the bullet list proposal, I'd wonder if it actually solves the issue, because if a waitset is waiting and has not work, but another waitset is waiting on it to finish before actually waiting despite the fact that it has data available, then you'd starve a ready waitset with a blocking waitset. Which effectively doesn't let you wait from two threads at the same time, if I understand it correctly.
Perhaps I don't have a grasp on the solution you've proposed yet. I'm happy to keep discussing it, but a pr, even a partial one, might be a more concrete thing to discuss.
Sorry for taking so long to look at it.
First cut at fixing ros2#192
First cut at fixing ros2#192
esteve commentedon Mar 22, 2019
@wjwwood I pushed master...esteve:thread_safe_rclpy_wait but I'm not sure if that's the correct approach. @pbaughman do you have a test or a script that fails? I'd like to check if my branch fixed the bug. Thanks.
pbaughman commentedon Mar 22, 2019
@esteve - I do not - I'm actually not exactly sure what subtle failures were seen before the big scary runtime error
RuntimeError: Executor.wait_for_ready_callbacks() called concurrently in multiple threads'was added.Maybe something like this:
Would probably want to run it for a while to make sure both nodes continue to receive data
wjwwood commentedon Mar 22, 2019
It is to do with the management of the signal handlers from within the C extension, as the description of the issue says:
So, running it for a long time is not the issue, I think instead you'd need to run it, sigint it, run it again, in a loop until you see (or do not see) some failure or deadlock.
Merge pull request ros2#192 from lucasw/subscriber_any2