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.

Merge branch 'fix/version-check-fetch-timeout' of https://github.com/rajarshidattapy/composio into fix/version-check-fetch-timeout

rajashidattapy0330637a187d
diff --cc ts/packages/core/src/utils/version.ts
index f1ae55301,01dafcc84..fa1d96e36
--- a/ts/packages/core/src/utils/version.ts
+++ b/ts/packages/core/src/utils/version.ts
@@@ -48,27 -47,11 +47,22 @@@ export async function checkForLatestVer
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 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.
- // 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),
- });
- const data = await response.json();
++ // Bound this best-effort request so a stalled registry cannot leave it pending
++ // indefinitely. 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
++ // successful check.
+ 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 --cc ts/packages/core/test/utils/version.test.ts
index 6616a6f31,c0ae59729..b62300cb7
--- a/ts/packages/core/test/utils/version.test.ts
+++ b/ts/packages/core/test/utils/version.test.ts
@@@ -3,71 -3,40 +3,41 @@@ import { checkForLatestVersionFromNPM
describe('checkForLatestVersionFromNPM', () => {
afterEach(() => {
++ vi.useRealTimers();
+ 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' })));
- 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);
- });
-
- it('leaves no pending timer once the registry responds', async () => {
- it('bounds the registry request with the configured timeout signal', async () => {
- const signal = new AbortController().signal;
- const timeoutSpy = vi.spyOn(AbortSignal, 'timeout').mockReturnValue(signal);
++ it('bounds the registry request and clears the timer once it responds', async () => {
+ vi.useFakeTimers();
- vi.stubGlobal(
- 'fetch',
- vi.fn(async () => new Response(JSON.stringify({ version: '0.0.1' })))
- );
+ const fetchMock = vi.fn<typeof fetch>(async () => Response.json({ version: '0.0.1' }));
+ vi.stubGlobal('fetch', fetchMock);
await checkForLatestVersionFromNPM('1.0.0');
- // An uncleared timeout keeps a workerd request context alive for its full duration.
- expect(timeoutSpy).toHaveBeenCalledWith(2_000);
+ expect(fetchMock).toHaveBeenCalledWith('https://registry.npmjs.org/@composio/core/latest', {
- signal,
++ signal: expect.any(AbortSignal),
+ });
++ // A timer left pending after the fetch resolved keeps a workerd request context
++ // open for the full timeout on every successful check.
+ 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.
- vi.stubGlobal(
- 'fetch',
- vi.fn(
- (_url: string, init?: RequestInit) =>
- new Promise<Response>((_resolve, reject) => {
- init?.signal?.addEventListener('abort', () => reject(init.signal!.reason));
- })
- )
- );
-
- // 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');
- })
+ it('swallows a timeout from a registry request that never responds', async () => {
- const timeoutController = new AbortController();
- vi.spyOn(AbortSignal, 'timeout').mockReturnValue(timeoutController.signal);
++ vi.useFakeTimers();
+ const fetchMock = vi.fn<typeof fetch>(
+ (_input, init) =>
+ new Promise<Response>((_resolve, reject) => {
+ init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), {
+ once: true,
+ });
+ })
);
-
- 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'));
++ await vi.advanceTimersByTimeAsync(2_000);
- expect(fetchMock).not.toHaveBeenCalled();
+ await expect(versionCheck).resolves.toBeUndefined();
});
});