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
Sometimes stopping the pipeline causes deadlock due to race condition.
A race condition occurs when you stopping pipeline and call tm2_sensor::stop() --> tm2_sensor::stop_interrupt() and trying to cancel last request but sometimes there are no pended request at that moment. At the same time another thread (which is processing lambda-function _interrupt_callback declared in tm2_sensor::start_interrupt()) just ready to add new request but it will never be canceled.
This code should be fixed.
void tm2_sensor::stop_interrupt()
{
if (_interrupt_request) {
if (_device->cancel_request(_interrupt_request)) {
_interrupt_callback->cancel();
_interrupt_request.reset();
}
}
}
The simplest way to fix it is to move cancelling callback (which has own mutex) outside the cancelling request. This will cancel callback function first and will be guarantee that processing thread will be stopped. For example like this:
void tm2_sensor::stop_interrupt()
{
if (_interrupt_request) {
_interrupt_callback->cancel();
if (_device->cancel_request(_interrupt_request))
_interrupt_request.reset();
}
}
Of course if we want exactly the same behavior (actually the same log-print on stopping), we should add mutex (condition_variable) to this code.
P.S.: all the same with tm2_sensor::stop_stream() and stream request/callback.
The text was updated successfully, but these errors were encountered:
ankyur
changed the title
T265 race condition when stopping the pipline
T265 race condition when stopping the pipeline
Sep 8, 2020
ankyur
changed the title
T265 race condition when stopping the pipeline
T265 deadlock when stopping the pipeline
Sep 10, 2020
krazycoder2k
added a commit
to krazycoder2k/librealsense
that referenced
this issue
Mar 12, 2021
This PR is effectively the suggested fix in the GH issue below. Thank you @ankyur.
IntelRealSense#7276
I've validated the fix works using the following code:
// Reproduces T265 Hand on Exit.
int main(int, char**)
{
constexpr std::chrono::seconds timeout{ 1 };
while (true)
{
// Start
rs2::config config;
rs2::pipeline pipeline;
std::cout << "Entering pipeline.start()" << std::endl;
pipeline.start();
std::cout << "Exiting pipeline.start()" << std::endl;
std::cout << "Sleeping for 1 second..." << std::endl;
std::this_thread::sleep_for(timeout);
std::cout << "Entering pipeline.stop()" << std::endl;
pipeline.stop();
std::cout << "Exiting pipeline.stop()" << std::endl;
}
return 0;
}
Suspect this fix potentially addresses the following open T265 issues as well:
IntelRealSense#7553IntelRealSense#5807IntelRealSense#6272IntelRealSense#7555IntelRealSense#7750
Issue Description
Sometimes stopping the pipeline causes deadlock due to race condition.
A race condition occurs when you stopping pipeline and call tm2_sensor::stop() --> tm2_sensor::stop_interrupt() and trying to cancel last request but sometimes there are no pended request at that moment. At the same time another thread (which is processing lambda-function _interrupt_callback declared in tm2_sensor::start_interrupt()) just ready to add new request but it will never be canceled.
This code should be fixed.
The simplest way to fix it is to move cancelling callback (which has own mutex) outside the cancelling request. This will cancel callback function first and will be guarantee that processing thread will be stopped. For example like this:
Of course if we want exactly the same behavior (actually the same log-print on stopping), we should add mutex (condition_variable) to this code.
P.S.: all the same with tm2_sensor::stop_stream() and stream request/callback.
The text was updated successfully, but these errors were encountered: