Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

http2: client session destroy also destroys associated socket #64

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 10 additions & 3 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,16 @@ class Http2Session extends EventEmitter {
destroy() {
const state = this[kState];
const streams = state.streams;
const socket = this[kSocket];
if (state.destroyed) {
return;
}
state.destroyed = true;
if (!socket.destroyed) {
socket.destroy();
}
this[kSocket] = undefined;
this[kServer] = undefined;
timers.unenroll(this);
streams.forEach((value, key) => {
value[kSession] = undefined;
Expand Down Expand Up @@ -955,12 +964,10 @@ Object.defineProperties(Http2Session.prototype, {
// establishment of a new connection.
function socketDestroy(error) {
const session = this[kSession];
session.destroy();
session[kServer] = undefined;
session[kSocket] = undefined;
this[kServer] = undefined;
this.destroy = this[kDestroySocket];
this.destroy(error);
session.destroy();
}

function socketOnResume() {
Expand Down
48 changes: 48 additions & 0 deletions test/parallel/test-http2-client-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const h2 = require('http2');

const server = h2.createServer();
server.listen(0);

server.on('listening', common.mustCall(function() {
const port = this.address().port;
const headers = { ':path': '/' };

const destroyCallbacks = [
client => client.destroy(),
client => client.socket.destroy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: Node.js core house style is to always wrap the arguments of arrow functions in params. This won't pass linting as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this on landing...

]

let remaining = destroyCallbacks.length;

destroyCallbacks.forEach((destroyCallback) => {
const client = h2.connect(`http://localhost:${port}`);
client.on('connect', common.mustCall(() => {
const socket = client.socket;

assert(client.socket, 'client session has associated socket');
assert(!client.destroyed,
'client has not been destroyed before destroy is called');
assert(!socket.destroyed,
'socket has not been destroyed before destroy is called');

// Ensure that 'close' event is emitted
client.on('close', common.mustCall(() => {}));

destroyCallback(client);

assert(!client.socket, 'client.socket undefined after destroy is called');
assert(client.destroyed,
'client marked as destroyed after destroy is called');
assert(socket.destroyed,
'socket marked as destroyed after destroy is called');

if (--remaining === 0) {
server.close();
}
}));
});
}));
2 changes: 1 addition & 1 deletion test/parallel/test-http2-create-client-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const URL = url.URL;
let count = items.length;

const maybeClose = common.mustCall((client) => {
client.socket.destroy();
client.destroy();
if (--count === 0) {
setImmediate(() => server.close());
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-create-client-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ server.on('listening', common.mustCall(function() {
assert.strictEqual(body, data);
if (--expected === 0) {
server.close();
client.socket.destroy();
client.destroy();
}
}));
req.end();
Expand Down