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
55 changes: 34 additions & 21 deletions src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,10 @@ struct Worker::Script::Impl {

kj::Maybe<jsg::JsContext<api::ServiceWorkerGlobalScope>> moduleContext;

// A service worker script is compiled in the context that its first Worker then takes, so
// that no throwaway context is deserialized before it.
kj::Maybe<jsg::JsContext<api::ServiceWorkerGlobalScope>> firstWorkerContext;

// If set, then any attempt to use this script shall throw this exception.
kj::Maybe<kj::Exception> permanentException;

Expand Down Expand Up @@ -1554,6 +1558,11 @@ Worker::Script::Script(kj::Own<const Isolate> isolateParam,
} else {
// Else block to avoid dangling else clang warning.
}
KJ_IF_SOME(c, implToDestroy->firstWorkerContext) {
recordedLock.disposeContext(kj::mv(c));
} else {
// Else block to avoid dangling else clang warning.
}
discardDeferredCodeCaches(lock);
});

Expand All @@ -1574,11 +1583,7 @@ Worker::Script::Script(kj::Own<const Isolate> isolateParam,
v8::Local<v8::Context> context;
if (modular) {
// Modules can't be compiled for multiple contexts. We need to create the real context now.
auto& mContext = impl->moduleContext.emplace(isolate->getApi().newContext(lock,
{
.newModuleRegistry = impl->tryGetNewModuleRegistry(),
.schemaLoader = getSchemaLoader(),
}));
auto& mContext = impl->moduleContext.emplace(newContext(lock));
mContext->enableWarningOnSpecialEvents();
context = mContext.getHandle(lock);
recordedLock.setupContext(context);
Expand Down Expand Up @@ -1625,16 +1630,10 @@ Worker::Script::Script(kj::Own<const Isolate> isolateParam,
}
}
} else {
// Although we're going to compile a script independent of context, V8 requires that
// there be an active context, otherwise it will segfault, I guess. So we create a
// dummy context. (Undocumented, as usual.)
context =
v8::Context::New(lock.v8Isolate, nullptr, v8::ObjectTemplate::New(lock.v8Isolate));
// We need to set the highest used index in every context we create to be a nullptr
// This is because we might later on call GetAlignedPointerFromEmbedderData which fails with
// a fatal error if the array is smaller than the given index.
jsg::setAlignedPointerInEmbedderData(
context, jsg::ContextPointerSlot::MAX_POINTER_SLOT, nullptr);
// The script is compiled independent of any context, but V8 requires an active one.
// Compile in the context that the first Worker will run in, rather than deserializing
// a throwaway context first.
context = impl->firstWorkerContext.emplace(newContext(lock)).getHandle(lock);
}

JSG_WITHIN_CONTEXT_SCOPE(lock, context, [&](jsg::Lock& js) {
Expand Down Expand Up @@ -1780,6 +1779,14 @@ void Worker::Script::installVirtualFileSystemOnContext(v8::Local<v8::Context> co
const_cast<VirtualFileSystem*>(impl->vfs.get()));
}

jsg::JsContext<api::ServiceWorkerGlobalScope> Worker::Script::newContext(jsg::Lock& lock) const {
return isolate->getApi().newContext(lock,
{
.newModuleRegistry = impl->tryGetNewModuleRegistry(),
.schemaLoader = getSchemaLoader(),
});
}

const capnp::SchemaLoader& Worker::Script::getSchemaLoader() const {
KJ_IF_SOME(moduleRegistry, impl->maybeNewModuleRegistry) {
return moduleRegistry->getSchemaLoader();
Expand Down Expand Up @@ -1848,6 +1855,9 @@ Worker::Script::~Script() noexcept(false) {
KJ_IF_SOME(c, impl->moduleContext) {
recordedLock.disposeContext(kj::mv(c));
}
KJ_IF_SOME(c, impl->firstWorkerContext) {
recordedLock.disposeContext(kj::mv(c));
}
impl = nullptr;
// A Worker closes the code cache production window that the constructor opened. If no
// Worker ran this script, close it here, so that the elections it holds do not keep other
Expand Down Expand Up @@ -2411,12 +2421,15 @@ Worker::Worker(kj::Own<const Script> scriptParam,
jsContext = const_cast<jsg::JsContext<api::ServiceWorkerGlobalScope>*>(&c);
currentSpan.setTag("module_context"_kjc, true);
} else {
// Create a new context.
jsContext = &this->impl->context.emplace(script->isolate->getApi().newContext(lock,
{
.newModuleRegistry = script->impl->tryGetNewModuleRegistry(),
.schemaLoader = script->getSchemaLoader(),
}));
// Take the context the script was compiled in, if no other Worker has, or else create
// a new one. const_cast OK because guarded by `lock`.
auto& scriptImpl = *const_cast<Script&>(*script).impl;
KJ_IF_SOME(c, scriptImpl.firstWorkerContext) {
Comment thread
csjh marked this conversation as resolved.
Resolved
jsContext = &this->impl->context.emplace(kj::mv(c));
scriptImpl.firstWorkerContext = kj::none;
} else {
jsContext = &this->impl->context.emplace(script->newContext(lock));
}
freshContext = true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/workerd/io/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ class Worker::Script: public kj::AtomicRefcounted {

kj::Maybe<kj::Arc<DynamicEnvBuilder>> dynamicEnvBuilder;

// Creates a context for this script's code, with its module registry and schema loader.
jsg::JsContext<api::ServiceWorkerGlobalScope> newContext(jsg::Lock& lock) const;

friend class Worker;

public: // pretend this is private (needs to be public because allocated through template)
Expand Down
Loading