{"record":{"id":"3021baa3f5b1fa82","repo":"apache/cassandra","slug":"queue-is-empty","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"java.util.NoSuchElementException","httpStatus":null,"severity":"warning","filePath":"src/java/org/apache/cassandra/concurrent/ManyToOneConcurrentLinkedQueue.java","lineNumber":95,"sourceCode":"            size++;\n        return size;\n    }\n\n    @Override\n    public E peek()\n    {\n        Node<E> next = head.next;\n        if (null == next)\n            return null;\n        return next.item;\n    }\n\n    @Override\n    public E element()\n    {\n        E item = peek();\n        if (null == item)\n            throw new NoSuchElementException(\"Queue is empty\");\n        return item;\n    }\n\n    @Override\n    public E poll()\n    {\n        Node<E> head = this.head;\n        Node<E> next = head.next;\n\n        if (null == next)\n            return null;\n\n        this.lazySetHead(next); // update head reference to next before making previous head node unreachable,\n        head.lazySetNext(head); // to maintain the guarantee of tail being always reachable from head\n\n        E item = next.item;\n        next.item = null;\n        return item;","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/concurrent/ManyToOneConcurrentLinkedQueue.java#L77-L113","documentation":"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().","triggerScenarios":"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.","commonSituations":"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.","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()."],"exampleFix":"// before\nE item = queue.element(); // throws NoSuchElementException when empty\n// after\nE item = queue.poll();\nif (item == null) {\n    // queue empty: wait, back off, or skip\n}","handlingStrategy":"type-guard","validationCode":"// check emptiness before element() (single-consumer only)\nif (queue.isEmpty()) return null; // or wait/backoff\nE head = queue.element();","typeGuard":"E headOrNull(ManyToOneConcurrentLinkedQueue<E> q) { return q.peek(); } // peek returns null instead of throwing","tryCatchPattern":"try {\n    E item = queue.element();\n    process(item);\n} catch (java.util.NoSuchElementException e) {\n    // queue empty: expected, back off or wait\n}","preventionTips":["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."],"tags":["queue","concurrency","no-such-element"],"backgroundTag":"empty-result-set","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}