Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable FinalizationRegistry #3560

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 15 additions & 5 deletions src/workerd/io/io-context.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1097,11 +1097,6 @@ void IoContext::runImpl(Runnable& runnable,
// Running the microtask queue can itself trigger a pending exception in the isolate.
v8::TryCatch tryCatch(workerLock.getIsolate());

// Just trying out stuff
auto& system = const_cast<jsg::V8System&>(js.getV8System());
while (v8::platform::PumpMessageLoop(&system.getDefaultPlatform(), js.v8Isolate, v8::platform::MessageLoopBehavior::kDoNotWait)) {}


js.runMicrotasks();

if (tryCatch.HasCaught()) {
Expand Down Expand Up @@ -1164,6 +1159,17 @@ void IoContext::runImpl(Runnable& runnable,
}
}
});

if (!isCurrentNull()) {
KJ_LOG(ERROR, "IoContext not-null before running PumpMessageLoop()");
} else {
worker->runInLockScope(lockType, [&](Worker::Lock& lock) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a new lock here, immediately after releasing the previous one, is expensive. You should find a way to run PumpMessageLoop() just before releasing the previous lock instead.

jsg::Lock& js = lock;
auto& system = const_cast<jsg::V8System&>(js.getV8System());
KJ_DBG(js.v8Isolate);
while (v8::platform::PumpMessageLoop(&system.getDefaultPlatform(), js.v8Isolate, v8::platform::MessageLoopBehavior::kDoNotWait)) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels wrong for this code to be aware of the v8::Platform. Knowledge of how the platform is set up (and even the fact that we use the default platform) should be encapsulated inside JSG.

To that end, perhaps jsg::Lock should have a pumpMessageLoop() method, which internally calls v8::platform::PumpMessageLoop appropriately?

Copy link
Member

@kentonv kentonv Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually the right time to call PumpMessageLoop -- at the end of every single ctx.run()?

Arguably it might be preferable if we invoked PumpMessageLoop asynchronously later on when we're not actively responding to a request.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we likely need to find a better place for this. We can end up running ctx.run quite a few times during a request.

});
}
}

static constexpr auto kAsyncIoErrorMessage =
Expand Down Expand Up @@ -1191,6 +1197,10 @@ bool IoContext::hasCurrent() {
return threadLocalRequest != nullptr;
}

bool IoContext::isCurrentNull() {
return threadLocalRequest == nullptr;
}

bool IoContext::isCurrent() {
return this == threadLocalRequest;
}
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/io/io-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ class IoContext final: public kj::Refcounted, private kj::TaskSet::ErrorHandler
// True if there is a current IoContext for the thread (current() will not throw).
static bool hasCurrent();

// True if there is no IoContext for this thread
static bool isCurrentNull();

// True if this is the IoContext for the current thread (same as `hasCurrent() && tcx == current()`).
bool isCurrent();

Expand Down
3 changes: 3 additions & 0 deletions src/workerd/jsg/setup.c++
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ V8System::V8System(v8::Platform& platformParam, kj::ArrayPtr<const kj::StringPtr
V8System::V8System(kj::Own<v8::Platform> platformParam, kj::ArrayPtr<const kj::StringPtr> flags, v8::Platform* defaultPlatformPtr)
: platformInner(kj::mv(platformParam)),
platformWrapper(*platformInner), defaultPlatformPtr_{defaultPlatformPtr} {
if (!defaultPlatformPtr_) {
defaultPlatformPtr_ = platformInner.get();
}
#if V8_HAS_STACK_START_MARKER
v8::StackStartMarker::EnableForProcess();
#endif
Expand Down
Loading