diff --git a/packages/middleware-endpoint-discovery/src/updateDiscoveredEndpointInCache.spec.ts b/packages/middleware-endpoint-discovery/src/updateDiscoveredEndpointInCache.spec.ts index c8310787b56d9..e5edc6d09802d 100644 --- a/packages/middleware-endpoint-discovery/src/updateDiscoveredEndpointInCache.spec.ts +++ b/packages/middleware-endpoint-discovery/src/updateDiscoveredEndpointInCache.spec.ts @@ -120,7 +120,8 @@ describe(updateDiscoveredEndpointInCache.name, () => { ); } verifyCallsOnCacheUndefined(); - expect(mockDelete).not.toHaveBeenCalled(); + expect(mockDelete).toHaveBeenCalledTimes(1); + expect(mockDelete).toHaveBeenCalledWith(cacheKey); expect(mockSet).toHaveBeenCalledTimes(1); expect(mockSet).toHaveBeenCalledWith(cacheKey, placeholderEndpoints); }); @@ -133,7 +134,8 @@ describe(updateDiscoveredEndpointInCache.name, () => { await updateDiscoveredEndpointInCache(config, options); verifyCallsOnCacheUndefined(); - expect(mockDelete).not.toHaveBeenCalled(); + expect(mockDelete).toHaveBeenCalledTimes(1); + expect(mockDelete).toHaveBeenCalledWith(cacheKey); expect(mockSet).toHaveBeenCalledTimes(2); expect(mockSet).toHaveBeenNthCalledWith(1, cacheKey, placeholderEndpoints); expect(mockSet).toHaveBeenNthCalledWith(2, cacheKey, placeholderEndpoints); diff --git a/packages/middleware-endpoint-discovery/src/updateDiscoveredEndpointInCache.ts b/packages/middleware-endpoint-discovery/src/updateDiscoveredEndpointInCache.ts index 99f5c619e0c7f..6823ceebd663e 100644 --- a/packages/middleware-endpoint-discovery/src/updateDiscoveredEndpointInCache.ts +++ b/packages/middleware-endpoint-discovery/src/updateDiscoveredEndpointInCache.ts @@ -54,10 +54,11 @@ export const updateDiscoveredEndpointInCache = async ( resolve(); }) .catch((error: any) => { - if (error.name === "InvalidEndpointException" || error.$metadata?.httpStatusCode === 421) { - // Endpoint is invalid, delete the cache entry. - endpointCache.delete(cacheKey); - } + // The cache entry must be deleted + // because a subsequent blocking request will be stuck + // in a waiting state if it sees the cache entry + // but we have already flushed the request queue. + endpointCache.delete(cacheKey); const errorToThrow = Object.assign( new Error(