From edc71ef008cb8ac68b86da3a3df8d826aae6a249 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 7 Jul 2020 12:47:58 -0700 Subject: [PATCH] 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. PR-URL: https://github.com/nodejs/node/pull/34247 Reviewed-By: Anna Henningsen --- doc/api/quic.md | 52 ++++++++++++++++- lib/internal/quic/core.js | 23 +++++++- test/parallel/test-quic-client-server.js | 1 + ...t-quic-server-session-event-error-async.js | 58 +++++++++++++++++++ .../test-quic-server-session-event-error.js | 58 +++++++++++++++++++ 5 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-quic-server-session-event-error-async.js create mode 100644 test/parallel/test-quic-server-session-event-error.js diff --git a/doc/api/quic.md b/doc/api/quic.md index 3667a05b394956..6b0f9ae41bb12e 100644 --- a/doc/api/quic.md +++ b/doc/api/quic.md @@ -1385,10 +1385,60 @@ The `'ready'` event will not be emitted multiple times. added: REPLACEME --> -Emitted when a new `QuicServerSession` has been created. +Emitted when a new `QuicServerSession` has been created. The callback is +invoked with a single argument providing the newly created `QuicServerSession` +object. + +```js +const { createQuicSocket } = require('net'); + +const options = getOptionsSomehow(); +const server = createQuicSocket({ server: options }); +server.listen(); + +server.on('session', (session) => { + // Attach session event listeners. +}); +``` The `'session'` event will be emitted multiple times. +The `'session'` event handler *may* be an async function. + +If the `'session'` event handler throws an error, or if it returns a `Promise` +that is rejected, the error will be handled by destroying the `QuicServerSession` +automatically and emitting a `'sessionError'` event on the `QuicSocket`. + +#### Event: `'sessionError'` + + +Emitted when an error occurs processing an event related to a specific +`QuicSession` instance. The callback is invoked with two arguments: + +* `error` {Error} The error that was either thrown or rejected. +* `session` {QuicSession} The `QuicSession` instance that was destroyed. + +The `QuicSession` instance will have been destroyed by the time the +`'sessionError'` event is emitted. + +```js +const { createQuicSocket } = require('net'); + +const options = getOptionsSomehow(); +const server = createQuicSocket({ server: options }); +server.listen(); + +server.on('session', (session) => { + throw new Error('boom'); +}); + +server.on('sessionError', (error, session) => { + console.log('error:', error.message); +}); +``` + #### quicsocket.addEndpoint(options)