Exception safety in an SPSC ring: a batch push rolls back, a batch pop cannot
A batch push into a ring buffer and a batch pop out of it look like mirror images, and for exception safety they are not. If the element type can throw while it is being constructed or moved, the push can act as though the batch never happened, while the pop cannot. That is not a matter of taste. At the instant of the throw, the two ends of the queue disagree about who owns the element, and the recovery has to follow the ownership instead of the symmetry.
I keep a single-producer single-consumer queue, spscring, as a written-down reference: the ordinary queue with the reasoning attached rather than a cleverer one. Its two batch operations, try_push_n and try_pop_n, are both members of detail::ring_core in include/spscring/spscring.hpp. They sit a few dozen lines apart and they recover in opposite directions.
The protocol the recovery has to fit into#
Two counters, never reset. write_ advances on a push, read_ on a pop, and a slot is the counter masked down to the capacity. Each thread writes exactly one of the two, which is why the producer may load write_ relaxed and must load read_ with acquire. Stripped of the queue, the whole protocol is the control program in tests/race_control.cpp:
std::thread producer([] {
for (int i = 0; i < total; ++i) {
for (;;) {
const std::size_t write = write_index.load(std::memory_order_relaxed);
const std::size_t read = read_index.load(std::memory_order_acquire);
if (write - read != capacity) {
slots[write & (capacity - 1)] = i;
write_index.store(write + 1, publish);
break;
}
std::this_thread::yield();
}
}
});That file builds this loop three times (once correct, once with the consumer's acquire load relaxed, once with the producer's release store relaxed) so that the ThreadSanitizer run which passes has been shown capable of failing. The header itself offers no way to ask for the wrong ordering, which is why the broken builds had to be written by hand.
The point for this article is narrower. The release store on write_ is what makes an element visible to the consumer, and therefore what makes it exist as far as the other thread is concerned. Everything about exception recovery follows from that one fact. The queue raises nothing of its own: every exception that reaches a catch block inside it arrived through T, or through the caller's output range.
Producer side: nothing published, so nothing happened#
try_push_n loads write_ relaxed and read_ with acquire, works out how much room there is, and then placement-constructs a T from *first into raw_slot(write + built) for each slot, inside a try block. If a constructor throws, the catch destroys every element this call already built, reaching them through element(write + built), and rethrows. write_ is never stored to. Only after every element exists does one write_.store(write + count, std::memory_order_release) publish them all.
Here is the shape of it, with the names as they are in the header:
std::size_t built = 0;
const std::size_t write = write_.load(std::memory_order_relaxed);
const std::size_t read = read_.load(std::memory_order_acquire);
const std::size_t room = capacity() - (write - read);
const std::size_t count = room < n ? room : n;
try {
for (; built < count; ++built, ++first) {
new (raw_slot(write + built)) T(*first);
}
} catch (...) {
while (built > 0) element(write + --built).~T();
throw; // write_ never moved: the batch did not happen
}
write_.store(write + count, std::memory_order_release);
return count;The rollback is free, and it is free for a structural reason: an unpublished element is observable by exactly one thread, the one that built it. Destroying it is not a change of state that anyone can see. The consumer's acquire load of write_ returns the same value it would have returned if the call had never been made, so from the consumer's side the batch is not a failed batch. It is not a batch at all.
The same reasoning covers the ordinary refusal. A batch push takes the room there is and reports how much that was, and when there is no room it stores to no index:
CHECK(q.try_push_n(std::begin(values), 6) == 4); // asked for six, took four
CHECK(q.full());
CHECK(q.try_push_n(std::begin(values), 2) == 0);
CHECK(q.size() == 4);
CHECK(q.try_push_n(std::begin(values), 0) == 0);
CHECK(q.try_pop_n(out, 0) == 0);
CHECK(q.size() == 4);A refusal and a throw leave the queue in the same state, because neither of them reached the release store.
Consumer side: the element is already gone#
try_pop_n mirrors the push everywhere except the recovery. It moves each element into *out and destroys its slot as it goes. If a handover throws, the catch stores read + moved with release before rethrowing, so the elements already written into the caller's range are released and the one whose move threw stays queued. On success a single read_.store(read + count, std::memory_order_release) hands all of them back.
std::size_t moved = 0;
const std::size_t read = read_.load(std::memory_order_relaxed);
const std::size_t write = write_.load(std::memory_order_acquire);
const std::size_t count = (write - read) < n ? (write - read) : n;
try {
for (; moved < count; ++moved, ++out) {
*out = std::move(element(read + moved));
element(read + moved).~T();
}
} catch (...) {
read_.store(read + moved, std::memory_order_release); // release what left
throw;
}
read_.store(read + count, std::memory_order_release);
return count;No rollback is available here, and it is worth being precise about why not. Putting an element back would mean moving it from the caller's range into the slot, which is another move of the same type that has just demonstrated it can throw, and the slot's destructor has already run. A rollback whose failure mode is a second throw inside a catch block is not a rollback. So the queue does not attempt one. It tells the truth instead: these moved elements are yours, the rest are still mine.
That is why the ordering table in the header and the README has a row that reads oddly until you know what it is for: pop_n's read_.store, "one for the batch and one more on the throwing path". The extra store is not a second publication. It is the same single-step handover, taken earlier than planned, with a smaller count.
The single-element pop takes the same stance#
try_pop fills its std::optional before the release store, for exactly the same reason, and this is the one piece of the asymmetry with a dedicated test. tests/test_spscring.cpp defines a type whose move constructor throws once a budget runs out, and then spends the budget:
int move_budget = 0;
struct fragile {
int value = 0;
explicit fragile(int v) : value(v) {}
fragile(fragile&& other) : value(other.value) {
if (move_budget == 0) throw std::runtime_error("no moves left");
--move_budget;
}
fragile(const fragile&) = delete;
fragile& operator=(const fragile&) = delete;
};With no budget at all, the move throws while the element still belongs to the queue, so nothing is lost and a retry gets it:
move_budget = 0;
bool threw = false;
try {
const std::optional<fragile> nothing = stuck.try_pop();
CHECK(!nothing.has_value()); // not reached: the move had no budget
} catch (const std::runtime_error&) {
threw = true;
}
CHECK(threw);
CHECK(stuck.size() == 2);
move_budget = 1;
const std::optional<fragile> retried = stuck.try_pop();
CHECK(retried.has_value() && retried->value == 0);
CHECK(stuck.size() == 1);The failure mode this rules out is the tempting ordering: store first, then move. That version counts the element as popped and then throws while producing it, and the element exists nowhere afterwards. One element silently missing from a queue, on a code path that only runs when a constructor throws, is the kind of bug that gets found years later by a reconciliation report rather than by a test.
What the invariant actually is#
It is not atomicity. A partial pop is observable: the caller holds some elements, the queue holds the rest, and nobody can pretend otherwise. What survives is conservation. Every element exists exactly once, in the queue or in the caller's range, never both, never neither, and the count the queue reports agrees with that. Atomicity on the consumer side is the thing you give up to keep conservation, and conservation is the property the calling code is actually built on. I have spent enough time on payment and reconciliation code to prefer an honest partial result over an operation that claims all-or-nothing and occasionally loses one.
The alternative designs are real, and both cost something. Staging the batch in temporary storage before handing it over buys atomicity on the pop, at the price of an allocation the queue has promised not to make after construction and an extra move per element, which is more throw surface, not less. Requiring a nothrow-movable T buys the whole problem away by contract, and narrows the set of types the queue accepts. The option taken here is the third one: let the partial result be reported, and write down where the extra release store comes from.
What I would do differently#
One thing I underweighted at first: on the pop side the throw does not have to come from T. The README's own example reads try_pop_n(std::back_inserter(batch), 32), and an inserting iterator allocates. The exception that triggers the partial release can originate entirely in the caller's output range, with a perfectly well-behaved element type. The recovery handles it correctly either way, since it only ever asks how many elements have left, but the documentation should say so: a reader who has proved their T cannot throw may conclude the catch block is dead code, and it is not.
The other thing is the interface. A caller who catches an exception out of try_pop_n knows that some prefix was delivered and has to work out the length from the output iterator. Handing the count back on the throwing path, by throwing a type that carries it, or by offering a non-throwing variant that reports the count and the pending exception, would save the caller a reconstruction. I have not changed it, because the simpler signature is worth something too and the throwing path is rare by construction. But if I were starting the header again, the count would come out of the failure instead of being inferred from it.
The general rule I would keep regardless: in a queue, exception recovery is decided by who owns the element when the throw lands, and publication is what transfers ownership. Everything before the release store can be erased. Nothing after it can.