Skip to content

Commit 66cd851

Browse files
committed
grpc-js: pick_first: Properly dispose of current pick when it disconnects
1 parent 698d142 commit 66cd851

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

packages/grpc-js/src/load-balancer-pick-first.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,29 @@ export class PickFirstLoadBalancer implements LoadBalancer {
223223
this.calculateAndReportNewState();
224224
}
225225

226+
private removeCurrentPick() {
227+
if (this.currentPick !== null) {
228+
/* Unref can cause a state change, which can cause a change in the value
229+
* of this.currentPick, so we hold a local reference to make sure that
230+
* does not impact this function. */
231+
const currentPick = this.currentPick;
232+
this.currentPick = null;
233+
currentPick.unref();
234+
currentPick.removeConnectivityStateListener(this.subchannelStateListener);
235+
this.channelControlHelper.removeChannelzChild(
236+
currentPick.getChannelzRef()
237+
);
238+
}
239+
}
240+
226241
private onSubchannelStateUpdate(
227242
subchannel: SubchannelInterface,
228243
previousState: ConnectivityState,
229244
newState: ConnectivityState
230245
) {
231246
if (this.currentPick?.realSubchannelEquals(subchannel)) {
232247
if (newState !== ConnectivityState.READY) {
233-
this.currentPick = null;
248+
this.removeCurrentPick();
234249
this.calculateAndReportNewState();
235250
this.channelControlHelper.requestReresolution();
236251
}
@@ -415,17 +430,7 @@ export class PickFirstLoadBalancer implements LoadBalancer {
415430

416431
destroy() {
417432
this.resetSubchannelList();
418-
if (this.currentPick !== null) {
419-
/* Unref can cause a state change, which can cause a change in the value
420-
* of this.currentPick, so we hold a local reference to make sure that
421-
* does not impact this function. */
422-
const currentPick = this.currentPick;
423-
currentPick.unref();
424-
currentPick.removeConnectivityStateListener(this.subchannelStateListener);
425-
this.channelControlHelper.removeChannelzChild(
426-
currentPick.getChannelzRef()
427-
);
428-
}
433+
this.removeCurrentPick();
429434
}
430435

431436
getTypeName(): string {

0 commit comments

Comments
 (0)