Skip to content

Commit

Permalink
fixup! Implement async_hooks.AsyncResource and other cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Dec 12, 2022
1 parent f1c48ed commit 87a5e19
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/workerd/jsg/async-context.c++
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ struct AsyncResourceWrappable final: public Wrappable,
}
return nullptr;
}

kj::Maybe<kj::Own<Wrappable>> maybeGetStrongRef() override {
return kj::addRef(*this);
}
};

} // namespace
Expand Down Expand Up @@ -62,7 +66,7 @@ AsyncResource::~AsyncResource() noexcept(false) {
AsyncResource& AsyncResource::current(Lock& js) {
auto& isolateBase = IsolateBase::from(js.v8Isolate);
KJ_ASSERT(!isolateBase.asyncResourceStack.empty());
return *isolateBase.asyncResourceStack.front();
return *isolateBase.asyncResourceStack.front().resource;
}

kj::Own<AsyncResource> AsyncResource::create(Lock& js, kj::Maybe<AsyncResource&> maybeParent) {
Expand Down Expand Up @@ -179,7 +183,10 @@ AsyncResource::StorageScope::~StorageScope() noexcept(false) {
}

void IsolateBase::pushAsyncResource(AsyncResource& next) {
asyncResourceStack.push_front(&next);
asyncResourceStack.push_front(AsyncResourceEntry{
&next,
next.maybeGetStrongRef()
});
}

void IsolateBase::popAsyncResource() {
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/jsg/async-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class AsyncResource {

~AsyncResource() noexcept(false);

virtual kj::Maybe<kj::Own<Wrappable>> maybeGetStrongRef() { return nullptr; }

static AsyncResource& current(Lock& js);

static kj::Own<AsyncResource> create(Lock& js, kj::Maybe<AsyncResource&> maybeParent = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/jsg/setup.c++
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ IsolateBase::IsolateBase(const V8System& system, v8::Isolate::CreateParams&& cre
ptr(newIsolate(kj::mv(createParams))),
heapTracer(ptr),
rootAsyncResource(*this) {
asyncResourceStack.push_front(&rootAsyncResource);
asyncResourceStack.push_front({&rootAsyncResource});

ptr->SetFatalErrorHandler(&fatalError);
ptr->SetOOMErrorHandler(&oomError);
Expand Down
7 changes: 6 additions & 1 deletion src/workerd/jsg/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,13 @@ class IsolateBase {
void pushAsyncResource(AsyncResource& next);
void popAsyncResource();

struct AsyncResourceEntry {
AsyncResource* resource;
kj::Maybe<kj::Own<Wrappable>> maybeStrongRef;
};

uint64_t nextAsyncResourceId = 0;
std::deque<AsyncResource*> asyncResourceStack;
std::deque<AsyncResourceEntry> asyncResourceStack;
kj::HashMap<uint64_t, AsyncResource*> asyncResourcesMap;
AsyncResource rootAsyncResource;

Expand Down

0 comments on commit 87a5e19

Please sign in to comment.