apache/cassandra · warning · java.util.NoSuchElementException
Queue is empty
Error message
Queue is empty
What it means
element() is the Queue contract method that retrieves, but does not remove, the head and must throw NoSuchElementException when the queue is empty. ManyToOneConcurrentLinkedQueue implements this by peeking and throwing 'Queue is empty' when peek() returns null. It is the exception-throwing twin of poll().
Source
Thrown at src/java/org/apache/cassandra/concurrent/ManyToOneConcurrentLinkedQueue.java:95
size++;
return size;
}
@Override
public E peek()
{
Node<E> next = head.next;
if (null == next)
return null;
return next.item;
}
@Override
public E element()
{
E item = peek();
if (null == item)
throw new NoSuchElementException("Queue is empty");
return item;
}
@Override
public E poll()
{
Node<E> head = this.head;
Node<E> next = head.next;
if (null == next)
return null;
this.lazySetHead(next); // update head reference to next before making previous head node unreachable,
head.lazySetNext(head); // to maintain the guarantee of tail being always reachable from head
E item = next.item;
next.item = null;
return item;View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Use poll() instead of element() and handle the null return.
- Guard with isEmpty() only when there is a single consumer (this is a many-producer/single-consumer queue; isEmpty then element can still race).
- Wrap in try-catch for NoSuchElementException if the empty case is expected.
- Restructure to block on a condition/semaphore before calling element().
Example fix
// before
E item = queue.element(); // throws NoSuchElementException when empty
// after
E item = queue.poll();
if (item == null) {
// queue empty: wait, back off, or skip
} Defensive patterns
Strategy: type-guard
Validate before calling
// check emptiness before element() (single-consumer only) if (queue.isEmpty()) return null; // or wait/backoff E head = queue.element();
Type guard
E headOrNull(ManyToOneConcurrentLinkedQueue<E> q) { return q.peek(); } // peek returns null instead of throwing Try / catch
try {
E item = queue.element();
process(item);
} catch (java.util.NoSuchElementException e) {
// queue empty: expected, back off or wait
} Prevention
- Prefer poll()/peek() over element()/remove() in consumers.
- Remember this is a many-producer/single-consumer queue; isEmpty checks can race.
- Use blocking queues when consumers must wait.
- Never assume remove()/element() block; they throw immediately when empty.
When it happens
Trigger: Calling element() on a ManyToOneConcurrentLinkedQueue that currently has no elements (peek() == null), e.g. draining a queue without checking emptiness or racing with a consumer that took the last element.
Common situations: Single-consumer loops that call element() instead of poll(); code that checks !isEmpty() but races with another consumer; misuse in tests where the producer hasn't run yet.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- UnsupportedOperationException
- UnsupportedOperationException
- Maximum pool size has been changed while resizing
- Must be one of ${values}
- ${executor} has shut down
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/3021baa3f5b1fa82.
Report an issue: GitHub.