@@ -16,6 +16,7 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
16
16
public traceProvider : BasicTracerProvider | undefined ;
17
17
private _tracer : Tracer | undefined ;
18
18
private _clientReportInterval : NodeJS . Timeout | undefined ;
19
+ private _clientReportOnExitFlushListener : ( ( ) => undefined ) | undefined ;
19
20
20
21
public constructor ( options : NodeClientOptions ) {
21
22
const clientOptions : ServerRuntimeClientOptions = {
@@ -35,8 +36,8 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
35
36
36
37
if ( clientOptions . sendClientReports !== false ) {
37
38
// There is one mild concern here, being that if users periodically and unboundedly create new clients, we will
38
- // create more and more intervals, which may leak memory. In these situations, users are required to
39
- // call `client.close()` in order to dispose of the client resource .
39
+ // create more and more intervals and beforeExit listeners , which may leak memory. In these situations, users are
40
+ // required to call `client.close()` in order to dispose of the acquired resources .
40
41
// Users are already confronted with the same reality with the SessionFlusher at the time of writing this so the
41
42
// working theory is that this should be fine.
42
43
// Note: We have experimented with using `FinalizationRegisty` to clear the interval when the client is garbage
@@ -47,6 +48,12 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
47
48
} , CLIENT_REPORT_FLUSH_INTERVAL_MS )
48
49
// Unref is critical, otherwise we stop the process from exiting by itself
49
50
. unref ( ) ;
51
+
52
+ this . _clientReportOnExitFlushListener = ( ) => {
53
+ this . _flushOutcomes ( ) ;
54
+ } ;
55
+
56
+ process . on ( 'beforeExit' , this . _clientReportOnExitFlushListener ) ;
50
57
}
51
58
}
52
59
@@ -86,6 +93,10 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
86
93
clearInterval ( this . _clientReportInterval ) ;
87
94
}
88
95
96
+ if ( this . _clientReportOnExitFlushListener ) {
97
+ process . off ( 'beforeExit' , this . _clientReportOnExitFlushListener ) ;
98
+ }
99
+
89
100
return super . close ( timeout ) ;
90
101
}
91
102
}
0 commit comments