Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Multiple C# Threads calling WASM Module

未关闭
#331 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
csharp, wasm

调研方向

首先复现使用两个线程的 C# 单元测试场景,然后将其与使用一个线程的情况以及 C# lock 情况进行比较。跟踪 TryVerify 中显示的共享 WASM 调用、字符串分配、内存读取和 _Free 调用。完成后应确定支持的并发调用行为,并提供经过验证的解决方案或明确的限制。

由索引模型根据 Issue 内容生成。

描述

I have tried to run a WASM module from a C# unit test where there are 2 threads calling the module in a loop. This dies with one of the threads ending in with the following exception

wasm trap: call stack exhausted

Here is logged output from the Test where

  • [n] is the thread number
  • Count is the iterations for that thread
  • Error Count is whether the result was in error (as I was checking for race conditions)
  • Exception is the C# exception that was caught from the WASM method Invoke
Debug Trace:
[0]   Count: 0   ErrorCount: 0   Exception: error while executing at wasm backtrace:
    0: <unknown>!<wasm function 37>
    1:  0xb25 - <unknown>!<wasm function 14>
    2: 0x47d8 - <unknown>!<wasm function 37>

Caused by:
    wasm trap: call stack exhausted
[1]   Count: 89498   ErrorCount: 0   Exception: 

It is like the WASM module method is being called OK by one thread while the other thread is looks to be calling the method but never leaving it and the stack eventually blew up??

If I run this as 1 thread OR put a C# Lock {} block around the WASM method call, then it all runs fine, and I have called the method for over 5,000,000 iterations without an issue.

The WASM module was written in C and and generated using Emscripten. The method takes in two string (pointers) and a an Int64 and returns a string (pointer).

The memory is being Free'ed.

C method
char* TryVerify(char* token, char* originHeader, uint64_t serverUtcUnixNowMilliseconds)

C# call

        object lockObject = new object();
        public bool TryVerify(string token, string originHeader, out TokenData tokenData)
        {
            //lock (lockObject)
            //{
                var serverUtcUnixNowMilliseconds = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
                int inPtrToken = WriteStringToUTF8(token);
                int inPtrOriginHeader = WriteStringToUTF8(originHeader);
                int outPtr = _TryVerify.Invoke(inPtrToken, inPtrOriginHeader, serverUtcUnixNowMilliseconds);
                var json = Memory.ReadNullTerminatedString(outPtr);
                _Free(outPtr);

                tokenData = System.Text.Json.JsonSerializer.Deserialize<TokenData>(json, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }) ?? throw new Exception("Failed to deserialize token data from wasm");
                if (tokenData == null)
                {
                    tokenData = new TokenData();
                }

                return tokenData.IsOk;
           // }
        }
主要语言
C#
星标
516
派生
69
平均合并
6 小时 1 分钟
30 天内合并 PR
5

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bytecodealliance/wasmtime-dotnet 的其他 Issue

查看 bytecodealliance/wasmtime-dotnet 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。