openage icon indicating copy to clipboard operation
openage copied to clipboard

Deadlock and Undefined Behavior in `ConcurrentQueue::pop()` on Empty Queue

Open codeCraft-Ritik opened this issue 1 month ago โ€ข 1 comments


  • Subsystem: Concurrency & Engine Core (libopenage/datastructure)
  • Affected File: libopenage/datastructure/concurrent_queue.h, lines 41โ€“47 and 50โ€“70
  • Severity: Critical (Engine Thread Deadlock / ISO C++ Undefined Behavior)

Description & Root Cause

ConcurrentQueue<T> provides thread-safe producer-consumer communication using std::recursive_mutex and std::condition_variable_any. Both overloads of pop() acquire the queue's mutex via std::scoped_lock before calling front():

template <typename... None, typename U = T>
T pop(...) {
    static_assert(sizeof...(None) == 0, "User-specified template arguments are prohibited.");
    std::scoped_lock lock{this->mutex}; // Lock depth = 1
    T ret = std::move(this->front());   // Calls front()
    this->queue.pop();
    return ret;
}

Inside front(), a second lock on the same recursive mutex is acquired:

T &front() {
    std::unique_lock<mutex_t> lock{this->mutex}; // Lock depth = 2
    while (this->queue.empty()) {
        this->elements_available.wait(lock);     // Unlocks ONLY depth 2 -> depth 1!
    }
    return this->queue.front();
}

When a consumer thread calls pop() on an empty queue:

  1. pop() locks this->mutex (lock depth = 1).
  2. pop() invokes front(), which constructs std::unique_lock<mutex_t> lock{this->mutex} (lock depth = 2).
  3. The queue is empty, so front() calls this->elements_available.wait(lock).
  4. Per the ISO C++ standard, std::condition_variable_any::wait(lock) only unlocks the lock passed to it (depth 2 $\to$ 1). The outer lock held by pop() remains active.
  5. When a producer thread calls push(), it attempts std::unique_lock<mutex_t> lock{this->mutex}. Because the consumer thread still owns this->mutex at depth 1, the producer thread blocks indefinitely.
  6. The consumer thread waits forever for a notification that the producer cannot send because the producer is blocked waiting on the mutex. The system is permanently deadlocked.

Furthermore, according to ISO C++ [thread.condition.condvarany] ยง4:

"Requires: lock is locked by the execution agent that called wait, or if lock is recursively locked, the behavior is undefined."

Calling elements_available.wait(lock) while holding multiple lock levels on a recursive mutex is explicitly Undefined Behavior.

Additionally, front() returns a raw reference T & to this->queue.front() while releasing its local unique_lock on exit. If callers invoke front() directly, they receive a reference to internal container memory without holding the queue lock, causing data races and dangling references if another thread pops or clears the queue.

Steps to Reproduce

  1. Create a ConcurrentQueue<int> queue.
  2. Spawn a consumer thread that calls queue.pop().
  3. Spawn a producer thread that sleeps for 50ms and calls queue.push(42).
  4. Observe that the producer blocks on this->mutex inside push(), the consumer never awakens, and the process hangs permanently.

Expected Behavior

A consumer calling pop() on an empty queue should release all locks, allowing producer threads to acquire the mutex, push data, and notify the condition variable.

Suggested Fix

Do not acquire the mutex in pop() before calling front(). Instead, use a non-recursive std::mutex, std::condition_variable, and encapsulate the pop operation with a single lock scope:

--- a/libopenage/datastructure/concurrent_queue.h
+++ b/libopenage/datastructure/concurrent_queue.h
@@ -20,7 +20,7 @@ template <typename T>
 class ConcurrentQueue {
-	using mutex_t = std::recursive_mutex;
+	using mutex_t = std::mutex;
 
@@ -40,11 +40,12 @@ public:
 	T &front() {
-		std::unique_lock<mutex_t> lock{this->mutex};
-		while (this->queue.empty()) {
-			this->elements_available.wait(lock);
-		}
+		std::scoped_lock lock{this->mutex};
+		ENSURE(!this->queue.empty(), "front() called on empty queue");
 		return this->queue.front();
 	}
 
@@ -63,5 +64,6 @@ public:
 	template <typename... None, typename U = T>
 	T pop([[maybe_unused]] typename std::enable_if_t<std::is_move_constructible_v<U>> *t = nullptr) {
 		static_assert(sizeof...(None) == 0, "User-specified template arguments are prohibited.");
-		std::scoped_lock lock{this->mutex};
-		T ret = std::move(this->front());
+		std::unique_lock<mutex_t> lock{this->mutex};
+		this->elements_available.wait(lock, [this] { return !this->queue.empty(); });
+		T ret = std::move(this->queue.front());
 		this->queue.pop();
 		return ret;
 	}

codeCraft-Ritik avatar Oct 28 '26 04:10 codeCraft-Ritik

thanks! as i can't integrate this right now, could you please also submit the fix as pull req?

TheJJ avatar Nov 01 '26 23:11 TheJJ