Skip to content

Commit bc5a382

Browse files
committed
src: make ReqWrap weak
This commit allows throwing an exception after creating `FSReqCallback`
1 parent 07d7e1b commit bc5a382

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

lib/fs.js

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
'use strict';
2626

27-
// When using FSReqCallback, make sure to create the object only *after* all
28-
// parameter validation has happened, so that the objects are not kept in memory
29-
// in case they are created but never used due to an exception.
30-
3127
const {
3228
ArrayPrototypePush,
3329
BigIntPrototypeToString,

src/req_wrap-inl.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ ReqWrap<T>::ReqWrap(Environment* env,
2020
AsyncWrap::ProviderType provider)
2121
: AsyncWrap(env, object, provider),
2222
ReqWrapBase(env) {
23+
MakeWeak();
2324
Reset();
2425
}
2526

2627
template <typename T>
2728
ReqWrap<T>::~ReqWrap() {
28-
CHECK_EQ(false, persistent().IsEmpty());
2929
}
3030

3131
template <typename T>
@@ -121,6 +121,7 @@ struct MakeLibuvRequestCallback<ReqT, void(*)(ReqT*, Args...)> {
121121

122122
static void Wrapper(ReqT* req, Args... args) {
123123
ReqWrap<ReqT>* req_wrap = ReqWrap<ReqT>::from_req(req);
124+
req_wrap->MakeWeak();
124125
req_wrap->env()->DecreaseWaitingRequestCounter();
125126
F original_callback = reinterpret_cast<F>(req_wrap->original_callback_);
126127
original_callback(req, args...);
@@ -138,7 +139,6 @@ template <typename T>
138139
template <typename LibuvFunction, typename... Args>
139140
int ReqWrap<T>::Dispatch(LibuvFunction fn, Args... args) {
140141
Dispatched();
141-
142142
// This expands as:
143143
//
144144
// int err = fn(env()->event_loop(), req(), arg1, arg2, Wrapper, arg3, ...)
@@ -158,8 +158,10 @@ int ReqWrap<T>::Dispatch(LibuvFunction fn, Args... args) {
158158
env()->event_loop(),
159159
req(),
160160
MakeLibuvRequestCallback<T, Args>::For(this, args)...);
161-
if (err >= 0)
161+
if (err >= 0) {
162+
ClearWeak();
162163
env()->IncreaseWaitingRequestCounter();
164+
}
163165
return err;
164166
}
165167

0 commit comments

Comments
 (0)