Skip to content

MaxListenersExceededWarning when running many sequential child tasks via batchTriggerAndWait #2708

@itsfabioroma

Description

@itsfabioroma

Bug Description

When running a parent task that triggers many child tasks via batchTriggerAndWait, a MaxListenersExceededWarning appears after ~10 child tasks complete. The warning indicates SIGTERM listeners are being added to the process without cleanup.

Environment

  • SDK Version: 4.1.2 (@trigger.dev/sdk, @trigger.dev/react-hooks, @trigger.dev/build)
  • Node.js: v22
  • OS: macOS (darwin)
  • Runtime: node

Steps to Reproduce

  1. Create a parent task that uses batch.triggerAndWait() to trigger multiple child tasks
  2. Child tasks are simple (send HTTP request, update parent metadata via metadata.parent.increment())
  3. Run the parent task with 10+ child tasks
  4. Observe warning in terminal after ~10 child tasks complete

Code Structure

// Parent task
export const parentTask = task({
  id: "parent-task",
  run: async (payload) => {
    const batchItems = [/* 13 items */];
    
    const batchHandle = await batch.triggerAndWait<typeof childTask>(
      batchItems.map(item => ({
        id: "child-task",
        payload: item.payload,
        options: { concurrencyKey: item.payload.instanceNumber },
      }))
    );
    
    // Process results...
  },
});

// Child task
export const childTask = task({
  id: "child-task",
  queue: { concurrencyLimit: 1 },
  run: async (payload) => {
    // Make HTTP request
    const response = await fetch(apiEndpoint, { ... });
    
    // Update parent metadata
    metadata.parent.increment("sent", 1);
    
    return { success: true, ... };
  },
});

Warning Output

(node:54268) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGTERM listeners added to [process]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)

Expected Behavior

No warning should appear. SIGTERM listeners should either:

  • Be reused across child task runs
  • Be properly removed after each child task completes

Actual Behavior

Each child task run appears to add a new SIGTERM listener without removing it, causing the warning after 10+ runs.

Notes

  • Functionality is not affected - tasks complete successfully and realtime metadata updates work correctly
  • The warning appears to be from SDK internals managing graceful shutdown handlers
  • Child tasks don't use any external clients (no Supabase, no DB connections) - only fetch and metadata.parent

Workaround Attempted

Tried process.setMaxListeners(50) but this just masks the issue rather than fixing the underlying listener accumulation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions