Skip to content

doc issue: socket server 'error' event: 'close' is not called as described in doc. #9710

Closed
@jjqq2013

Description

@jjqq2013
  • Version:v7.1.0
  • Platform:Darwin p14010112-no-MacBook-Pro.local 15.6.0 Darwin Kernel Version 15.6.0: Thu Jun 23 18:25:34 PDT 2016; root:xnu-3248.60.10~1/RELEASE_X86_64 x86_64
  • Subsystem:

I am curious that should i call server.close in server.on('error'...) handler?

According to the document about net.Server 'error' event

Event: 'error'#
<Error>
Emitted when an error occurs. The 'close' event will be called directly following this event. See example in discussion of server.listen.

But in fact, the 'close' event does not happen after error.

This is my test.js which try to listen a privileged port 80, cause an EACCESS error and exit.

require('net').createServer({allowHalfOpen: false}, function(stream) {
  //
}).listen({host: 'localhost', port: 80}, function () {
  //
}).on('error', function(e) {
  console.log(e.message);
  setTimeout(function () {
    //
  },1000)
}).on('close', function () {
  console.log('closed')
});

To ensure the close event can be caught before process exit, i set up a 1 second timer so process will keep alive in 1 second.

But still, the close event does not happen.

If i add this.close() in error handler, then the 'close' does happen.

According to another doc about server.listen, obviously node.js does not call close on error, at least for EADDRINUSE.

server.on('error', (e) => {
  if (e.code == 'EADDRINUSE') {
    console.log('Address in use, retrying...');
    setTimeout(() => {
      server.close();                       //see here please. If it is auto closed, why need this?
      server.listen(PORT, HOST);
    }, 1000);
  }
});

So the document is contradictory, it should be noted that 'close' will not be called automatically.

Regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.netIssues and PRs related to the net subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions