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

[TSAN] unexpected deadlock detection. #62370

Open
JackyWoo opened this issue Apr 26, 2023 · 2 comments
Open

[TSAN] unexpected deadlock detection. #62370

JackyWoo opened this issue Apr 26, 2023 · 2 comments
Labels
compiler-rt:tsan Thread sanitizer

Comments

@JackyWoo
Copy link

JackyWoo commented Apr 26, 2023

Env

$ clang -v
Ubuntu clang version 15.0.7
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Selected multilib: .;@m64

Description

  1. No deadlock, but report potential deadlock
// not deadlock, but report potential deadlock
void notDeadlockButReport()
{
    std::mutex m1;
    std::mutex m2;

    // m1 => m2
    std::cout << "t try to acquire m1\n";
    m1.lock();
    std::cout << "t got m1\n";

    //std::this_thread::sleep_for(std::chrono::microseconds (2)); // wait main got m2

    // code never executed
    std::cout << "t try to acquire m2\n";
    m2.lock();
    std::cout << "t got m2\n";

    m2.unlock();
    m1.unlock();

    // m2 => m1
    std::cout << "main try to acquire m2\n";
    m2.lock();
    std::cout << "main got m2\n";

    // code never executed
    std::cout << "main try to acquire m1\n";
    m1.lock();
    std::cout << "main got m1\n";

    m1.unlock();
    m2.unlock();
}
  1. Deadlock but not report
void deadlockButNotReport()
{
    std::mutex m1;
    std::mutex m2;

    std::thread t([&] {

        std::cout << "t try to acquire m1\n";
        m1.lock();
        std::cout << "t got m1\n";

        std::this_thread::sleep_for(std::chrono::milliseconds (20)); // wait main got m2

        std::cout << "t try to acquire m2\n";
        m2.lock();
        std::cout << "t got m2\n"; // code never executed

        m2.unlock();
        m1.unlock();
    });

    std::this_thread::sleep_for(std::chrono::milliseconds(10)); // wait t got m1

    std::cout << "main try to acquire m2\n";
    m2.lock();
    std::cout << "main got m2\n";

    std::cout << "main try to acquire m1\n";
    m1.lock();
    std::cout << "main got m1\n"; // code never executed

    m1.unlock();
    m2.unlock();

    t.join();
}
@JackyWoo JackyWoo changed the title TSAN: unexpected deadlock detection. [TSAN] unexpected deadlock detection. Apr 26, 2023
@EugeneZelenko EugeneZelenko added compiler-rt:tsan Thread sanitizer and removed new issue labels Apr 26, 2023
@EugeneZelenko
Copy link
Contributor

Could you please try 16 or main branch?

@JackyWoo
Copy link
Author

@EugeneZelenko I tried with clang-16, the result turn out the same with clang-15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt:tsan Thread sanitizer
Projects
None yet
Development

No branches or pull requests

2 participants