Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

TLSSocket.destroy() is not fully closing the socket #9242

Closed
user447463 opened this issue Feb 18, 2015 · 7 comments
Closed

TLSSocket.destroy() is not fully closing the socket #9242

user447463 opened this issue Feb 18, 2015 · 7 comments
Labels

Comments

@user447463
Copy link

node 0.12 seems to have introduced a timeout related issue for the TLS package. Closing the TLSSocket is not enough to stop the timeout but an additional TCP socket close is necessary.

Please see the following code sample for a reproducible pattern (0.10 vs. 0.12)

var url=require('url').parse(process.argv[2]);
var secure=url.protocol=='https:';

var socket=new require('net').Socket();

socket.setTimeout(2000, function() {
    this.destroy();
    console.log('TCP connection timed out');
});

socket.connect({ host: url.hostname, port: url.port || (secure?443:80) }, function() {
    if (secure)
    {
        require('tls').connect({ socket: this, rejectUnauthorized: false, servername: url.hostname }, function() {
            this.destroy();
            console.log('SSL successfully connected');
        }).on('error', function(error){
            this.destroy();
            console.log('Error during SSL handshake');
        });
    }
    else
    {
        this.destroy();
        console.log('Non-SSL successfully connected');
    }
}).on('error', function(error) {
    this.destroy();
    console.log('Error during TCP connection '+error.code);
});

// Dummy line to keep node from exiting
setInterval(function() {}, 1000);

Expected result

node file.js https://www.google.com
SSL successfully connected

Actual result

node file.js https://www.google.com
SSL successfully connected
TCP connection timed out
@user447463 user447463 changed the title TLSSocket.destroy() is not fully closing the closing TLSSocket.destroy() is not fully closing the socket Feb 18, 2015
@trevnorris trevnorris added the tls label Feb 18, 2015
@trevnorris
Copy link

/cc @indutny

@user447463
Copy link
Author

In case it is of any help, the issue seems to have started with 0.11.3.

@user447463
Copy link
Author

For cross-reference, the respective bug at io.js
nodejs/node#878

indutny added a commit to indutny/io.js that referenced this issue Feb 19, 2015
`TLSSocket` wraps the original `net.Socket`, but writes/reads to/from
`TLSSocket` do not touch the timers of original `net.Socket`.

Introduce `socket._parent` property, and iterate through all parents
to unref timers and prevent timeout event on original `net.Socket`.

Fix: nodejs/node-v0.x-archive#9242
@indutny
Copy link
Member

indutny commented Feb 19, 2015

Should be fixed by nodejs/node#891

indutny added a commit to nodejs/node that referenced this issue Feb 19, 2015
`TLSSocket` wraps the original `net.Socket`, but writes/reads to/from
`TLSSocket` do not touch the timers of original `net.Socket`.

Introduce `socket._parent` property, and iterate through all parents
to unref timers and prevent timeout event on original `net.Socket`.

Fix: nodejs/node-v0.x-archive#9242
PR-URL: #891
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@indutny
Copy link
Member

indutny commented Feb 19, 2015

See nodejs/node@9b6b055 for a fix.

indutny added a commit that referenced this issue Feb 19, 2015
`TLSSocket` wraps the original `net.Socket`, but writes/reads to/from
`TLSSocket` do not touch the timers of original `net.Socket`.

Introduce `socket._parent` property, and iterate through all parents
to unref timers and prevent timeout event on original `net.Socket`.

Fix: #9242
PR-URL: nodejs/node#891
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@trevnorris
Copy link

Thanks @indutny. Landed the same on bada87b.

@jadbaz
Copy link

jadbaz commented Oct 13, 2015

So has this been resolved or not?
I'm on node 4.0.0 and I still have this issue.
client.destroy simply does not work.

This is my code:

var client = new require('net').Socket();
client.connect(54321, '192.168.1.7', ()=>{
    client.write("Hello");
    client.destroy();
    process.exit();
});

I'm using Hercules to test it.
I get: 5:19:24 PM: 192.168.1.21 Client connected
The program exits and I don't get a disconnected message.

If this has been fixed or I'm wrong please tell me. If this is indeed a bug, I'll open a new issue for Node 4.0.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants