From c181cf45a7d3993c2936ef02de228ec9a42e7460 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 29 Jul 2024 17:19:47 -0700 Subject: [PATCH] Fixes #12910 (#12911) --- src/bun.js/bindings/ModuleLoader.cpp | 18 ++++++++++-------- test/regression/issue/12910/12910.test.ts | 18 ++++++++++++++++++ test/regression/issue/12910/t.mjs | 10 ++++++++++ test/regression/issue/12910/t3.mjs | 1 + 4 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 test/regression/issue/12910/12910.test.ts create mode 100644 test/regression/issue/12910/t.mjs create mode 100644 test/regression/issue/12910/t3.mjs diff --git a/src/bun.js/bindings/ModuleLoader.cpp b/src/bun.js/bindings/ModuleLoader.cpp index d5a27f3a8b..63655b948e 100644 --- a/src/bun.js/bindings/ModuleLoader.cpp +++ b/src/bun.js/bindings/ModuleLoader.cpp @@ -418,17 +418,19 @@ extern "C" void Bun__onFulfillAsyncModule( auto specifierValue = Bun::toJS(globalObject, *specifier); if (auto entry = globalObject->esmRegistryMap()->get(globalObject, specifierValue)) { - if (res->result.value.commonJSExportsLen) { - if (entry.isObject()) { - if (auto isEvaluated = entry.getObject()->getIfPropertyExists(globalObject, Bun::builtinNames(vm).evaluatedPublicName())) { - if (isEvaluated.isTrue()) { - // it's a race! we lost. - // https://github.com/oven-sh/bun/issues/6946 - return; - } + if (entry.isObject()) { + auto* object = entry.getObject(); + if (auto state = object->getIfPropertyExists(globalObject, Bun::builtinNames(vm).statePublicName())) { + if (state.toInt32(globalObject) > JSC::JSModuleLoader::Status::Fetch) { + // it's a race! we lost. + // https://github.com/oven-sh/bun/issues/6946 + // https://github.com/oven-sh/bun/issues/12910 + return; } } + } + if (res->result.value.commonJSExportsLen) { auto created = Bun::createCommonJSModule(jsCast(globalObject), specifierValue, res->result.value); if (created.has_value()) { JSSourceCode* code = JSSourceCode::create(vm, WTFMove(created.value())); diff --git a/test/regression/issue/12910/12910.test.ts b/test/regression/issue/12910/12910.test.ts new file mode 100644 index 0000000000..149c6b81de --- /dev/null +++ b/test/regression/issue/12910/12910.test.ts @@ -0,0 +1,18 @@ +import { test, expect } from "bun:test"; +import { bunEnv, bunExe } from "harness"; +import { join } from "path"; +test("12910", async () => { + const buns = Array.from( + { length: 25 }, + () => + Bun.spawn({ + cmd: [bunExe(), join(import.meta.dir, "t.mjs")], + cwd: import.meta.dir, + stdio: ["inherit", "inherit", "inherit"], + env: bunEnv, + }).exited, + ); + + const exited = await Promise.all(buns); + expect(exited).toEqual(Array.from({ length: 25 }, () => 0)); +}); diff --git a/test/regression/issue/12910/t.mjs b/test/regression/issue/12910/t.mjs new file mode 100644 index 0000000000..7b6e965be8 --- /dev/null +++ b/test/regression/issue/12910/t.mjs @@ -0,0 +1,10 @@ +// Test should fail if thrown exception is not caught +process.exitCode = 1; + +try { + import("./t3.mjs"); + require("./t3.mjs"); +} catch (e) { + console.log(e); + process.exitCode = 0; +} diff --git a/test/regression/issue/12910/t3.mjs b/test/regression/issue/12910/t3.mjs new file mode 100644 index 0000000000..45b603f0ed --- /dev/null +++ b/test/regression/issue/12910/t3.mjs @@ -0,0 +1 @@ +throw new Error("intentional error");