Skip to content

close event emitted on net socket but not tls #24984

Description

@davedoesdev

Why does this emit a close event:

const fs = require('fs');
const path = require('path');
const net = require('net');
const server = net.createServer();

server.on('connection', function (conn) {
    conn.on('close', function () {
        console.log("CLOSE");
    });

    conn.resume();
    conn.end(Buffer.alloc(1024*1024));
});

server.listen(7000, function () {
    net.connect({
        port: 7000
    }, function () {
        this.end();
    });
});

but this does not emit a close event:

const fs = require('fs');
const path = require('path');
const tls = require('tls');
const server = tls.createServer({
    key: fs.readFileSync(path.join(__dirname, 'server.key')),
    cert: fs.readFileSync(path.join(__dirname, 'server.pem'))
});

server.on('secureConnection', function (conn) {
    conn.on('close', function () {
        console.log("CLOSE");
    });

    conn.resume();
    conn.end(Buffer.alloc(1024*1024));
});

server.listen(7000, function () {
    tls.connect({
        ca: fs.readFileSync(path.join(__dirname, 'ca.pem')),
        port: 7000
    }, function () {
        this.end();
    });
});

?

Activity

  1. lpinca commented on Dec 12, 2018

    @lpinca
    Member

    They both don't emit 'close' on my machine and the reason is that data is not read on the client. It works if this.resume() is added on the client.

  2. davedoesdev commented on Dec 12, 2018

    @davedoesdev
    ContributorAuthor

    OS TCP buffer size perhaps then

  3. davedoesdev commented on Dec 12, 2018

    @davedoesdev
    ContributorAuthor

    @lpinca what about this? I don't get a close event.

    const fs = require('fs');
    const path = require('path');
    const tls = require('tls');
    const server = tls.createServer({
        key: fs.readFileSync(path.join(__dirname, 'server.key')),
        cert: fs.readFileSync(path.join(__dirname, 'server.pem'))
    });
    let cconn = null;
    let sconn = null;
    
    function doit() {
        if (cconn && sconn) {
            cconn.resume();
            sconn.resume();
            sconn.end(Buffer.alloc(1024*1024));
            cconn.end();
        }
    }
    
    server.on('secureConnection', function (conn) {
        conn.on('close', function () {
            console.log("CLOSE");
        });
        sconn = conn;
        doit();
    });
    
    server.listen(7000, function () {
        tls.connect({
            ca: fs.readFileSync(path.join(__dirname, 'ca.pem')),
            port: 7000
        }, function () {
            cconn = this;
            doit();
        });
    });
  4. davedoesdev commented on Dec 13, 2018

    @davedoesdev
    ContributorAuthor

    This fixes it:

    diff --git a/lib/net.js b/lib/net.js
    index 0229e450fc..377dd202af 100644
    --- a/lib/net.js
    +++ b/lib/net.js
    @@ -371,8 +371,8 @@ Socket.prototype._final = function(cb) {
     };
     
     
    -function afterShutdown(status, handle) {
    -  var self = handle[owner_symbol];
    +function afterShutdown(status) {
    +  var self = this.handle[owner_symbol];
     
       debug('afterShutdown destroyed=%j', self.destroyed,
             self._readableState);
  5. lpinca commented on Dec 13, 2018

    @lpinca
    Member

    I can reproduce the issue, both 'end' and 'finish' are emitted but not 'close'.

  6. added
    confirmed-bugIssues and PRs for confirmed bugs.
    netIssues and PRs related to the net subsystem.
    on Dec 13, 2018
  7. lpinca commented on Dec 13, 2018

    @lpinca
    Member
  8. added
    tlsIssues and PRs related to the tls subsystem.
    and removed
    netIssues and PRs related to the net subsystem.
    on Dec 13, 2018
  9. lpinca commented on Dec 13, 2018

    @lpinca
    Member

    The first bad commit seems to be e82f67d710.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.tlsIssues and PRs related to the tls subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions