Skip to content

fix(serialization): record deserialized Stack<T> for reference tracking - #11612

Open
johnkaram02 wants to merge 1 commit into
dotnet:mainfrom
johnkaram02:fix-orleans-x
Open

johnkaram02 wants to merge 1 commit into
dotnet:mainfrom
johnkaram02:fix-orleans-x

Conversation

@johnkaram02

@johnkaram02 johnkaram02 commented Oct 28, 2026 •

Copy link
Copy Markdown

Problem

StackCodec<T>.ReadValue records the temporary element array in the session's reference table instead of the Stack<T> it returns. If an object graph contains the same Stack<T> instance more than once, the writer emits later occurrences as references. Reading one of those references resolves to the array, and deserialization fails:

System.InvalidCastException: Unable to cast object of type 'System.Int32[]' to type 'System.Collections.Generic.Stack`1[System.Int32]'.

Solution

Record the Stack<T> instance (result) instead of the array, which matches the other collection codecs such as QueueCodec. The wire format does not change.

StackCodecTests.PreservesSharedStackReferences round-trips a list that holds the same stack twice. It checks that both entries deserialize to the same Stack<T> instance with the original contents.

Microsoft Reviewers: Open in CodeFlow

StackCodec<T>.ReadValue recorded the temporary element array in the
reference table instead of the Stack<T> it returns. When an object graph
contains the same Stack<T> instance more than once, the serializer writes
later occurrences as references, and reading one of them resolved to the
array and threw InvalidCastException ("Unable to cast object of type
'System.Int32[]' to type 'System.Collections.Generic.Stack`1[System.Int32]'").

The codec now records the Stack<T> instance, so shared references
deserialize to the same stack.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant