Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions docs/validation/terminal-cancelled-storage-flush-884.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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');
Expand All @@ -675,6 +678,7 @@ void test_terminal_navigation_waits_for_indexeddb_commit()
}, 0));
};
}, 0);
clearTimeout(pendingFlushTimer);
});
addEventListener('pagehide', () => {
terminalIndexedDb.order.push('pagehide');
Expand All @@ -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')
Expand Down Expand Up @@ -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"
Expand Down
Loading