GeneralPurpose-BlockingCallsInCriticalSection

There shall be no calls to blocking functions in a critical region

Required inputs: IR

Blocking functions like I/O operations, sleep, or receive calls should not be called while holding locks or inside critical sections. Blocking while holding a lock can cause deadlocks, starvation, or severe performance degradation. Other threads waiting for the lock cannot proceed, and the blocking operation may wait indefinitely for resources that other threads need to release.
Bad code (blocking call in critical section):
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *Process(void *) {
    pthread_mutex_lock(&lock);
    char buffer[100];
    fgets(buffer, sizeof(buffer), stdin);  // ERROR: blocking I/O while locked
    pthread_mutex_unlock(&lock);
    return NULL;
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, Process, NULL);
}
Good code (release lock before blocking):
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *Process(void *) {
    char buffer[100];
    fgets(buffer, sizeof(buffer), stdin);  // OK: blocking I/O outside lock

    pthread_mutex_lock(&lock);
    // Process buffer with lock held
    pthread_mutex_unlock(&lock);
    return NULL;
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, Process, NULL);
}

Possible Messages

Key

Text

Severity

Disabled

blocking_call_in_critical_section

Call to possible blocking function ‘{}’ in critical section.

None

False

Options

blocking_function_calls

blocking_function_calls : set[bauhaus.analysis.config.QualifiedName] = {'fgets', 'fopen', 'getc', 'read', 'recv', 'sleep'}

Warn if a call to a function with name in this set, is inside a critical section. E.g. a function that waits for I/O or is very slow.
 

enter_critical_functions

enter_critical_functions

Type: set[bauhaus.analysis.config.QualifiedName]

Default: {'EnterCriticalSection', 'mtx_lock', 'pthread_mutex_lock', 'std::_Mutex_base::lock', 'std::mutex::lock'}

Set of function names to enter a critical region.
 

enter_critical_macros

enter_critical_macros : set[bauhaus.analysis.config.MacroName] = set()

Set of macro names to enter a critical region (macros must expand to asm() statement).
 

exit_critical_functions

exit_critical_functions

Type: set[bauhaus.analysis.config.QualifiedName]

Default: {'ExitCriticalSection', 'mtx_unlock', 'pthread_mutex_unlock', 'std::_Mutex_base::unlock', 'std::mutex::unlock'}

Set of function names to exit a critical region.
 

exit_critical_macros

exit_critical_macros : set[bauhaus.analysis.config.MacroName] = set()

Set of macro names to exit a critical region (macros must expand to asm() statement).
 

nested_critical_regions

nested_critical_regions : bool = True

If set to true, critical regions nest; if set to false, a single exit-critical-region terminates all open critical regions.