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

T265 deadlock when stopping the pipeline #7276

Open
ankyur opened this issue Sep 8, 2020 · 0 comments
Open

T265 deadlock when stopping the pipeline #7276

ankyur opened this issue Sep 8, 2020 · 0 comments
Labels
T260 series Intel® T265 library

Comments

@ankyur
Copy link

ankyur commented Sep 8, 2020

Required Info
Camera Model T265
Firmware Version 0.2.0.951
Operating System & Version Win10 Build 2004
Platform PC
SDK Version v2.38.1 (master)
Language C++

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.

    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.

@ankyur ankyur changed the title T265 race condition when stopping the pipline T265 race condition when stopping the pipeline Sep 8, 2020
@ankyur 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#7553
IntelRealSense#5807
IntelRealSense#6272
IntelRealSense#7555
IntelRealSense#7750
@RealSenseSupport RealSenseSupport added the T260 series Intel® T265 library label Mar 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T260 series Intel® T265 library
Projects
None yet
Development

No branches or pull requests

2 participants