Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1083,3 +1083,12 @@ wd_test(
args = ["--experimental"],
data = ["streams/body-read-operation-test.js"],
)

kj_test(
src = "worker-loader-source-test.c++",
deps = [
":worker-loader",
"//src/workerd/io",
"//src/workerd/tests:test-fixture",
],
)
141 changes: 141 additions & 0 deletions src/workerd/api/worker-loader-source-test.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright (c) 2026 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
//
// Tests of how WorkerLoader::get() hands the DynamicWorkerSource that getCode() describes to the
// runtime that loads the Worker.

#include <workerd/api/http.h>
#include <workerd/api/worker-loader.h>
#include <workerd/io/io-context.h>
#include <workerd/jsg/jsg.h>
#include <workerd/tests/test-fixture.h>

#include <kj/test.h>

namespace workerd::api {
namespace {

// Records its destruction. WorkerLoader makes it the loaded Worker's global outbound, so it lives
// exactly as long as the DynamicWorkerSource that holds it.
class TrackedChannel final: public IoChannelFactory::SubrequestChannel {
public:
explicit TrackedChannel(bool& destroyed): destroyed(destroyed) {}
~TrackedChannel() noexcept(false) {
destroyed = true;
}

kj::Own<WorkerInterface> startRequest(IoChannelFactory::SubrequestMetadata) override {
KJ_UNREACHABLE;
}
void requireAllowsTransfer() override {}
kj::OneOf<kj::Array<byte>, kj::Promise<kj::Array<byte>>> getTokenMaybeSync(
IoChannelFactory::ChannelTokenUsage) override {
KJ_UNREACHABLE;
}

private:
bool& destroyed;
};

class NeverUsedStubChannel final: public WorkerStubChannel {
public:
kj::Rc<IoChannelFactory::SubrequestChannel> getEntrypointResolved(
kj::Maybe<kj::String>, Frankenvalue, kj::Maybe<ResourceLimits>) override {
KJ_UNREACHABLE;
}
kj::Rc<IoChannelFactory::ActorClassChannel> getActorClassResolved(
kj::Maybe<kj::String>, Frankenvalue, kj::Maybe<ResourceLimits>) override {
KJ_UNREACHABLE;
}
};

// What the loader channel factory shares with the test.
struct LoaderState {
bool channelDestroyed = false;
kj::Maybe<kj::Function<kj::Promise<DynamicWorkerSource>()>> fetchSource;
};

// Keeps the source callback that WorkerLoader::get() passes to loadIsolate(), so the test can
// call it as the runtime would when it first needs the Worker.
struct LoaderChannelFactory final: public TestFixture::DummyIoChannelFactory {
LoaderChannelFactory(TimerChannel& timer, LoaderState& state)
: DummyIoChannelFactory(timer),
state(state) {}

kj::Rc<SubrequestChannel> getSubrequestChannelResolved(
uint channel, kj::Maybe<Frankenvalue>, kj::Maybe<VersionRequest>, Persistent) override {
KJ_ASSERT(channel == IoContext::NULL_CLIENT_CHANNEL);
return kj::rc<TrackedChannel>(state.channelDestroyed);
}

kj::Own<WorkerStubChannel> loadIsolate(uint,
kj::Maybe<kj::String>,
kj::Function<kj::Promise<DynamicWorkerSource>()> fetchSource) override {
state.fetchSource = kj::mv(fetchSource);
return kj::refcounted<NeverUsedStubChannel>();
}

LoaderState& state;
};

WorkerLoader::WorkerCode makeWorkerCode() {
auto modules = kj::heapArrayBuilder<jsg::Dict<
kj::OneOf<jsg::V8Ref<v8::WasmModuleObject>, WorkerLoader::Module, kj::String>>::Field>(1);
modules.add(kj::str("main.js"), kj::str("export default {}"));
return {
.compatibilityDate = kj::str("2024-01-01"),
.mainModule = kj::str("main.js"),
.modules = {.fields = modules.finish()},
};
}

KJ_TEST("WorkerLoader::get() hands the source to KJ in the microtask that converts it") {
// A limit can terminate execution between two microtasks, discarding the rest of the queue. The
// microtask that converts the WorkerCode into a DynamicWorkerSource must also hand the source to
// the KJ promise returned to the runtime. Were the handoff left to another microtask, it would
// be discarded: the runtime's wait would never settle, and the source, with the KJ channels it
// holds, would stay parked in the V8 heap.
LoaderState state;
TestFixture fixture(TestFixture::SetupParams{
.ioChannelFactory = kj::Function<kj::Rc<IoChannelFactory>(TimerChannel&)>(
[&state](TimerChannel& timer) -> kj::Rc<IoChannelFactory> {
return kj::rc<LoaderChannelFactory>(timer, state);
}),
});
auto request = fixture.newIncomingRequest();
auto& context = request->getContext();

context
.run([&](Worker::Lock& lock) {
auto& js = jsg::Lock::from(lock.getIsolate());
auto loader = js.alloc<WorkerLoader>(0, CompatibilityDateValidation::CODE_VERSION);
loader->get(
js, kj::none, jsg::Function<jsg::Promise<WorkerLoader::WorkerCode>()>([](jsg::Lock& js) {
// Terminates execution in the second round of microtasks: after the microtask that the
// WorkerCode's resolution schedules, and before any microtask that one schedules in turn.
js.resolvedPromise().then(js, [](jsg::Lock&) {}).then(js, [](jsg::Lock& js) {
js.terminateExecutionNow();
});
return js.resolvedPromise(makeWorkerCode());
}));
}).wait(fixture.getWaitScope());

auto promise = KJ_ASSERT_NONNULL(state.fetchSource)();
fixture.pollEventLoop();
// Were the handoff left to a discarded microtask, the wait would never settle, and the source
// would stay in the V8 heap.
KJ_ASSERT(promise.poll(fixture.getWaitScope()), "the termination discarded the source");
{
auto source = promise.wait(fixture.getWaitScope());
KJ_EXPECT(source.globalOutbound != kj::none);
KJ_EXPECT(!state.channelDestroyed);
}
KJ_EXPECT(state.channelDestroyed);

state.fetchSource = kj::none;
fixture.drainAndDestroy(kj::mv(request));
}

} // namespace
} // namespace workerd::api
13 changes: 6 additions & 7 deletions src/workerd/api/worker-loader.c++
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,12 @@ jsg::Ref<WorkerStub> WorkerLoader::get(
[getCode = kj::mv(getCode), compatDateValidation = compatDateValidation](
jsg::Lock& js, IoContext& ioctx) mutable {
// getCode() is application-provided and may resolve its promise from a different context.
// Binding the continuation to this context ensures that it either runs here or throws before
// accessing the context.
return getCode(js).then(js,
ioctx.addFunctor([compatDateValidation](jsg::Lock& js, IoContext& ioctx,
WorkerCode code) -> DynamicWorkerSource {
return toDynamicWorkerSource(js, ioctx, compatDateValidation, kj::mv(code));
}));
// awaitJs() runs the conversion only in this context, and in the same continuation that hands
// the resulting DynamicWorkerSource, which owns KJ objects, to the returned KJ promise.
return ioctx.awaitJs(js, getCode(js),
[compatDateValidation](jsg::Lock& js, WorkerCode code) -> DynamicWorkerSource {
return toDynamicWorkerSource(js, IoContext::current(), compatDateValidation, kj::mv(code));
});
});

auto isolateChannel =
Expand Down
Loading