Skip to content

fix(core): release per-run process listeners and task history results - #36974

Merged
FrozenPandaz merged 1 commit into
22.7.xfrom
backport/36866-22.7.x
Sep 9, 2026
Merged

FrozenPandaz merged 1 commit into
22.7.xfrom
backport/36866-22.7.x

Conversation

@AgentEnder

Copy link
Copy Markdown
Member

Backport of #36866 to 22.7.x. The leak goes back to at least 22.5, so agents on this line still grow unbounded.

The port differs from the original in three places, since 22.7.x still has separate signal handlers, a synchronous ForkedProcessTaskRunner.cleanup(), and jest rather than vitest. The commit message covers each.

Current Behavior

Every runDiscreteTasks call registers SIGINT/SIGTERM/SIGHUP handlers that are never removed. Each closes over its orchestrator, so an Nx Cloud agent retains every orchestrator it ever created, along with its task hasher and every task result. TaskHistoryLifeCycle.pendingResults keeps every TaskResult for the life of the process too.

Expected Behavior

Listeners are released once a run completes, and agent memory stays flat across assignment batches.

Related Issue(s)

Fixes #

…#36866)

## Current Behavior

Every `runDiscreteTasks` call registers SIGINT/SIGTERM/SIGHUP handlers
(plus the forked runner's message/exit handlers) that are never removed.
Each closes over its orchestrator, so a long-lived process like an Nx
Cloud agent retains every orchestrator it ever created: the task hasher
(with its native project-graph copy), the lifecycles, and every task
result with full terminal output. `TaskHistoryLifeCycle.pendingResults`
also keeps every `TaskResult` for the life of the process. A profiled
customer agent grew from 237MB to 5.5GB over 50 tasks.

## Expected Behavior

Process listeners are removed once a run completes, task history stops
retaining terminal output, and agent memory stays flat across assignment
batches.

## Backport notes

Three places where 22.7.x differs from the branch this came from:

- `setupSignalHandlers` still registers three separate `once` handlers
  with different bodies here, rather than the single `handleSignal`
  helper. Each is hoisted to a named const so `dispose()` can remove it;
  the bodies are unchanged. A `once` listener that never fires stays on
  the emitter, so the leak is the same.
- `ForkedProcessTaskRunner.cleanup()` is synchronous on this branch, so
  the `await` in `dispose()` is a formality. The comment says the
  children are signalled first rather than reaped first.
- `packages/nx` runs on jest here, not vitest, so the three specs use
  `jest.*`. `task-orchestrator.spec.ts` does not exist on 22.7.x and is
  added with only the tests this change introduces.

(cherry picked from commit 0ae9069)

Co-authored-by: FrozenPandaz <jasonjean1993@gmail.com>
Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com>
@AgentEnder
AgentEnder requested a review from a team as a code owner September 9, 2026 19:20
@AgentEnder
AgentEnder requested a review from MaxKless September 9, 2026 19:20
@nx-cloud

nx-cloud Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit bd035b2

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 1h 8m 1s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 3s View ↗
nx-cloud record -- pnpm nx-cloud conformance:check ✅ Succeeded 16s View ↗
nx build workspace-plugin ✅ Succeeded <1s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded 23s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 7s View ↗

☁️ Nx Cloud last updated this comment at 2026-09-09 20:32:31 UTC

@FrozenPandaz
FrozenPandaz merged commit 45202b1 into 22.7.x Sep 9, 2026
17 checks passed
@FrozenPandaz
FrozenPandaz deleted the backport/36866-22.7.x branch September 9, 2026 20:39
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants