Skip to content

Commit

Permalink
quic: fix endpointClose error handling, document
Browse files Browse the repository at this point in the history
PR-URL: #34283
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasnell authored and cjihrig committed Jul 22, 2020
1 parent b9297f3 commit 4eb0b3f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
15 changes: 15 additions & 0 deletions doc/api/quic.md
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,21 @@ Emitted after the `QuicSocket` has been destroyed and is no longer usable.

The `'close'` event will not be emitted multiple times.

#### Event: `'endpointClose'`
<!-- YAML
added: REPLACEME
-->

Emitted after a `QuicEndpoint` associated witht the `QuicSocket` closes and
has been destroyed. The handler will be invoked with two arguments:

* `endpoint` {QuicEndpoint} The `QuicEndpoint` that has been destroyed.
* `error` {Error} An `Error` object if the `QuicEndpoint` was destroyed because
of an error.

When all of the `QuicEndpoint` instances associated with a `QuicSocket` have
closed, the `QuicEndpoint` will also automatically close.

#### Event: `'error'`
<!-- YAML
added: REPLACEME
Expand Down
9 changes: 7 additions & 2 deletions lib/internal/quic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,12 +1056,17 @@ class QuicSocket extends EventEmitter {
[kEndpointClose](endpoint, error) {
const state = this[kInternalState];
state.endpoints.delete(endpoint);
process.nextTick(emit.bind(this, 'endpointClose', endpoint, error));
process.nextTick(() => {
try {
this.emit('endpointClose', endpoint, error);
} catch (error) {
this.destroy(error);
}
});

// If there are no more QuicEndpoints, the QuicSocket is no
// longer usable.
if (state.endpoints.size === 0) {
// Ensure that there are absolutely no additional sessions
for (const session of state.sessions)
session.destroy(error);

Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-quic-client-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ server.on('ready', common.mustCall(() => {
client = createQuicSocket({ client: { key, cert, ca, alpn: kALPN }
});

client.on('endpointClose', common.mustCall());

client.on('close', common.mustCall(() => {
debug('Client closing. Duration', client.duration);
debug(' Bound duration',
Expand Down

0 comments on commit 4eb0b3f

Please sign in to comment.