You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
#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() throwsStack<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
Priority: Medium. Data integrity: when an element's DeepClone throws, a get-only Stack/Queue/ConcurrentQueue property is left holding part of the source and has lost its own contents. It only happens on that exception path and only on these three types, and the fix is small and follows the existing ConcurrentStack<T> overload.
Area / suggested assignment: the Stack<T>, Queue<T> and ConcurrentQueue<T> overloads of DeepCloneFrom in DeepClone/DeepCloneContainerExtensions.cs, and DeepCloneFromExceptionSafetyTests.
Next step: Clone into an array first, then Clear() and refill, as the issue suggests. Add the four collection types to DeepCloneFromExceptionSafetyTests so a parallel branch can't bring the regression back without a failing test.
Activity
matt-edmondson commentedon Nov 4, 2026
Triage
DeepClonethrows, a get-onlyStack/Queue/ConcurrentQueueproperty is left holding part of the source and has lost its own contents. It only happens on that exception path and only on these three types, and the fix is small and follows the existingConcurrentStack<T>overload.Stack<T>,Queue<T>andConcurrentQueue<T>overloads ofDeepCloneFrominDeepClone/DeepCloneContainerExtensions.cs, andDeepCloneFromExceptionSafetyTests.ConcurrentDictionaryunder concurrent writes), which is still open.Clear()and refill, as the issue suggests. Add the four collection types toDeepCloneFromExceptionSafetyTestsso a parallel branch can't bring the regression back without a failing test.Generated by Claude Code