Skip to content

Commit

Permalink
src: make ReqWrap weak
Browse files Browse the repository at this point in the history
This commit allows throwing an exception after creating `FSReqCallback`
  • Loading branch information
RafaelGSS committed Aug 2, 2022
1 parent 07d7e1b commit 6564369
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
4 changes: 0 additions & 4 deletions lib/fs.js
Expand Up @@ -24,10 +24,6 @@

'use strict';

// When using FSReqCallback, make sure to create the object only *after* all
// parameter validation has happened, so that the objects are not kept in memory
// in case they are created but never used due to an exception.

const {
ArrayPrototypePush,
BigIntPrototypeToString,
Expand Down
7 changes: 4 additions & 3 deletions src/req_wrap-inl.h
Expand Up @@ -20,12 +20,12 @@ ReqWrap<T>::ReqWrap(Environment* env,
AsyncWrap::ProviderType provider)
: AsyncWrap(env, object, provider),
ReqWrapBase(env) {
MakeWeak();
Reset();
}

template <typename T>
ReqWrap<T>::~ReqWrap() {
CHECK_EQ(false, persistent().IsEmpty());
}

template <typename T>
Expand Down Expand Up @@ -138,7 +138,6 @@ template <typename T>
template <typename LibuvFunction, typename... Args>
int ReqWrap<T>::Dispatch(LibuvFunction fn, Args... args) {
Dispatched();

// This expands as:
//
// int err = fn(env()->event_loop(), req(), arg1, arg2, Wrapper, arg3, ...)
Expand All @@ -158,8 +157,10 @@ int ReqWrap<T>::Dispatch(LibuvFunction fn, Args... args) {
env()->event_loop(),
req(),
MakeLibuvRequestCallback<T, Args>::For(this, args)...);
if (err >= 0)
if (err >= 0) {
ClearWeak();
env()->IncreaseWaitingRequestCounter();
}
return err;
}

Expand Down

0 comments on commit 6564369

Please sign in to comment.