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(core): complete version check timeout guard

jkomyno2ac6ad320ad6
diff --git a/.changeset/bound-version-check-request.md b/.changeset/bound-version-check-request.md
new file mode 100644
index 000000000..8b5d7b46e
--- /dev/null
+++ b/.changeset/bound-version-check-request.md
@@ -0,0 +1,5 @@
+---
+'@composio/core': patch
+---
+
+Bound the background npm version check so registry outages cannot leave the request pending indefinitely.
diff --git a/ts/packages/core/src/utils/version.ts b/ts/packages/core/src/utils/version.ts
index 9fb49b93b..01dafcc84 100644
--- a/ts/packages/core/src/utils/version.ts
+++ b/ts/packages/core/src/utils/version.ts
@@ -19,15 +19,14 @@ export function isNewerVersion(version1: string, version2: string): boolean {
return false;
}
-/** Upper bound on the npm registry version check. Kept short: it is best-effort and unawaited. */
-const VERSION_CHECK_TIMEOUT_MS = 2000;
+const VERSION_CHECK_TIMEOUT_MS = 2_000;
/**
* Checks for the latest version of the Composio SDK from NPM.
* If a newer version is available, it logs a warning to the console.
*
- * Best-effort: never throws, and never keeps the process alive for more than
- * {@link VERSION_CHECK_TIMEOUT_MS}.
+ * Best-effort: never throws. The npm registry request is aborted after
+ * {@link VERSION_CHECK_TIMEOUT_MS} milliseconds.
*/
export async function checkForLatestVersionFromNPM(currentVersion: string) {
try {
@@ -48,12 +47,7 @@ export async function checkForLatestVersionFromNPM(currentVersion: string) {
return;
}
- // Bounded and abortable: this check is a non-essential background nicety that
- // nobody awaits, so it must never extend process lifetime. An in-flight fetch
- // 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.
+ // Bound this best-effort request so a stalled registry cannot leave it pending indefinitely.
const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`, {
signal: AbortSignal.timeout(VERSION_CHECK_TIMEOUT_MS),
});
diff --git a/ts/packages/core/test/utils/version.test.ts b/ts/packages/core/test/utils/version.test.ts
index 2d9796beb..c0ae59729 100644
--- a/ts/packages/core/test/utils/version.test.ts
+++ b/ts/packages/core/test/utils/version.test.ts
@@ -3,57 +3,40 @@ import { checkForLatestVersionFromNPM } from '../../src/utils/version';
describe('checkForLatestVersionFromNPM', () => {
afterEach(() => {
+ vi.restoreAllMocks();
vi.unstubAllGlobals();
});
- it('bounds the registry request with an AbortSignal so it cannot outlive the process', async () => {
- const fetchMock = vi.fn(async () => new Response(JSON.stringify({ version: '0.0.1' })));
+ it('bounds the registry request with the configured timeout signal', async () => {
+ const signal = new AbortController().signal;
+ const timeoutSpy = vi.spyOn(AbortSignal, 'timeout').mockReturnValue(signal);
+ const fetchMock = vi.fn<typeof fetch>(async () => Response.json({ version: '0.0.1' }));
vi.stubGlobal('fetch', fetchMock);
await checkForLatestVersionFromNPM('1.0.0');
- expect(fetchMock).toHaveBeenCalledOnce();
- const init = fetchMock.mock.calls[0]![1] as RequestInit | undefined;
- expect(init?.signal).toBeInstanceOf(AbortSignal);
+ expect(timeoutSpy).toHaveBeenCalledWith(2_000);
+ expect(fetchMock).toHaveBeenCalledWith('https://registry.npmjs.org/@composio/core/latest', {
+ signal,
+ });
});
- 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.
- vi.stubGlobal(
- 'fetch',
- vi.fn(
- (_url: string, init?: RequestInit) =>
- new Promise<Response>((_resolve, reject) => {
- init?.signal?.addEventListener('abort', () => reject(init.signal!.reason));
- })
- )
+ it('swallows a timeout from a registry request that never responds', async () => {
+ const timeoutController = new AbortController();
+ vi.spyOn(AbortSignal, 'timeout').mockReturnValue(timeoutController.signal);
+ const fetchMock = vi.fn<typeof fetch>(
+ (_input, init) =>
+ new Promise<Response>((_resolve, reject) => {
+ init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), {
+ once: true,
+ });
+ })
);
-
- // Resolves (rather than hanging) only because the signal aborts it; the
- // function swallows the resulting TimeoutError.
- await expect(checkForLatestVersionFromNPM('1.0.0')).resolves.toBeUndefined();
- });
-
- it('never rejects when the registry errors', async () => {
- vi.stubGlobal(
- 'fetch',
- vi.fn(async () => {
- throw new Error('ENOTFOUND registry.npmjs.org');
- })
- );
-
- await expect(checkForLatestVersionFromNPM('1.0.0')).resolves.toBeUndefined();
- });
-
- it('skips the request entirely for non-semver and prerelease versions', async () => {
- const fetchMock = vi.fn(async () => new Response(JSON.stringify({ version: '9.9.9' })));
vi.stubGlobal('fetch', fetchMock);
- await checkForLatestVersionFromNPM('not-a-version');
- await checkForLatestVersionFromNPM('1.0.0-alpha.1');
- await checkForLatestVersionFromNPM('1.0.0-beta.3');
+ const versionCheck = checkForLatestVersionFromNPM('1.0.0');
+ timeoutController.abort(new DOMException('The operation timed out', 'TimeoutError'));
- expect(fetchMock).not.toHaveBeenCalled();
+ await expect(versionCheck).resolves.toBeUndefined();
});
});