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

dgram: tracking unrefdness of dgram sockets #5828

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function Socket(type, listener) {
this._bindState = BIND_STATE_UNBOUND;
this.type = type;
this.fd = null; // compatibility hack
this._unref = false;

// If true - UV_UDP_REUSEADDR flag will be set
this._reuseAddr = options && options.reuseAddr;
Expand Down Expand Up @@ -532,6 +533,7 @@ function onMessage(nread, handle, buf, rinfo) {


Socket.prototype.ref = function() {
this._unref = false;
if (this._handle)
this._handle.ref();

Expand All @@ -540,6 +542,7 @@ Socket.prototype.ref = function() {


Socket.prototype.unref = function() {
this._unref = true;
if (this._handle)
this._handle.unref();

Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-dgram-ref-unref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
require('../common');
const dgram = require('dgram');
const assert = require('assert');

const sock4 = dgram.createSocket('udp4');
assert.strictEqual(sock4.hasOwnProperty('_unref'), true);
assert.strictEqual(sock4._unref, false);
sock4.unref();
assert.strictEqual(sock4._unref, true);
sock4.ref();
assert.strictEqual(sock4._unref, false);
sock4.unref();
assert.strictEqual(sock4._unref, true);

const sock6 = dgram.createSocket('udp6');
assert.strictEqual(sock6.hasOwnProperty('_unref'), true);
assert.strictEqual(sock6._unref, false);
sock6.unref();
assert.strictEqual(sock6._unref, true);
sock6.ref();
assert.strictEqual(sock6._unref, false);
sock6.unref();
assert.strictEqual(sock6._unref, true);