ClosedQueueError
RubyERRORNotableConcurrency

Operation on a closed Queue

Quick Answer

Check queue.closed? before pushing, or rescue ClosedQueueError in producer threads when a queue shutdown is expected.

What this means

Raised when a push or pop operation is attempted on a Queue or SizedQueue that has been closed. ClosedQueueError is a subclass of StopIteration.

Why it happens
  1. 1A producer thread pushing to a queue after the consumer has closed it
  2. 2Calling Queue#push after Queue#close in a multi-threaded pipeline

Fix

Check closed? before pushing

Check closed? before pushing
queue = Queue.new
# ... later ...
queue.push(item) unless queue.closed?

Why this works

closed? returns true after Queue#close is called, allowing producers to skip the push gracefully.

Code examples
Reproducing the errorruby
q = Queue.new
q.close
q.push('item')
# ClosedQueueError: queue closed
Producer rescue patternruby
begin
  queue.push(work_item)
rescue ClosedQueueError
  break   # stop producing when queue is shut down
end
Ancestry checkruby
ClosedQueueError < StopIteration  # => true
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors