Skip to content

[Bug] Mutex leak in delete_current() when exception thrown under CONSISTENCY_CHECKING #59

@hanqing2025

Description

@hanqing2025

Describe the Bug

A mutex leak in delete_current() causes permanent deadlock when an exception is thrown while holding the lock. When compiled with CONSISTENCY_CHECKING defined, the function acquires a mutex but throws std::runtime_error without releasing it.

Location: src/prioque.cpp (lines 355-386)

void delete_current(Queue * q)
{
    Queue_element temp, prev;

    // lock entire queue
    pthread_mutex_lock(&(q->lock));  // Line 355: Lock acquired

#if defined(CONSISTENCY_CHECKING)
    if(q->queue == 0 || q->current == 0) {
        std::string msg("Null pointer in function delete_current()\n");
        fprintf(stderr, "%s", msg.c_str());
        throw std::runtime_error(msg);  // Line 361: BUG - exception with lock held!
    }
    else
#endif
    {
        // ... deletion logic ...
        free(q->current->info);
        q->current->info = NULL;
        temp = q->current;
        // ... more deletion logic ...
        free(temp);
        (q->queuelength)--;
    }

    nolock_rewind_queue(q);

    // release lock on queue
    pthread_mutex_unlock(&(q->lock));  // Line 386: Never reached on exception
}

Impact:

  • Queue mutex q->lock remains permanently locked when exception is thrown
  • All subsequent queue operations block permanently
  • File carving operations completely stall → Denial of Service

Execution Flow:

Thread 1:
  → pthread_mutex_lock(&(q->lock))       // Lock acquired
  → Check: q->queue == 0 || q->current == 0  // Condition TRUE
  → throw std::runtime_error(msg)        // Exception thrown
  → pthread_mutex_unlock() SKIPPED       // BUG: lock never released
  → q->lock remains locked forever

Thread 2, 3, ...N:
  → pthread_mutex_lock(&(q->lock))       // BLOCKS permanently
  → Complete deadlock state

I would appreciate it if you could review and confirm this potential issue. Thank you for your time and for maintaining this project!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions