Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(version): improve timeout handling in NPM version check and add related tests

rajashidattapyb08b18d73b1e
diff --git a/ts/packages/core/src/utils/version.ts b/ts/packages/core/src/utils/version.ts
index e99fbfe62..f1ae55301 100644
--- a/ts/packages/core/src/utils/version.ts
+++ b/ts/packages/core/src/utils/version.ts
@@ -53,11 +53,22 @@ export async function checkForLatestVersionFromNPM(currentVersion: string) {
// keeps its socket referenced on the event loop, so without a signal a slow or
// black-holed registry (corporate proxy, egress firewall that drops rather than
// rejects, npm incident) keeps short-lived scripts alive until undici's own
- // timeouts fire. The catch below already swallows the resulting TimeoutError.
- const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`, {
- signal: AbortSignal.timeout(VERSION_CHECK_TIMEOUT_MS),
- });
- const data = await response.json();
+ // timeouts fire. The catch below already swallows the resulting abort.
+ //
+ // A hand-rolled timer rather than AbortSignal.timeout so it can be cleared the
+ // moment the response lands: an uncleared timer is itself pending work, which on
+ // workerd pins the request context open for the full timeout on every success.
+ const controller = new AbortController();
+ const timer = setTimeout(() => controller.abort(), VERSION_CHECK_TIMEOUT_MS);
+ let data;
+ try {
+ const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`, {
+ signal: controller.signal,
+ });
+ data = await response.json();
+ } finally {
+ clearTimeout(timer);
+ }
const latestVersion = data.version;
if (semver.gt(latestVersion, currentVersionFromPackageJson) && !IS_DEVELOPMENT_OR_CI) {
diff --git a/ts/packages/core/test/utils/version.test.ts b/ts/packages/core/test/utils/version.test.ts
index 2d9796beb..6616a6f31 100644
--- a/ts/packages/core/test/utils/version.test.ts
+++ b/ts/packages/core/test/utils/version.test.ts
@@ -17,6 +17,20 @@ describe('checkForLatestVersionFromNPM', () => {
expect(init?.signal).toBeInstanceOf(AbortSignal);
});
+ it('leaves no pending timer once the registry responds', async () => {
+ vi.useFakeTimers();
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () => new Response(JSON.stringify({ version: '0.0.1' })))
+ );
+
+ await checkForLatestVersionFromNPM('1.0.0');
+
+ // An uncleared timeout keeps a workerd request context alive for its full duration.
+ expect(vi.getTimerCount()).toBe(0);
+ vi.useRealTimers();
+ });
+
it('gives up instead of hanging when the registry never responds', async () => {
// A registry that accepts the request and never answers. Without a timeout this
// promise never settles, which is exactly what keeps a real process alive.