aglinxinyuan opened a new pull request, #9198:
URL: https://github.com/apache/texera/pull/9198

   ### What changes were proposed in this PR?
   
   This fixes the random unit-test timeouts on the macOS frontend leg (#9197) 
at their cause: memory that grows across spec files inside each Vitest worker.
   
   The Angular unit-test builder runs Vitest with `isolate: false`, so each 
worker runs its spec files one after another against one jsdom document and one 
copy of Vitest. Several things outside any one test kept every finished test's 
TestBed graph (component tree, root injector, NgZone, services) reachable, and 
any one of them was enough on its own. On the 7 GB macOS runner, with 2 workers 
of about 120 files each, that added up to about 2.4 GB per worker. The VM then 
swapped and stalled long enough to time out whatever test was running.
   
   **Root services now release what they start** (production code):
   - `WorkflowActionService.ngOnDestroy()` destroys the shared model.
   - `SharedModel.destroy()` calls `wsProvider.destroy()`. Before, it only 
called `disconnect()`, and only when the provider was connected. Now the 
provider's check interval, BroadcastChannel subscription and doc/awareness 
listeners are released too, and a provider that never connected no longer 
retries forever.
   - `WorkflowWebsocketService.ngOnDestroy()` stops the heartbeat `interval()` 
and closes the socket.
   - `JupyterPanelService.ngOnDestroy()` removes its `window` `message` 
listener.
   
   **jsdom's selector cache is emptied between tests.** nwsapi memoizes every 
selector together with the nodes it matched, for the life of the document, and 
that includes the fresh `#root<N>` query TestBed makes for each fixture. The 
new setup file `src/jsdom-selector-cache-setup.ts` handles this; it is 
registered in `vitest.config.ts` and implemented in 
`src/app/common/testing/jsdom-selector-cache.ts`.
   
   **Vitest 4's mock registry no longer pins every mock.** `@vitest/spy` keeps 
every `vi.fn()` / `vi.spyOn()` in a strong `Set` that is never pruned, together 
with what each one recorded or spied on. A Yarn patch 
(`frontend/.yarn/patches/@vitest-spy-npm-4.1.11-*.patch`) backports Vitest 5's 
WeakRef-based registry. Angular 21's builder still requires Vitest 4, so 
upgrading isn't an option yet. Supporting changes:
   - The patch carries Vitest's MIT code, so it is listed in the root 
`LICENSE`, with the license text in `licenses-3rd-party-code/vitest.txt`.
   - The `resolutions` key pins the exact version, so every Vitest bump drops 
the patch. A new Renovate rule keeps Vitest bumps in their own PR, so 
regenerating the patch doesn't hold up the grouped frontend updates.
   
   **Guard and docs:**
   - `src/app/common/testing/worker-memory.spec.ts` forces a GC and fails if 
either the patch or the cache reset stops releasing memory.
   - `frontend/TESTING.md` gains a "Memory in shared test workers" section. It 
covers the rule for root services, why the setup file and the patch exist, and 
how to carry the patch over a Vitest bump.
   
   Before and after, measured locally in the macOS CI shape: 2 workers, 
coverage on, cold caches, full suite, with process memory sampled.
   
   | | main | this PR |
   | --- | --- | --- |
   | Peak physical footprint per worker | 2.9–3.1 GB | 1.5–1.9 GB |
   | Peak footprint of the whole process tree | 8.7–9.0 GB | 5.6–6.2 GB |
   | Vitest `Duration` | 85–115 s | 65–66 s |
   
   On CI the macOS leg's Vitest run currently takes 250–950 s, against 100–150 
s on ubuntu for the same suite.
   
   Notes for review:
   - **Why not `isolate: true`?** It also bounds memory (4.9 GB tree peak), but 
it roughly doubled the Vitest run locally (230 s against about 100 s), on every 
leg.
   - **Every piece is needed.** Leaving out the spy patch put the tree peak 
back at 7.4 GB. Clearing or resetting all mocks at the end of each file instead 
of patching only reached 6.8 / 7.0 GB, because a `vi.spyOn` keeps the object it 
spied on through its restore closure.
   - **Left out on purpose.** I also tried a per-test event-loop yield, which 
trims peaks within a file further. It exposes a jsdom `requestAnimationFrame` / 
fake-timer trap: after a fake-timer test, animation frames stop firing for the 
rest of the worker. So it is not in this PR.
   - **Tracked separately.** `WorkflowCompilingService` builds a throwaway 
`WorkflowGraph` on every compile, and that graph opens a y-websocket connection 
that is never closed. It is a real but separate production leak, with little 
memory impact on the tests, so it gets its own PR.
   
   ### Any related issues, documentation, discussions?
   
   Closes #9197.
   
   Related:
   - #7713 / #7717 raised the test timeouts.
   - #7975 / #7976 sized the worker heap.
   
   Both treated the symptom. This fixes the growth behind it.
   
   ### How was this PR tested?
   
   - `yarn --cwd frontend run test:ci`: 244 test files passed, 6857 tests 
passed, 1 skipped.
   - The full suite in the macOS shape, twice: 2 workers through a runner 
config setting `maxWorkers: 2`, coverage on, cold caches, process memory 
sampled. Both runs passed all 244 files; the numbers are in the table above.
   - Each new regression test fails against the old code and passes with the 
fix:
     - `SharedModel`: "destroys the provider, not just disconnects it" and 
"stops a provider that never connected from retrying"
     - `WorkflowActionService`: "destroys the shared model when its injector is 
destroyed"
     - `WorkflowWebsocketService`: "stops the heartbeat and closes the socket 
when destroyed"
     - `JupyterPanelService`: "stops listening for notebook messages when its 
injector is destroyed"
     - `worker-memory.spec.ts`: the mock-registry test fails without the Yarn 
patch.
   - `yarn install --immutable` passes with the new resolution and lock entry. 
`prettier-eslint --list-different` and `eslint` are clean on the changed files.
   - Not run locally on Node 24: this machine has Node 20, and CI's 24.19.0 
legs will be the first Node 24 run of the GC-based guard spec.
   
   ### Was this PR authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to