Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions packages/services/service-common/src/iam-aws.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout as sleep } from 'node:timers/promises';
import { Sha256 } from '@aws-crypto/sha256-js';
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
import { formatUrl } from '@aws-sdk/util-format-url';
Expand Down Expand Up @@ -114,45 +115,51 @@ export function startTokenRefreshTimer(
const retryBackoffMs = options?.retryBackoffMs ?? 5_000;

// Track shutdown state to prevent in-flight callbacks and retries during graceful shutdown
let isShuttingDown = false;
const abortController = new AbortController();
let currentTimer: ReturnType<typeof setTimeout> | undefined;

function scheduleNext() {
if (isShuttingDown) return;

if (abortController.signal.aborted) return;
const jitterMs = Math.floor(Math.random() * maxJitterMs);

currentTimer = setTimeout(() => {
if (isShuttingDown) return;
if (abortController.signal.aborted) return;

void (async () => {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
if (isShuttingDown) return;
if (abortController.signal.aborted) return;

try {
await refreshFn(attempt, maxRetries);
break;
} catch {
if (attempt < maxRetries) {
await new Promise(r => setTimeout(r, attempt * retryBackoffMs));
if (attempt >= maxRetries) {
return;
}
}

try {
await sleep(attempt * retryBackoffMs, {
signal: abortController.signal,
});
} catch (err) {
if (abortController.signal.aborted) {
return;
}
throw err;
}
}
// Schedule next cycle only after current refresh (including retries) completes
scheduleNext();
})();
}, intervalMs + jitterMs);

// Allow clean process shutdown even if the refresh timer is still active.
if (typeof currentTimer.unref === 'function') {
currentTimer.unref();
}
}

scheduleNext();

// Return a cleanup function that sets shutdown flag and clears the pending timer
return () => {
isShuttingDown = true;
abortController.abort();
if (currentTimer !== undefined) {
clearTimeout(currentTimer);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/services/service-common/src/redis-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
// Start IAM token refresh if enabled
// Each client gets its own independent token lifecycle
if (redisIamConfig) {
startIamTokenRefresh(redis, redisIamConfig, options.logger);
const stopTokenRefresh = startIamTokenRefresh(redis, redisIamConfig, options.logger);
redis.on('end', stopTokenRefresh);

Check failure on line 98 in packages/services/service-common/src/redis-client.ts

View workflow job for this annotation

GitHub Actions / test / unit

packages/services/service-common/src/redis-client.spec.ts > createRedisClient > two-client pattern (server/workflows use case) > each client call resolves credentials independently

TypeError: The "listener" argument must be of type function. Received undefined ❯ createRedisClient packages/services/service-common/src/redis-client.ts:98:11 ❯ packages/services/service-common/src/redis-client.spec.ts:474:25 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_ARG_TYPE' }

Check failure on line 98 in packages/services/service-common/src/redis-client.ts

View workflow job for this annotation

GitHub Actions / test / unit

packages/services/service-common/src/redis-client.spec.ts > createRedisClient > two-client pattern (server/workflows use case) > supports calling createRedisClient twice with independent IAM token refreshes

TypeError: The "listener" argument must be of type function. Received undefined ❯ createRedisClient packages/services/service-common/src/redis-client.ts:98:11 ❯ packages/services/service-common/src/redis-client.spec.ts:433:25 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_ARG_TYPE' }

Check failure on line 98 in packages/services/service-common/src/redis-client.ts

View workflow job for this annotation

GitHub Actions / test / unit

packages/services/service-common/src/redis-client.spec.ts > createRedisClient > IAM-based authentication > respects clusterModeEnabled flag when starting IAM token refresh

TypeError: The "listener" argument must be of type function. Received undefined ❯ createRedisClient packages/services/service-common/src/redis-client.ts:98:11 ❯ packages/services/service-common/src/redis-client.spec.ts:412:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_ARG_TYPE' }

Check failure on line 98 in packages/services/service-common/src/redis-client.ts

View workflow job for this annotation

GitHub Actions / test / unit

packages/services/service-common/src/redis-client.spec.ts > createRedisClient > IAM-based authentication > starts IAM token refresh when IAM is enabled

TypeError: The "listener" argument must be of type function. Received undefined ❯ createRedisClient packages/services/service-common/src/redis-client.ts:98:11 ❯ packages/services/service-common/src/redis-client.spec.ts:379:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_ARG_TYPE' }

Check failure on line 98 in packages/services/service-common/src/redis-client.ts

View workflow job for this annotation

GitHub Actions / test / unit

packages/services/service-common/src/redis-client.spec.ts > createRedisClient > IAM-based authentication > defaults username to "default" when not provided in IAM config

TypeError: The "listener" argument must be of type function. Received undefined ❯ createRedisClient packages/services/service-common/src/redis-client.ts:98:11 ❯ packages/services/service-common/src/redis-client.spec.ts:362:7 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_ARG_TYPE' }

Check failure on line 98 in packages/services/service-common/src/redis-client.ts

View workflow job for this annotation

GitHub Actions / test / unit

packages/services/service-common/src/redis-client.spec.ts > createRedisClient > IAM-based authentication > derives redisIamConfig from environment config when IAM is enabled

TypeError: The "listener" argument must be of type function. Received undefined ❯ createRedisClient packages/services/service-common/src/redis-client.ts:98:11 ❯ packages/services/service-common/src/redis-client.spec.ts:338:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_ARG_TYPE' }
}

return redis;
Expand Down
2 changes: 1 addition & 1 deletion packages/services/workflows/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ registerShutdown({
await pg.end();
logger.info('Shutdown postgres connection successful.');
logger.info('Shutdown redis connection.');
redis.disconnect(false);
await Promise.all([redis.quit(), redisSubscriber.quit()]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated the the actual changes 😓 - i just noticed we are not cleaning up the subscriber client

if (shutdownMetrics) {
logger.info('Stopping prometheus endpoint');
await shutdownMetrics();
Expand Down
Loading