Skip to content

Stack, Queue and ConcurrentQueue DeepCloneFrom still clear the destination before cloning, so a throwing element leaves it half-filled (#90 regression in the #115 overloads) #124

Closed

Description

@matt-edmondson

What's wrong

#111 (Fixes #90) made DeepCloneFrom clone every item before touching the destination, so an element whose DeepClone throws leaves the destination unchanged. #115 merged three minutes later from a parallel branch and added DeepCloneFrom overloads for Stack<T>, Queue<T>, ConcurrentStack<T> and ConcurrentQueue<T>. Three of the four still use the old order: they clear the destination first and clone inside the refill loop.

DeepClone/DeepCloneContainerExtensions.cs:

  • DeepCloneFrom(this Stack<T> dest, …): dest.Clear(), then dest.Push(DeepClone(items[i])) in the loop
  • DeepCloneFrom(this Queue<T> dest, …): dest.Clear(), then dest.Enqueue(DeepClone(item)) in the loop
  • DeepCloneFrom(this ConcurrentQueue<T> dest, …): clears or drains, then dest.Enqueue(DeepClone(item)) in the loop

The ConcurrentStack<T> overload does this correctly: it builds clones first, then calls Clear() and PushRange.

Reproduction

I ran this on main (34ee710), net10.0, using the throwing item from DeepCloneFromExceptionSafetyTests:

Item[] src = [new("a"), new("boom"), new("c")];   // "boom".DeepClone() throws
Stack<Item> s = new([new("keep1"), new("keep2")]);
Queue<Item> q = new([new("keep1"), new("keep2")]);
ConcurrentQueue<Item> cq = new([new("keep1"), new("keep2")]);
ConcurrentStack<Item> cs = new([new("keep1"), new("keep2")]);
// call DeepCloneFrom(src) on each, catching InvalidOperationException

Contents afterwards:

Destination After the throw Expected
Stack<T> c keep2, keep1
Queue<T> a keep1, keep2
ConcurrentQueue<T> a keep1, keep2
ConcurrentStack<T> keep2, keep1 ✅ keep2, keep1

Why it matters

The example in the README and CLAUDE.md is clone.Collection.DeepCloneFrom(Collection) inside DeepClone(TDerived clone). For a get-only Stack/Queue property, which is exactly the case #114 added these overloads for, a failing element clone now leaves the object holding a fragment of the source with its own contents gone. That is the #90 failure, now only on these types. DeepCloneFromExceptionSafetyTests covers ICollection<T> and IDictionary<,> only, so nothing caught it.

Suggested fix

Clone into an array first in all three overloads, the way the ConcurrentStack<T> one already does, and touch dest only after every clone has succeeded. Push/Enqueue can't refuse an item, so these overloads don't need ReplaceContents's restore path.

Acceptance criteria

  • For each of Stack<T>, Queue<T>, ConcurrentStack<T> and ConcurrentQueue<T>, a DeepCloneFrom whose second element throws leaves the destination with its original contents in the original pop/dequeue order
  • Those four cases are added to DeepCloneFromExceptionSafetyTests
  • The existing InPlaceStackQueueTests (order, self-clone) still pass

Activity

  1. added
    bug
    on Nov 4, 2026
  2. matt-edmondson commented on Nov 4, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions