@devries-cameron20 / composio-1 / commits / 1c829b4f7
Fork 0

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): contain rejected promises from async onSubscriptionError handlers

The containment around the subscription error callback only caught synchronous throws. TypeScript accepts an async function against a void-returning callback type, so a rejected promise escaped as an unhandled rejection after subscribe() resolved - on modern Node that can terminate the process, the same failure class this PR contains. Route the callback result through Promise.resolve(...).catch so both sync throws and async rejections land in the same contained logger path, and add a regression test for the async case. Addresses greptile-apps P1 and Cursor Bugbot review comments.

jkomyno committed Sep 15, 2026 at 17:09 UTC 1c829b4f732b4cc54184cabcb7a80315c324a16f
3 files changed +42 -6
ts/packages/core/src/models/Triggers.ts
+1
@@ -615,6 +615,7 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
615 * @param onSubscriptionError - Optional callback invoked with the raw Pusher payload when
616 * the underlying subscription fails (for example on auth or permission rejection). The
617 * subscribe promise still resolves; this is the only programmatic signal of the failure.
618 + * Errors thrown from — or promises rejected by — the callback are contained and logged.
619 *
620 * @example
621 * ```ts
ts/packages/core/src/services/pusher/Pusher.ts
+12 -6
@@ -164,7 +164,7 @@ export class PusherService {
164 * @param fn - The function to call when the event is received
165 * @param onSubscriptionError - Optional callback invoked with the raw payload when the
166 * Pusher subscription fails (for example on auth or permission rejection). Errors thrown
167 - * from this callback are contained and logged, never rethrown.
167 + * from — or promises rejected by — this callback are contained and logged, never rethrown.
168 */
169 async subscribe(
170 fn: (data: Record<string, unknown>) => void,
@@ -176,17 +176,23 @@ export class PusherService {
176 const channel = await pusherClient.subscribe(this.pusherChannel);
177
178 // add subscription error handling
179 + const logCallbackFailure = (callbackError: unknown) => {
180 + const errorMessage =
181 + callbackError instanceof Error ? callbackError.message : String(callbackError);
182 + logger.error('❌ Error in subscription error callback:', errorMessage);
183 + };
184 channel.bind('pusher:subscription_error', (data: Record<string, unknown>) => {
185 logger.error('Trigger subscription error:', data);
186
187 // surface the failure to the caller without letting a faulty
183 - // handler crash the host (same containment as the trigger callback)
188 + // handler crash the host (same containment as the trigger callback).
189 + // A handler may be async: contain rejected promises too, or the
190 + // rejection escapes as an unhandled rejection after subscribe()
191 + // already resolved.
192 try {
185 - onSubscriptionError?.(data);
193 + Promise.resolve(onSubscriptionError?.(data)).catch(logCallbackFailure);
194 } catch (callbackError: unknown) {
187 - const errorMessage =
188 - callbackError instanceof Error ? callbackError.message : String(callbackError);
189 - logger.error('❌ Error in subscription error callback:', errorMessage);
195 + logCallbackFailure(callbackError);
196 }
197 });
198
ts/packages/core/test/services/pusher.test.ts
+29
@@ -158,4 +158,33 @@ describe('PusherService subscription errors', () => {
158 'handler exploded'
159 );
160 });
161 +
162 + it('contains rejections from async subscription error callbacks', async () => {
163 + const service = new PusherService({
164 + baseURL: 'https://backend.composio.dev',
165 + apiKey: 'api-key',
166 + } as never);
167 + const onSubscriptionError = vi.fn(async () => {
168 + throw new Error('async handler failed');
169 + });
170 +
171 + await service.subscribe(vi.fn(), onSubscriptionError);
172 +
173 + await new Promise<void>(resolve => {
174 + setImmediate(resolve);
175 + });
176 +
177 + mockChannel.emit('pusher:subscription_error', { error: 401 });
178 +
179 + // let the rejected promise settle and the containment log run
180 + await new Promise<void>(resolve => {
181 + setImmediate(resolve);
182 + });
183 +
184 + expect(onSubscriptionError).toHaveBeenCalledTimes(1);
185 + expect(mockLoggerError).toHaveBeenCalledWith(
186 + '❌ Error in subscription error callback:',
187 + 'async handler failed'
188 + );
189 + });
190 });