Skip to content

Commit 26d929b

Browse files
authored
Merge pull request #8708 from cloudflare/littledivy/2026-11-20-modules-terminate-test
Test that a terminating Rust builtin module stays a termination
2 parents 4169bf4 + 1131c79 commit 26d929b

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎src/workerd/io/io-context/io-context-test.c++‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,7 @@ KJ_TEST("an await_js() whose promise GC collects in another request releases its
26562656

26572657
constexpr kj::StringPtr PROBE_SPECIFIER = "test:io-context-probe"_kj;
26582658
constexpr kj::StringPtr FAILING_SPECIFIER = "test:io-context-failing"_kj;
2659+
constexpr kj::StringPtr TERMINATING_SPECIFIER = "test:io-context-terminating"_kj;
26592660

26602661
// Has `registerModules` add Rust modules to a legacy registry, then calls `body` with the
26612662
// registry inside a request.
@@ -2698,6 +2699,20 @@ void withNewRustRegistry(auto&& registerModules, auto&& body) {
26982699
fixture.runInIoContext([&](const TestFixture::Environment& env) { body(env); });
26992700
}
27002701

2702+
// Whether calling `fn` throws `JsExceptionThrown` with execution terminating. Cancels the
2703+
// termination.
2704+
bool terminates(const TestFixture::Environment& env, auto&& fn) {
2705+
// NOLINTNEXTLINE(custom-raw-v8-trycatch): the test only checks that the termination stays.
2706+
v8::TryCatch tryCatch(env.isolate);
2707+
KJ_DEFER(env.isolate->CancelTerminateExecution());
2708+
try {
2709+
fn();
2710+
return false;
2711+
} catch (jsg::JsExceptionThrown&) {
2712+
return tryCatch.HasTerminated();
2713+
}
2714+
}
2715+
27012716
// What calling `fn` throws, as a string.
27022717
kj::String thrownBy(jsg::Lock& js, auto&& fn) {
27032718
return js.tryCatch([&]() -> kj::String {
@@ -3077,5 +3092,20 @@ KJ_TEST("the new registry throws the error of a failing Rust module callback") {
30773092
});
30783093
}
30793094

3095+
KJ_TEST("the legacy registry keeps the termination of a terminating Rust module callback") {
3096+
withLegacyRustRegistry(register_terminating_module, [](const auto& env, auto& registry) {
3097+
KJ_EXPECT(terminates(env, [&] {
3098+
registry.resolve(env.js, kj::Path::parse(TERMINATING_SPECIFIER), kj::none,
3099+
jsg::ModuleRegistry::ResolveOption::INTERNAL_ONLY);
3100+
}));
3101+
});
3102+
}
3103+
3104+
KJ_TEST("the new registry keeps the termination of a terminating Rust module callback") {
3105+
withNewRustRegistry(register_terminating_module, [](const auto& env) {
3106+
KJ_EXPECT(terminates(env, [&] { env.js.resolveInternalModule(TERMINATING_SPECIFIER); }));
3107+
});
3108+
}
3109+
30803110
} // namespace
30813111
} // namespace workerd::io_context::test

‎src/workerd/io/io-context/test_bridge.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ mod ffi {
183183
unsafe fn await_and_record_async_context(isolate: usize) -> Result<()>;
184184
fn register_probe_module(registry: Pin<&mut ModuleRegistry>);
185185
fn register_failing_module(registry: Pin<&mut ModuleRegistry>);
186+
fn register_terminating_module(registry: Pin<&mut ModuleRegistry>);
186187
fn take_probe_observation() -> ProbeObservation;
187188
unsafe fn await_released(isolate: usize, ending: AwaitEnding) -> Result<()>;
188189
fn release_await(value: i32);
@@ -389,6 +390,17 @@ fn register_failing_module(registry: Pin<&mut ffi::ModuleRegistry>) {
389390
);
390391
}
391392

393+
/// Registers the internal module `test:io-context-terminating`, whose callback terminates
394+
/// execution.
395+
fn register_terminating_module(registry: Pin<&mut ffi::ModuleRegistry>) {
396+
jsg::modules::add_builtin(
397+
registry,
398+
"test:io-context-terminating",
399+
|lock| -> jsg::Result<jsg::Rc<probe::Probe>> { Err(lock.terminate_execution_now()) },
400+
jsg::modules::ModuleType::Internal,
401+
);
402+
}
403+
392404
/// What the probe module's closure saw when it last ran, which is then forgotten.
393405
fn take_probe_observation() -> ffi::ProbeObservation {
394406
PROBE_OBSERVATION.with(|cell| cell.replace(ffi::ProbeObservation::NotRun))

0 commit comments

Comments
 (0)