-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quic: handle errors thrown / rejections in the session event
Errors thrown within the session event handler will be handled by destroying the session (allowing a proper connection close to be sent to the client peer). They will not crash the parent QuicSocket by default. Instead, a `'sessionError'` event will be emitted, allowing the error to be logged or handled.
- Loading branch information
Showing
5 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
test/parallel/test-quic-server-session-event-error-async.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Flags: --expose-internals --no-warnings | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasQuic) | ||
common.skip('missing quic'); | ||
|
||
const { internalBinding } = require('internal/test/binding'); | ||
const { | ||
constants: { | ||
NGTCP2_CONNECTION_REFUSED | ||
} | ||
} = internalBinding('quic'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
key, | ||
cert, | ||
ca, | ||
} = require('../common/quic'); | ||
|
||
const { createQuicSocket } = require('net'); | ||
|
||
const options = { key, cert, ca, alpn: 'zzz' }; | ||
|
||
const server = createQuicSocket({ server: options }); | ||
|
||
server.on('session', common.mustCall(async (session) => { | ||
session.on('close', common.mustCall()); | ||
session.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err.message, 'boom'); | ||
})); | ||
// Throwing inside the session event handler should cause | ||
// the session to be destroyed immediately. This should | ||
// cause the client side to be closed also. | ||
throw new Error('boom'); | ||
})); | ||
|
||
server.on('sessionError', common.mustCall((err, session) => { | ||
assert.strictEqual(err.message, 'boom'); | ||
assert(session.destroyed); | ||
})); | ||
|
||
server.listen(); | ||
|
||
server.once('listening', common.mustCall(() => { | ||
const client = createQuicSocket({ client: options }); | ||
const req = client.connect({ | ||
address: 'localhost', | ||
port: server.endpoints[0].address.port | ||
}); | ||
req.on('close', common.mustCall(() => { | ||
assert.strictEqual(req.closeCode.code, NGTCP2_CONNECTION_REFUSED); | ||
assert.strictEqual(req.closeCode.silent, true); | ||
server.close(); | ||
client.close(); | ||
})); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Flags: --expose-internals --no-warnings | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasQuic) | ||
common.skip('missing quic'); | ||
|
||
const { internalBinding } = require('internal/test/binding'); | ||
const { | ||
constants: { | ||
NGTCP2_CONNECTION_REFUSED | ||
} | ||
} = internalBinding('quic'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
key, | ||
cert, | ||
ca, | ||
} = require('../common/quic'); | ||
|
||
const { createQuicSocket } = require('net'); | ||
|
||
const options = { key, cert, ca, alpn: 'zzz' }; | ||
|
||
const server = createQuicSocket({ server: options }); | ||
|
||
server.on('session', common.mustCall((session) => { | ||
session.on('close', common.mustCall()); | ||
session.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err.message, 'boom'); | ||
})); | ||
// Throwing inside the session event handler should cause | ||
// the session to be destroyed immediately. This should | ||
// cause the client side to be closed also. | ||
throw new Error('boom'); | ||
})); | ||
|
||
server.on('sessionError', common.mustCall((err, session) => { | ||
assert.strictEqual(err.message, 'boom'); | ||
assert(session.destroyed); | ||
})); | ||
|
||
server.listen(); | ||
|
||
server.once('listening', common.mustCall(() => { | ||
const client = createQuicSocket({ client: options }); | ||
const req = client.connect({ | ||
address: 'localhost', | ||
port: server.endpoints[0].address.port | ||
}); | ||
req.on('close', common.mustCall(() => { | ||
assert.strictEqual(req.closeCode.code, NGTCP2_CONNECTION_REFUSED); | ||
assert.strictEqual(req.closeCode.silent, true); | ||
server.close(); | ||
client.close(); | ||
})); | ||
})); |