Skip to content

Commit b9297f3

Browse files
jasnellcjihrig
authored andcommitted
quic: restrict addEndpoint to before QuicSocket bind
Restricting this to pre-bind keeps things simple PR-URL: #34283 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent ee7e2e5 commit b9297f3

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

doc/api/quic.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,10 @@ added: REPLACEME
14581458
**Default**: `'udp4'`.
14591459
* Returns: {QuicEndpoint}
14601460

1461-
Creates and adds a new `QuicEndpoint` to the `QuicSocket` instance.
1461+
Creates and adds a new `QuicEndpoint` to the `QuicSocket` instance. An
1462+
error will be thrown if `quicsock.addEndpoint()` is called either after
1463+
the `QuicSocket` has already started binding to the local ports or after
1464+
the `QuicSocket` has been destroyed.
14621465

14631466
#### quicsocket.bound
14641467
<!-- YAML

lib/internal/quic/core.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -1102,22 +1102,19 @@ class QuicSocket extends EventEmitter {
11021102
});
11031103
}
11041104

1105+
// A QuicSocket will typically bind only to a single local port, but it is
1106+
// possible to bind to multiple, even if those use different IP families
1107+
// (e.g. IPv4 or IPv6). Calls to addEndpoint() must be made before the
1108+
// QuicSocket is bound (e.g. before any calls to listen() or connect()).
11051109
addEndpoint(options = {}) {
11061110
const state = this[kInternalState];
11071111
if (this.destroyed)
11081112
throw new ERR_INVALID_STATE('QuicSocket is already destroyed');
1109-
1110-
// TODO(@jasnell): Also forbid adding an endpoint if
1111-
// the QuicSocket is closing.
1113+
if (state.state !== kSocketUnbound)
1114+
throw new ERR_INVALID_STATE('QuicSocket is already being bound');
11121115

11131116
const endpoint = new QuicEndpoint(this, options);
11141117
state.endpoints.add(endpoint);
1115-
1116-
// If the QuicSocket is already bound at this point,
1117-
// also bind the newly created QuicEndpoint.
1118-
if (state.state !== kSocketUnbound)
1119-
endpoint[kMaybeBind]();
1120-
11211118
return endpoint;
11221119
}
11231120

test/parallel/test-quic-quicendpoint-address.js

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ async function Test2() {
4343

4444
server.listen({ key, cert, ca, alpn: 'zzz' });
4545

46+
// Attempting to add an endpoint after fails.
47+
assert.throws(() => server.addEndpoint(), {
48+
code: 'ERR_INVALID_STATE'
49+
});
50+
4651
await once(server, 'ready');
4752

4853
assert.strictEqual(server.endpoints.length, 2);

0 commit comments

Comments
 (0)