Skip to content

Commit 9dc6f23

Browse files
robertsipkayichoi
authored andcommitted
Fix error handling in server.listen function (#927)
IoT.js-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent 18f4cc1 commit 9dc6f23

File tree

4 files changed

+88
-11
lines changed

4 files changed

+88
-11
lines changed

src/js/net.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ Server.prototype.listen = function() {
503503
var err = self._handle.bind(host, port);
504504
if (err) {
505505
self._handle.close();
506-
self._handle = null;
507-
return err;
506+
return self.emit('error', err);
508507
}
509508

510509
// listen
@@ -516,8 +515,7 @@ Server.prototype.listen = function() {
516515

517516
if (err) {
518517
self._handle.close();
519-
self._handle = null;
520-
return err;
518+
return self.emit('error', err);
521519
}
522520

523521
process.nextTick(function() {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
// Copyright Joyent, Inc. and other Node contributors.
17+
//
18+
// Permission is hereby granted, free of charge, to any person obtaining a
19+
// copy of this software and associated documentation files (the
20+
// "Software"), to deal in the Software without restriction, including
21+
// without limitation the rights to use, copy, modify, merge, publish,
22+
// distribute, sublicense, and/or sell copies of the Software, and to permit
23+
// persons to whom the Software is furnished to do so, subject to the
24+
// following conditions:
25+
//
26+
// The above copyright notice and this permission notice shall be included
27+
// in all copies or substantial portions of the Software.
28+
//
29+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
32+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
33+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
34+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
35+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
36+
37+
'use strict';
38+
39+
var common = require('node/common');
40+
var assert = require('assert');
41+
var net = require('net');
42+
43+
var server1 = net.createServer(common.fail);
44+
server1.listen(0, '127.0.0.1', common.mustCall(function() {
45+
46+
var server2 = net.createServer(common.fail);
47+
48+
server2.on('error', common.mustCall(function(e) {
49+
assert.strictEqual(e, -98); // EADDRINUSE
50+
51+
server1.close();
52+
}));
53+
54+
server2.listen(this.address().port, '127.0.0.1', common.fail);
55+
}));

test/run_pass/test_net_httpclient_error.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,47 @@ var http = require('http');
1818
var recievedResponse = false;
1919

2020
var server = http.createServer(function(request, response) {
21-
server.close();
21+
var str = '';
22+
23+
request.on('data', function (chunk) {
24+
str += chunk;
25+
});
26+
27+
request.on('end', function() {
28+
response.end();
29+
});
2230
});
2331

24-
server.listen(3085, 'localhost');
32+
server.listen(20004, 5);
2533

2634
// Connection refusal: The request will try to connect to a server,
2735
// however the connection can not be created because the port is invalid.
28-
// Therefore the client will destroy the socket without an error emit.
29-
http.request({
36+
var req = http.request({
3037
host: 'localhost',
31-
port: 3089,
38+
port: 20002,
3239
path: '/',
3340
method: 'GET'
3441
}, function(response) {
42+
var str = '';
43+
44+
response.on('data', function(chunk) {
45+
str += chunk;
46+
});
47+
3548
response.on('end', function() {
3649
recievedResponse = true;
50+
server.close();
3751
});
38-
}).end();
52+
});
53+
54+
req.on('error', function() {
55+
recievedResponse = false;
56+
server.close();
57+
});
58+
59+
req.setTimeout(100, function(){
60+
req.end();
61+
});
3962

4063
process.on('exit', function() {
4164
assert.equal(recievedResponse, false);

test/testsets.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
{ "name": "test_process_implicit_exit.js", "expected-failure": true }
107107
],
108108
"node/parallel": [
109-
{ "name": "test-assert.js" }
109+
{ "name": "test-assert.js" },
110+
{ "name": "test-net-bind-twice.js" }
110111
]
111112
}

0 commit comments

Comments
 (0)