Skip to content

Seg fault when accessing Context from multiple threads #5577

@crdumoul

Description

@crdumoul

I've got a test program that does the following in two separate threads. I understand that the runtime and context can only be accessed by one thread at a time.

In the main thread, create a runtime and a context, and execute some JavaScript code to add a function called "square" to the global object. The JavaScript code being run is very simple:
function square(number) { return number * number; }

The API calls in the main thread are the following, in this order:
JsCreateRuntime
JsCreateContext
JsAddRef: passing in the JsContextRef so the Context won't be garbage collected.
JsSetCurrentContext
JsCreateExternalArrayBuffer: create a buffer that points to the JavaScript code to be run.
JsCreateString: create a string for the URL parameter to JsRun
JsRun: run the JavaScript code to create the "square" function.
JsSetCurrentContext(JS_INVALID_REFERENCE)

After that a second thread is spawned. This second thread will be calling the "square" function. It makes the following API calls, in this order:
JsSetCurrentContext
JsGetGlobalObject
JsCreatePropertyId

At this point the second thread occasionally seg faults (SIGSEGV) in the call to JsCreatePropertyId.

The back trace looks like:
#0 0x000055e69854223e in Memory::RecyclerWriteBarrierManager::WriteBarrier(void*) ()
#1 0x000055e69857d6e7 in ThreadContext::GetOrAddPropertyId(char16_t const*, int, Js::PropertyRecord const**) ()
#2 0x000055e6985698ae in Js::ScriptContext::GetOrAddPropertyRecord(char16_t const*, int, Js::PropertyRecord const**) ()
#3 0x000055e6984b252b in JsGetPropertyIdFromNameInternal(char16_t const*, unsigned long, void**)::{lambda(Js::ScriptContext*)#1} ContextAPINoScriptWrapper_Core<_JsErrorCode ContextAPINoScriptWrapper_NoRecord<JsGetPropertyIdFromNameInternal(char16_t const*, unsigned long, void**)::{lambda(Js::ScriptContext*)#1}>(JsGetPropertyIdFromNameInternal(char16_t const*, unsigned long, void**)::{lambda(Js::ScriptContext*)#1}, bool, bool)::{lambda(Js::ScriptContext*)#1}>(_JsErrorCode, bool, bool) ()
#4 0x000055e6984af6cb in JsCreatePropertyId ()

This test program is written in Rust, which is why I'm not just including the source code, but instead trying to describe the API calls I'm making.

I'd appreciate any help in figuring out what I'm doing wrong here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions