Skip to content

JSMarshaller is not thread-safe: concurrent conversions corrupt _expressionNames #521

Open

Description

@r-Larch

JSMarshaller.FullMethodName reads and writes the private HashSet<string> _expressionNames without synchronization (src/NodeApi.DotNetHost/JSMarshaller.cs, unchanged on main since #467). When several threads build conversions through one shared JSMarshaller at the same time, the set gets corrupted. That happens, for example, when several NodeEmbeddingThreadRuntimes initialize linked modules that share a marshaller.

GetToJSValueExpression then throws JSMarshallerException, whose innermost exception is one of:

  • InvalidOperationException: "Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state."
  • IndexOutOfRangeException

The stack always ends in HashSet<string>.Contains or HashSet<string>.Add, inside JSMarshaller.FullMethodName, called from BuildFromJSFunctionExpression.

In about a quarter of the failing rounds the instance stays broken: later single-threaded conversions of new types through the same marshaller fail as well.

In our app, a linked module's exports stopped at the key whose conversion failed. Scripts then saw errors like binding.groupPeriods is not a function, and the module stayed broken for the life of the process.

Repro

Microsoft.JavaScript.NodeApi and Microsoft.JavaScript.NodeApi.DotNetHost 0.9.42, net10.0, Windows. 12 threads start together from a Barrier, and each converts a delegate type through one new JSMarshaller. This repeats for 1000 rounds.

JsmRepro.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.9.42" />
    <PackageReference Include="Microsoft.JavaScript.NodeApi.DotNetHost" Version="0.9.42" />
  </ItemGroup>
</Project>
Program.cs
using Microsoft.JavaScript.NodeApi.DotNetHost;

// Many threads build their first conversions through one shared JSMarshaller at the same moment.
const int threads = 12, rounds = 1000;
Type[] types = [
    typeof(Func<int, string, string, Task<string>>),
    typeof(Func<string, string?, string>),
    typeof(Func<int, int, int>),
    typeof(Func<string, Task<string>>),
    typeof(Func<string, string, Task<string>>),
    typeof(Action<int>),
];
int failedRounds = 0, brokenAfterwards = 0;
var causes = new Dictionary<string, int>();

for (var round = 0; round < rounds; round++) {
    var marshaller = new JSMarshaller();
    using var barrier = new Barrier(threads);
    var failed = false;
    var workers = Enumerable.Range(0, threads).Select(i => Task.Factory.StartNew(() => {
        barrier.SignalAndWait();
        try {
            marshaller.GetToJSValueExpression(types[i % types.Length]);
        }
        catch (Exception ex) {
            var root = ex;
            while (root.InnerException is not null) root = root.InnerException;
            lock (causes) {
                failed = true;
                var key = $"{root.GetType().Name}: {root.Message.Split('\n')[0]}";
                causes[key] = causes.GetValueOrDefault(key) + 1;
            }
        }
    }, TaskCreationOptions.LongRunning)).ToArray();
    Task.WaitAll(workers);
    if (!failed) continue;
    failedRounds++;

    // Single-threaded now: does the same instance still work for a type it has not seen?
    try { marshaller.GetToJSValueExpression(typeof(Func<double, double>)); }
    catch { brokenAfterwards++; }
}

Console.WriteLine($"{failedRounds} of {rounds} rounds failed; {brokenAfterwards} of those marshallers still fail single-threaded afterwards");
foreach (var (key, count) in causes.OrderByDescending(c => c.Value)) Console.WriteLine($"  {count,5} x {key}");
return failedRounds > 0 ? 1 : 0;

Output from one run (other runs failed 40 and 72 of 1000 rounds):

45 of 1000 rounds failed; 13 of those marshallers still fail single-threaded afterwards
     91 x InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.
     19 x IndexOutOfRangeException: Index was outside the bounds of the array.

Possible fixes

  • Take a lock around the Contains/Add loop in FullMethodName, or use a ConcurrentDictionary<string, byte>.
  • Or document that a JSMarshaller instance must not be used from several threads at once.

In the meantime, we work around it with a lock around all of our own conversions through the shared instance.

Activity

  1. r-Larch commented on Nov 19, 2026

    @r-Larch
    ContributorAuthor

    Vladimir Morozov (@vmoroz), a follow-up after digging further, and a check against #500.

    A correction to the report above: the library's own paths are not affected. JSMarshaller.Current is [ThreadStatic] (JSMarshaller.cs:54), and ManagedHost creates its marshaller through it, so each JS thread gets its own instance. The race only happens when an embedder shares one JSMarshaller across threads. We did exactly that: one static marshaller for the linked modules of several NodeEmbeddingThreadRuntimes. Since #512, linked modules initialize within the runtime's scope, and sharing their marshaller across runtimes started on parallel threads looked natural. Nothing in the API says it is unsafe.

    I also checked #500: it changes JSMarshaller only for the module lookup (JSValueScope.Current.Module) and doesn't touch _expressionNames or FullMethodName, so it neither fixes nor affects this.

    Two options, whichever you prefer:

    1. Make it safe to share: a lock around the Contains/Add loop in FullMethodName. It only runs when an expression is built, not per call. I can send a small PR with the repro above as a test.
    2. Document it: an XML doc on JSMarshaller saying an instance must not be used from several threads at once, and to use JSMarshaller.Current or one instance per runtime instead.

    We now use a lock on our side, so there is no urgency for us.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions