microsoft / microsoft/vs-streamjsonrpc

Disposal of marshallable object followed by disposal of jsonRpc on client leads to InvalidOperationException on server

Open
#1,087 3 comments 0 reactions 1 assignee View on GitHub

@matteo-prosperi is already working on this.

Since Feb 19, 2025.

bug
Dominant language
C#
Stars
937
Forks
178
Avg merge
3d 1h
Merged PRs (30d)
28

Description

Sometimes I get the following exception on server side on 'await jsonRpc.Completion' when jsonRpc is disposed just after disposal of marshallable object on client side:

  System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
     at System.Collections.Generic.List`1.Enumerator.MoveNext()
     at StreamJsonRpc.Reflection.RpcTargetInfo.DisposeAsync()
     at StreamJsonRpc.JsonRpc.JsonRpcDisconnectedShutdownAsync(JsonRpcDisconnectedEventArgs eventArgs)

I believe the reason is a race condition when accessing the localTargetObjectsToDispose field in the RpcTargetInfo class from different threads. There are other non-concurrent collections in the RpcTargetInfo that probably also need attention.

I created a stress test to reproduce this (attached). It may take a while to get the exception in the server console.

vs-streamjsonrpc-test.zip

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.