From a6a62e626db38ba67857f51f81c55c930d57814a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Mon, 21 Sep 2026 01:59:33 +0200 Subject: [PATCH] Retain terminal storage flush after lifecycle disposal --- .../terminal-cancelled-storage-flush-884.md | 71 +++++++++++++++++++ .../webscene_v8_runtime_browser_apis.inc | 14 +++- .../native_v8_runtime_indexeddb_tests.inc | 13 +++- 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 docs/validation/terminal-cancelled-storage-flush-884.md diff --git a/docs/validation/terminal-cancelled-storage-flush-884.md b/docs/validation/terminal-cancelled-storage-flush-884.md new file mode 100644 index 00000000..5f17c73e --- /dev/null +++ b/docs/validation/terminal-cancelled-storage-flush-884.md @@ -0,0 +1,71 @@ +# Terminal cancelled storage flush handoff + +Tracking: #884, parent #269, predecessor #882 / PR #883. + +## Exact product evidence + +The unchanged Code OSS 1.137 package built from AppScene +`cd0a02ecd15f0a635bccc242cdd657888db39e25` and WebScene +`2520661b599b8f6f0b9996967bbee816451c0e85` still lost its workspace +`mcpInputs` record across reload after IndexedDB-owned work promotion landed: + +```text +mcpStoredInputKeysBeforeReload = ["${input:sceneSecret}"] +mcpProfileInputKeysBeforeReload = [] +mcpStoredInputKeys = [] +mcpStoredInputPresent = false +``` + +The integration artifact is +`artifacts/smoke-cd0a02e-2520661-mcp-fixed-1`. Its executable SHA-256 is +`1f729289f7f71727bbee538f08c2491dcce7289dc2262bc083eb124b36a7e4e4`. +The same exact sources pass when the validation overlay explicitly awaits +upstream `IStorageService.flush(SHUTDOWN)` before reload. That isolates the +remaining failure to lifecycle scheduling rather than IndexedDB durability. + +## Root cause + +During `beforeunload`, VS Code emits its final storage state and schedules a +zero-delay flush. Its browser workbench then synchronously publishes shutdown +and disposes the storage `Delayer`; disposal calls `clearTimeout()` before the +lifecycle event returns. WebScene consequently removed the only task that +could start the IndexedDB write and observed no terminal persistence work, so +`pagehide` and reload handoff proceeded immediately. + +A focused native fixture reproduces the exact order. Before the fix it reports: + +```text +order=[beforeunload, preexisting-commit, pagehide] +committed=false +continuation=false +``` + +## Runtime contract + +A zero-delay, one-shot timer created during an accepted terminal lifecycle is +already charged to WebScene's bounded terminal handoff budget. If synchronous +shutdown disposal clears that handle during the same lifecycle dispatch, +WebScene retains the accepted callback privately until the handoff boundary. +Its microtasks, zero-delay descendants, and IndexedDB operations inherit the +same bounded terminal ancestry. + +This terminal-only durability rule is intentionally narrower than ordinary +timer cancellation. Timers created outside terminal lifecycle, delayed timers, +intervals, animation frames, and work beyond the 1,024-task budget keep their +existing behavior. The focused fixture explicitly verifies that an ordinary +zero-delay `clearTimeout()` still cancels. + +## Focused gate + +```sh +cmake --build /private/tmp/webscene-882-build \ + --target webscene_native_engine_tests -j 6 +WEBSCENE_NATIVE_ENGINE_TEST_FILTER=terminal-indexeddb-navigation \ + /private/tmp/webscene-882-build/webscene_native_engine_tests +``` + +Result on macOS arm64 Release: pass. The durable transaction-to-handoff gate +completed in 0.0036 seconds. The fixture also covers restart durability, +pre-existing IndexedDB work, exact lifecycle order, ordinary cancellation, +future timers, intervals, native completion descendants, and the bounded +self-refilling-task ceiling. diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_browser_apis.inc b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_browser_apis.inc index 87239d22..f5cadc14 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_browser_apis.inc +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_browser_apis.inc @@ -180,9 +180,21 @@ #if defined(WEBSCENE_NATIVE_ENGINE_WITH_V8_INSPECTOR) [self, id, &cancelled_animation_frames](const auto& timer) { #else - [id, &cancelled_animation_frames](const auto& timer) { + [self, id, &cancelled_animation_frames](const auto& timer) { #endif if (timer.id != id) return false; + // Browser workbenches can synchronously dispose their timer + // scheduler after accepting a terminal storage flush from a + // beforeunload listener. The public handle is then canceled, + // but the native handoff still owes the already accepted, + // bounded persistence callback before pagehide tears storage + // down. Only zero-delay one-shot timers created by that active + // terminal lifecycle carry this flag; ordinary cancellation, + // future timers, intervals, and animation frames are unchanged. + if (timer.terminal_handoff_critical + && self->window_close_lifecycle_dispatching) { + return false; + } if (timer.animation_frame) ++cancelled_animation_frames; #if defined(WEBSCENE_NATIVE_ENGINE_WITH_V8_INSPECTOR) self->inspector_async_task_canceled(id); diff --git a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_indexeddb_tests.inc b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_indexeddb_tests.inc index e2536b78..5577c147 100644 --- a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_indexeddb_tests.inc +++ b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_indexeddb_tests.inc @@ -640,7 +640,7 @@ void test_terminal_navigation_waits_for_indexeddb_commit() execute_and_wait(engine, R"JS( globalThis.terminalIndexedDb = { ready: false, committed: false, continuation: false, error: null, - order: [] + ordinaryCancelFired: false, order: [] }; (async () => { const database = await new Promise((resolve, reject) => { @@ -656,7 +656,10 @@ void test_terminal_navigation_waits_for_indexeddb_commit() // An unrelated interval and future timer must not hold navigation. setInterval(() => {}, 60000); setTimeout(() => {}, 60000); - // Browser storage commonly enters IndexedDB from a lifecycle task. + // VS Code schedules its terminal storage flush, then synchronously + // disposes the Delayer during the same beforeunload dispatch. The + // bounded terminal handoff must finish that accepted persistence + // callback even though disposal clears its public timer handle. pendingFlushTimer = setTimeout(() => { terminalIndexedDb.order.push('flush-timer'); const transaction = database.transaction('state', 'readwrite'); @@ -675,6 +678,7 @@ void test_terminal_navigation_waits_for_indexeddb_commit() }, 0)); }; }, 0); + clearTimeout(pendingFlushTimer); }); addEventListener('pagehide', () => { terminalIndexedDb.order.push('pagehide'); @@ -693,6 +697,10 @@ void test_terminal_navigation_waits_for_indexeddb_commit() const auto started = std::chrono::steady_clock::now(); require(evaluate(engine, R"JS( + const ordinaryTimer = setTimeout(() => { + terminalIndexedDb.ordinaryCancelFired = true; + }, 0); + clearTimeout(ordinaryTimer); globalThis.terminalIndexedDbPendingTransaction = terminalIndexedDbDatabase.transaction('state', 'readwrite'); terminalIndexedDbPendingTransaction.objectStore('state') @@ -722,6 +730,7 @@ void test_terminal_navigation_waits_for_indexeddb_commit() "terminalIndexedDb.ready===true" "&&terminalIndexedDb.committed===true" "&&terminalIndexedDb.continuation===true" + "&&!terminalIndexedDb.ordinaryCancelFired" "&&terminalIndexedDb.order[0]==='beforeunload'" "&&terminalIndexedDb.order.indexOf('preexisting-commit')>0" "&&terminalIndexedDb.order.indexOf('flush-timer')>0"