@@ -181,24 +181,33 @@ export class AtlasService {
181181 }
182182 this . refreshing = true ;
183183 try {
184- await Promise . race ( [
185- // When oidc-plugin logged that token was refreshed, the token is not
186- // actually refreshed yet in the plugin state and so calling `REFRESH_TOKEN_CALLBACK`
187- // causes weird behavior that actually opens the browser again, to work
188- // around that we wait for the state update event in addition. We can't
189- // guarantee that this event will be emitted for our particular state as
190- // this is not something oidc-plugin exposes, but we can ignore this for
191- // now as only one auth state is created in this instance of oidc-plugin
192- once ( this . oidcPluginLogger , 'mongodb-oidc-plugin:state-updated' ) ,
193- // At the same time refresh can still fail at this stage, so to avoid
194- // refresh being stuck, we also wait for refresh-failed event and throw
195- // if it happens to avoid calling `REFRESH_TOKEN_CALLBACK`
196- once ( this . oidcPluginLogger , 'mongodb-oidc-plugin:refresh-failed' ) . then (
197- ( ) => {
184+ // We expect only one promise below to resolve, to clean up listeners that
185+ // never fired we are setting up an abort controller
186+ const listenerController = new AbortController ( ) ;
187+ try {
188+ await Promise . race ( [
189+ // When oidc-plugin logged that token was refreshed, the token is not
190+ // actually refreshed yet in the plugin state and so calling `REFRESH_TOKEN_CALLBACK`
191+ // causes weird behavior that actually opens the browser again, to work
192+ // around that we wait for the state update event in addition. We can't
193+ // guarantee that this event will be emitted for our particular state as
194+ // this is not something oidc-plugin exposes, but we can ignore this for
195+ // now as only one auth state is created in this instance of oidc-plugin
196+ once ( this . oidcPluginLogger , 'mongodb-oidc-plugin:state-updated' , {
197+ signal : listenerController . signal ,
198+ } ) ,
199+ // At the same time refresh can still fail at this stage, so to avoid
200+ // refresh being stuck, we also wait for refresh-failed event and throw
201+ // if it happens to avoid calling `REFRESH_TOKEN_CALLBACK`
202+ once ( this . oidcPluginLogger , 'mongodb-oidc-plugin:refresh-failed' , {
203+ signal : listenerController . signal ,
204+ } ) . then ( ( ) => {
198205 throw new Error ( 'Refresh failed' ) ;
199- }
200- ) ,
201- ] ) ;
206+ } ) ,
207+ ] ) ;
208+ } finally {
209+ listenerController . abort ( ) ;
210+ }
202211 try {
203212 const token =
204213 await this . plugin . mongoClientOptions . authMechanismProperties
@@ -240,16 +249,30 @@ export class AtlasService {
240249 // is trying to refresh the token automatically, we can wait for this process
241250 // to finish before proceeding with a request
242251 if ( this . oidcPluginSyncedFromLoggerState === 'expired' ) {
243- await Promise . race ( [
244- // We are using our own events here and not oidc plugin ones because
245- // after plugin logged that token was refreshed, we still need to run
246- // REFRESH_TOKEN_CALLBACK to get the actual token value in the state
247- once ( this . oidcPluginLogger , 'atlas-service-token-refreshed' ) ,
248- once ( this . oidcPluginLogger , 'atlas-service-token-refresh-failed' ) ,
249- new Promise ( ( resolve ) => {
250- signal ?. addEventListener ( 'abort' , resolve , { once : true } ) ;
251- } ) ,
252- ] ) ;
252+ // We expect only one promise below to resolve, to clean up listeners that
253+ // never fired we are setting up an abort controller
254+ const listenerController = new AbortController ( ) ;
255+ try {
256+ await Promise . race ( [
257+ // We are using our own events here and not oidc plugin ones because
258+ // after plugin logged that token was refreshed, we still need to run
259+ // REFRESH_TOKEN_CALLBACK to get the actual token value in the state
260+ once ( this . oidcPluginLogger , 'atlas-service-token-refreshed' , {
261+ signal : listenerController . signal ,
262+ } ) ,
263+ once ( this . oidcPluginLogger , 'atlas-service-token-refresh-failed' , {
264+ signal : listenerController . signal ,
265+ } ) ,
266+ signal
267+ ? once ( signal , 'abort' , { signal : listenerController . signal } )
268+ : new Promise ( ( ) => {
269+ // This should just never resolve if no signal was passed to
270+ // this method
271+ } ) ,
272+ ] ) ;
273+ } finally {
274+ listenerController . abort ( ) ;
275+ }
253276 }
254277 }
255278
0 commit comments