Backtestsvuerun-20261021-133314#15113
SSR memory leak - options API watchers are created and never stopped with async setup()
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The runtime change is byte-for-byte the same in both patches: wrap the deferred handleSetupResult call in setInSSRSetupState(true)/finally setInSSRSetupState(false), so applyOptions runs with isInSSRComponentSetup set and the options-API watch/computed paths take the SSR branch (non-immediate, non-sync watchers become no-ops; sync ones register with ctx.__watcherHandles). This is the right root-cause fix in both, and all five independent tests go from FAIL on base to PASS on both. The differences are in craft and tests. Patch B adds a comment explaining why the state has to be restored (setupComponent already reset it before the promise resolved), which is exactly the non-obvious thing a future reader needs. Patch B's tests also cover more of the issue's stated expectation ('the same behavior as with a synchronous setup'): the plain repro, an immediate watcher that fires once and never again, and a flush:'sync' watcher that is registered in __watcherHandles and stopped after render. Patch A adds the repro plus a WeakRef/gc leak test; that targets the headline leak directly and matches the existing gc describe block's convention, but it is skipped unless the runner exposes gc, so it rarely executes. Neither adds unrelated churn. B wins on explanatory comment and broader, always-run behavioral coverage; the margin is modest since the fix itself is identical.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
5m 14s
Wall-clock time in the container
Full test suite
No new failures
1,739 passed · 1 skipped · 1,740 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
97vs 76 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.5.40
Steps to reproduce
originally reported in https://github.com/nuxt/nuxt/issues/30782
- create an options API component with an
async setup(), acomputedproperty that reads shared module-level state, and awatchon that - render it on the server and then mutate the shared state after the render is done
- see that the watcher is still active on the server
Reproduction
test('options-api watchers should not be created for async setup components', async () => {
const open = ref(true)
const spy = vi.fn()
const App = defineComponent({
setup: () => Promise.resolve({}),
computed: {
isOpen() {
return open.value
},
},
watch: {
isOpen: spy,
},
render() {
return h('div', String(this.isOpen))
},
})
const html = await renderToString(createSSRApp(App))
expect(html).toBe('<div>true</div>')
// mutating the state after the request is done should not trigger the watcher
open.value = false
await nextTick()
expect(spy).not.toHaveBeenCalled()
})
What is expected?
the same behavior as with a synchronous setup: regular watchers declared via the watch option don't run during SSR and don't survive the render
mutating the shared state after renderToString resolves should not trigger anything
What is actually happening?
when setup() returns a Promise, the watch option creates a live watcher on the server that is never stopped
if this subscribes to state that outlives the request, it keeps the entire component instance (and app) reachable forever
System Info
Any additional comments?
_No response_
The two patches
packages/runtime-core/src/component.tsindex c46741bee..38fde993e 100644@@ -894,7 +894,15 @@ function setupStatefulComponent( // return the promise so server-renderer can wait on it return setupResult .then((resolvedResult: unknown) => {- handleSetupResult(instance, resolvedResult, isSSR)+ // setupComponent() has already reset the SSR setup state by the+ // time the promise resolves, restore it so that options API+ // watchers / computed / hooks are applied in SSR mode+ setInSSRSetupState(true)+ try {+ handleSetupResult(instance, resolvedResult, isSSR)+ } finally {+ setInSSRSetupState(false)+ } }) .catch(e => { handleError(e, instance, ErrorCodes.SETUP_FUNCTION)packages/server-renderer/__tests__/ssrWatch.spec.tsindex 2b1ad98d2..be358a225 100644@@ -212,6 +212,93 @@ describe('ssr: watch', () => { expect(beforeAwaitTriggered).toBe(false) expect(afterAwaitTriggered).toBe(false) })++ test('options-api watchers should not be created for async setup components', async () => {+ const open = ref(true)+ const spy = vi.fn()++ const App = defineComponent({+ setup: () => Promise.resolve({}),+ computed: {+ isOpen() {+ return open.value+ },+ },+ watch: {+ isOpen: spy,+ },+ render() {+ return h('div', String(this.isOpen))+ },+ })++ const html = await renderToString(createSSRApp(App))+ expect(html).toBe('<div>true</div>')++ // mutating the state after the request is done should not trigger the watcher+ open.value = false+ await nextTick()+ expect(spy).not.toHaveBeenCalled()+ })++ test('options-api immediate watchers should be stopped for async setup components', async () => {+ const open = ref(true)+ const spy = vi.fn()++ const App = defineComponent({+ setup: () => Promise.resolve({}),+ watch: {+ open: {+ handler: spy,+ immediate: true,+ },+ },+ data: () => ({ open }),+ render() {+ return h('div', String(this.open))+ },+ })++ const html = await renderToString(createSSRApp(App))+ expect(html).toBe('<div>true</div>')+ expect(spy).toHaveBeenCalledTimes(1)++ open.value = false+ await nextTick()+ expect(spy).toHaveBeenCalledTimes(1)+ })++ test('options-api sync watchers should be stopped after render for async setup components', async () => {+ const open = ref(true)+ const spy = vi.fn()++ const App = defineComponent({+ setup: () => Promise.resolve({}),+ computed: {+ isOpen() {+ return open.value+ },+ },+ watch: {+ isOpen: {+ handler: spy,+ flush: 'sync',+ },+ },+ render() {+ return h('div', String(this.isOpen))+ },+ })++ const ctx: SSRContext = {}+ const html = await renderToString(createSSRApp(App), ctx)+ expect(html).toBe('<div>true</div>')+ expect(ctx.__watcherHandles!.length).toBe(1)++ open.value = false+ await nextTick()+ expect(spy).not.toHaveBeenCalled()+ }) }) describe('ssr: watchEffect', () => {
packages/runtime-core/src/component.tsindex c46741bee..162a6ad44 100644@@ -894,7 +894,12 @@ function setupStatefulComponent( // return the promise so server-renderer can wait on it return setupResult .then((resolvedResult: unknown) => {- handleSetupResult(instance, resolvedResult, isSSR)+ setInSSRSetupState(true)+ try {+ handleSetupResult(instance, resolvedResult, isSSR)+ } finally {+ setInSSRSetupState(false)+ } }) .catch(e => { handleError(e, instance, ErrorCodes.SETUP_FUNCTION)packages/server-renderer/__tests__/ssrWatch.spec.tsindex 2b1ad98d2..2dad83cd2 100644@@ -130,6 +130,35 @@ describe('ssr: watch', () => { expect(msg).toBe('start') }) + // #15113+ test('options-api watchers should not be created for async setup components', async () => {+ const open = ref(true)+ const spy = vi.fn()++ const App = defineComponent({+ setup: () => Promise.resolve({}),+ computed: {+ isOpen() {+ return open.value+ },+ },+ watch: {+ isOpen: spy,+ },+ render() {+ return h('div', String(this.isOpen))+ },+ })++ const html = await renderToString(createSSRApp(App))+ expect(html).toBe('<div>true</div>')++ // mutating the state after the request is done should not trigger the watcher+ open.value = false+ await nextTick()+ expect(spy).not.toHaveBeenCalled()+ })+ test('should not run non-immediate watchers registered after async context restore', async () => { const text = ref('start') let beforeAwaitTriggered = false@@ -351,4 +380,44 @@ describe.skipIf(!global.gc)('ssr: watch gc', () => { expect(weakRefs.filter(ref => ref.deref()).length).toBe(0) })++ // #15113+ test('options-api should not retain apps with async setup + watcher on global state', async () => {+ const weakRefs: { deref(): unknown | undefined }[] = []++ const open = ref(true)++ const App = defineComponent({+ setup: () => Promise.resolve({}),+ computed: {+ isOpen() {+ return open.value+ },+ },+ watch: {+ isOpen() {},+ },+ render() {+ return h('div', String(this.isOpen))+ },+ })++ async function renderOnce() {+ const app = createSSRApp(App)+ // @ts-expect-error ES2021 API+ weakRefs.push(new WeakRef(app))+ const html = await renderToString(app)+ expect(html).toBe('<div>true</div>')+ }++ for (let i = 0; i < 10; i++) {+ await renderOnce()+ }++ for (let i = 0; i < 5; i++) {+ await gc()+ }++ expect(weakRefs.filter(ref => ref.deref()).length).toBe(0)+ }) })