Blocking call made while holding a lock
ID |
c.maintainability.blocking_call_in_critical_section |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Concurrency |
Language |
C / C++ |
Description
This blocking call runs while a lock acquired earlier in the same scope is still held. Every other thread that needs the same mutex now waits for however long this call takes, turning a short critical section into a potentially unbounded stall and, if the call can itself wait on that lock indirectly, a deadlock. Move the wait outside the locked region, or restructure so the lock is released before the blocking call runs.
Rationale
This blocking call runs while a lock acquired earlier in the same scope is still held. Every other thread that needs the same mutex now waits for however long this call takes, turning a short critical section into a potentially unbounded stall and, if the call can itself wait on that lock indirectly, a deadlock. Move the wait outside the locked region, or restructure so the lock is released before the blocking call runs.
The following code illustrates the pattern detected by this rule:
std::mutex m;
void sleeps_while_locked()
{
std::lock_guard<std::mutex> guard(m);
// FLAGGED: Blocking call made while holding a lock
std::this_thread::sleep_for(std::chrono::seconds(1));
}
Remediation
Follow secure coding practices and review the references below for detailed remediation guidance.