-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-quic-session-update-data-stats-after-close.mjs
More file actions
54 lines (45 loc) Β· 1.91 KB
/
test-quic-session-update-data-stats-after-close.mjs
File metadata and controls
54 lines (45 loc) Β· 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Flags: --experimental-quic --no-warnings
// Regression test for https://github.com/nodejs/node/pull/62126
// UpdateDataStats() must not crash when called after the session's impl_ has
// been reset to null (i.e. after the session is destroyed).
//
// The crash path is in Session::SendDatagram():
// auto on_exit = OnScopeLeave([&] { ...; UpdateDataStats(); });
// If the send encounters an error it calls Close(SILENT) β Destroy() β
// impl_.reset(). The on_exit lambda then fires with impl_ == nullptr.
import { hasQuic, skip, mustCall } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
if (!hasQuic) {
skip('QUIC is not enabled');
}
const { listen, connect } = await import('node:quic');
const { createPrivateKey } = await import('node:crypto');
const keys = createPrivateKey(fixtures.readKey('agent1-key.pem'));
const certs = fixtures.readKey('agent1-cert.pem');
// Datagrams must be enabled on both sides for sendDatagram() to work.
const kDatagramOptions = {
transportParams: {
maxDatagramFrameSize: 1200n,
},
};
const serverEndpoint = await listen(
mustCall((serverSession) => {
serverSession.opened.then(mustCall(async () => {
// Send a datagram then immediately close. This exercises the
// UpdateDataStats() call that fires via on_exit after SendDatagram β
// the close can race with or precede the stats update, leaving
// impl_ == nullptr. Before the fix this would crash.
serverSession.sendDatagram(Buffer.from('hello')).catch(() => {});
serverSession.close();
}));
}),
{ keys, certs, ...kDatagramOptions },
);
const clientSession = await connect(serverEndpoint.address, kDatagramOptions);
await clientSession.opened;
// Mirror the race on the client side.
clientSession.sendDatagram(Buffer.from('world')).catch(() => {});
clientSession.close();
await clientSession.closed;
serverEndpoint.close();
await serverEndpoint.closed;