Skip to content

Commit a58d483

Browse files
committed
src: simplify handlewrap state tracking logic
This also updates the tests to expect that a closed handle has no reference count. PR-URL: #6395 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent cad1a62 commit a58d483

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

src/handle_wrap.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
3535

3636
void HandleWrap::Unrefed(const FunctionCallbackInfo<Value>& args) {
3737
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
38-
// XXX(bnoordhuis) It's debatable whether a nullptr wrap should count
39-
// as having a reference count but it's compatible with the logic that
40-
// it replaces.
41-
args.GetReturnValue().Set(wrap == nullptr || !HasRef(wrap));
38+
args.GetReturnValue().Set(!HasRef(wrap));
4239
}
4340

4441

@@ -51,6 +48,9 @@ void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
5148
if (!IsAlive(wrap))
5249
return;
5350

51+
if (wrap->state_ != kInitialized)
52+
return;
53+
5454
CHECK_EQ(false, wrap->persistent().IsEmpty());
5555
uv_close(wrap->handle__, OnClose);
5656
wrap->state_ = kClosing;

src/handle_wrap.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ class HandleWrap : public AsyncWrap {
3838
static void Unrefed(const v8::FunctionCallbackInfo<v8::Value>& args);
3939

4040
static inline bool IsAlive(const HandleWrap* wrap) {
41-
// XXX(bnoordhuis) It's debatable whether only kInitialized should
42-
// count as alive but it's compatible with the check that it replaces.
43-
return wrap != nullptr && wrap->state_ == kInitialized;
41+
return wrap != nullptr && wrap->state_ != kClosed;
4442
}
4543

4644
static inline bool HasRef(const HandleWrap* wrap) {
47-
return wrap != nullptr &&
48-
wrap->state_ != kClosed &&
49-
uv_has_ref(wrap->GetHandle());
45+
return IsAlive(wrap) && uv_has_ref(wrap->GetHandle());
5046
}
5147

5248
inline uv_handle_t* GetHandle() const { return handle__; }

test/parallel/test-handle-wrap-isrefed-tty.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (process.argv[2] === 'child') {
1919
assert(tty._handle.unrefed(), false);
2020
tty.unref();
2121
assert(tty._handle.unrefed(), true);
22-
tty._handle.close();
22+
tty._handle.close(common.mustCall(() => assert(tty._handle.unrefed(), true)));
2323
assert(tty._handle.unrefed(), true);
2424
return;
2525
}

test/parallel/test-handle-wrap-isrefed.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function makeAssert(message) {
2222
assert(cp._handle.unrefed(), true);
2323
cp.ref();
2424
assert(cp._handle.unrefed(), false);
25-
cp._handle.close();
25+
cp._handle.close(common.mustCall(() => assert(cp._handle.unrefed(), true)));
2626
assert(cp._handle.unrefed(), false);
2727
}
2828

@@ -39,7 +39,8 @@ function makeAssert(message) {
3939
assert(sock4._handle.unrefed(), true);
4040
sock4.ref();
4141
assert(sock4._handle.unrefed(), false);
42-
sock4._handle.close();
42+
sock4._handle.close(
43+
common.mustCall(() => assert(sock4._handle.unrefed(), true)));
4344
assert(sock4._handle.unrefed(), false);
4445

4546
const sock6 = dgram.createSocket('udp6');
@@ -49,7 +50,8 @@ function makeAssert(message) {
4950
assert(sock6._handle.unrefed(), true);
5051
sock6.ref();
5152
assert(sock6._handle.unrefed(), false);
52-
sock6._handle.close();
53+
sock6._handle.close(
54+
common.mustCall(() => assert(sock6._handle.unrefed(), true)));
5355
assert(sock6._handle.unrefed(), false);
5456
}
5557

@@ -65,7 +67,7 @@ function makeAssert(message) {
6567
assert(handle.unrefed(), true);
6668
handle.ref();
6769
assert(handle.unrefed(), false);
68-
handle.close();
70+
handle.close(common.mustCall(() => assert(handle.unrefed(), true)));
6971
assert(handle.unrefed(), false);
7072
}
7173

@@ -84,7 +86,8 @@ function makeAssert(message) {
8486
server.ref();
8587
assert(server._handle.unrefed(), false);
8688
assert(server._unref, false);
87-
server._handle.close();
89+
server._handle.close(
90+
common.mustCall(() => assert(server._handle.unrefed(), true)));
8891
assert(server._handle.unrefed(), false);
8992
}
9093

@@ -98,6 +101,7 @@ function makeAssert(message) {
98101
assert(timer._handle.unrefed(), true);
99102
timer.ref();
100103
assert(timer._handle.unrefed(), false);
101-
timer.close();
104+
timer._handle.close(
105+
common.mustCall(() => assert(timer._handle.unrefed(), true)));
102106
assert(timer._handle.unrefed(), false);
103107
}

0 commit comments

Comments
 (0)