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: don't hide implicit bind errors #31958

Merged
merged 2 commits into from
Feb 28, 2020
Merged
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
14 changes: 9 additions & 5 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1727,11 +1727,6 @@ value.
While using [`dgram.createSocket()`][], the size of the receive or send `Buffer`
could not be determined.

<a id="ERR_SOCKET_CANNOT_SEND"></a>
### `ERR_SOCKET_CANNOT_SEND`

Data could be sent on a socket.

<a id="ERR_SOCKET_CLOSED"></a>
### `ERR_SOCKET_CLOSED`

Expand Down Expand Up @@ -2293,6 +2288,15 @@ removed: v10.0.0

The `repl` module was unable to parse data from the REPL history file.

<a id="ERR_SOCKET_CANNOT_SEND"></a>
### `ERR_SOCKET_CANNOT_SEND`
<!-- YAML
added: v9.0.0
removed: REPLACEME
-->

Data could be sent on a socket.

<a id="ERR_STDERR_CLOSE"></a>
### `ERR_STDERR_CLOSE`
<!-- YAML
Expand Down
6 changes: 2 additions & 4 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const {
ERR_SOCKET_BAD_BUFFER_SIZE,
ERR_SOCKET_BAD_PORT,
ERR_SOCKET_BUFFER_SIZE,
ERR_SOCKET_CANNOT_SEND,
ERR_SOCKET_DGRAM_IS_CONNECTED,
ERR_SOCKET_DGRAM_NOT_CONNECTED,
ERR_SOCKET_DGRAM_NOT_RUNNING,
Expand Down Expand Up @@ -506,23 +505,22 @@ function enqueue(self, toEnqueue) {
// event handler that flushes the send queue after binding is done.
if (state.queue === undefined) {
state.queue = [];
self.once('error', onListenError);
self.once(EventEmitter.errorMonitor, onListenError);
self.once('listening', onListenSuccess);
}
state.queue.push(toEnqueue);
}


function onListenSuccess() {
this.removeListener('error', onListenError);
this.removeListener(EventEmitter.errorMonitor, onListenError);
clearQueue.call(this);
}


function onListenError(err) {
this.removeListener('listening', onListenSuccess);
this[kStateSymbol].queue = undefined;
this.emit('error', new ERR_SOCKET_CANNOT_SEND());
}


Expand Down
1 change: 0 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,6 @@ E('ERR_SOCKET_BAD_TYPE',
E('ERR_SOCKET_BUFFER_SIZE',
'Could not get or set buffer size',
SystemError);
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data', Error);
E('ERR_SOCKET_CLOSED', 'Socket is closed', Error);
E('ERR_SOCKET_DGRAM_IS_CONNECTED', 'Already connected', Error);
E('ERR_SOCKET_DGRAM_NOT_CONNECTED', 'Not connected', Error);
Expand Down
47 changes: 15 additions & 32 deletions test/sequential/test-dgram-implicit-bind-failure.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,31 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');
const dgram = require('dgram');
const dns = require('dns');
const { kStateSymbol } = require('internal/dgram');
const mockError = new Error('fake DNS');

// Monkey patch dns.lookup() so that it always fails.
dns.lookup = function(address, family, callback) {
process.nextTick(() => { callback(new Error('fake DNS')); });
process.nextTick(() => { callback(mockError); });
};

const socket = dgram.createSocket('udp4');
let dnsFailures = 0;
let sendFailures = 0;

process.on('exit', () => {
assert.strictEqual(dnsFailures, 3);
assert.strictEqual(sendFailures, 3);
});

socket.on('error', (err) => {
if (/^Error: fake DNS$/.test(err)) {
// The DNS lookup should fail since it is monkey patched. At that point in
// time, the send queue should be populated with the send() operation. There
// should also be two listeners - this function and the dgram internal one
// time error handler.
dnsFailures++;
assert(Array.isArray(socket[kStateSymbol].queue));
assert.strictEqual(socket[kStateSymbol].queue.length, 1);
assert.strictEqual(socket.listenerCount('error'), 2);
return;
}

if (err.code === 'ERR_SOCKET_CANNOT_SEND') {
// On error, the queue should be destroyed and this function should be
// the only listener.
sendFailures++;
assert.strictEqual(socket[kStateSymbol].queue, undefined);
assert.strictEqual(socket.listenerCount('error'), 1);
return;
}

assert.fail(`Unexpected error: ${err}`);
});
socket.on(EventEmitter.errorMonitor, common.mustCall((err) => {
// The DNS lookup should fail since it is monkey patched. At that point in
// time, the send queue should be populated with the send() operation.
assert.strictEqual(err, mockError);
assert(Array.isArray(socket[kStateSymbol].queue));
assert.strictEqual(socket[kStateSymbol].queue.length, 1);
}, 3));

socket.on('error', common.mustCall((err) => {
assert.strictEqual(err, mockError);
assert.strictEqual(socket[kStateSymbol].queue, undefined);
}, 3));

// Initiate a few send() operations, which will fail.
socket.send('foobar', common.PORT, 'localhost');
Expand Down