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
59 changes: 59 additions & 0 deletions docs/validation/terminal-indexeddb-navigation-20260921.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Terminal IndexedDB navigation persistence

Tracking: #878, parent #269.

## Reproduction

The exact unchanged Code OSS Release package built from AppScene
`cd0a02ecd15f0a635bccc242cdd657888db39e25` and WebScene
`a7943723d7b33dd50de2a4ef323cde4dd8500905` reaches the first browser task
after `beforeunload` and `pagehide`, then exposes reload to AppScene while an
IndexedDB transaction descended from that task is still committing. The next
realm retains the encrypted MCP key but has no workspace `mcpInputs` record.

The absence of the earlier IndexedDB revision-conflict diagnostic confirms the
serialized commit fix in #875 is active. Lifecycle counters confirm #874 and
#877 are active. The remaining boundary is the descendant transaction and its
native asynchronous load/store completion.

## Runtime contract

Non-repeating zero-delay tasks scheduled by terminal lifecycle listeners,
their microtasks, or another tagged task inherit a terminal-persistence tag.
Native IndexedDB work started in that lineage inherits the tag through storage
completion and Promise continuations. WebScene keeps the reserved terminal
reload/navigation request private until tagged timers and IndexedDB operations
drain.

Intervals, animation frames, delayed timers, pre-existing work, and unrelated
IndexedDB operations do not hold navigation. A 1,024-task lineage budget
prevents a self-refilling zero-delay chain from starving the host handoff.
Veto, error, shutdown, and successful publication clear the remaining budget
and preserve the existing bounded host-request ownership.

## Focused gate

`webscene_native_terminal_indexeddb_navigation` creates a real durable
IndexedDB partition, starts a write from a lifecycle zero-delay task, includes
a Promise and nested timer after native commit, and requests reload. It asserts
that the native reload is unavailable until the transaction and continuation
finish, restarts with a different loopback port, and reads the durable value.
It also includes unrelated interval and delayed timers and a two-second bound.

```sh
cmake --build /private/tmp/webscene-sdk-build-f5df884a \
--target webscene_native_engine_tests -j 3
WEBSCENE_NATIVE_ENGINE_TEST_FILTER=terminal-indexeddb-navigation \
/private/tmp/webscene-sdk-build-f5df884a/webscene_native_engine_tests
```

The next product gate rebuilds the exact AppScene/WebScene SDK and unchanged
Code OSS Release package, then repeats the two-realm MCP persistence smoke.

## Result

The focused macOS arm64 Release gate passed. Durable commit-to-handoff took
0.0036 seconds. The related 1,000-request navigation gate also passed in
0.0044 seconds. The self-refilling zero-delay fixture exhausted the 1,024-task
lineage budget and still exposed reload while leaving its later untagged work
outside the persistence boundary.
6 changes: 6 additions & 0 deletions experiments/WebScene.NativeEngine.Probe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,8 @@ if(WEBSCENE_NATIVE_ENGINE_ENABLE_V8)
add_test(NAME webscene_native_recursive_selector_cache
COMMAND webscene_native_engine_tests)
add_test(NAME webscene_native_indexeddb_contract COMMAND webscene_native_engine_tests)
add_test(NAME webscene_native_terminal_indexeddb_navigation
COMMAND webscene_native_engine_tests)
add_test(NAME webscene_native_panel_listener_lifecycle COMMAND webscene_native_engine_tests)
add_test(NAME webscene_native_navigation_realm COMMAND webscene_native_engine_tests)
add_test(NAME webscene_native_query_iframe_worker_bootstrap
Expand Down Expand Up @@ -1222,6 +1224,10 @@ if(WEBSCENE_NATIVE_ENGINE_ENABLE_V8)
set_tests_properties(webscene_native_indexeddb_contract PROPERTIES
ENVIRONMENT "WEBSCENE_NATIVE_ENGINE_TEST_FILTER=indexeddb"
LABELS "storage;indexeddb;durability" TIMEOUT 60)
set_tests_properties(webscene_native_terminal_indexeddb_navigation PROPERTIES
ENVIRONMENT "WEBSCENE_NATIVE_ENGINE_TEST_FILTER=terminal-indexeddb-navigation"
LABELS "native;runtime;navigation;storage;indexeddb;durability;performance"
TIMEOUT 30)
set_tests_properties(webscene_native_panel_listener_lifecycle PROPERTIES
ENVIRONMENT "WEBSCENE_NATIVE_ENGINE_TEST_FILTER=panel-subtree-reclamation"
LABELS "native;runtime;dom;lifecycle" TIMEOUT 30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7026,7 +7026,7 @@ struct v8_dom_runtime::implementation final {
std::lock_guard lock(host_request_mutex);
const auto count = pending_terminal_host_requests.size();
pending_terminal_host_requests.clear();
terminal_host_request_timer_cutoff.reset();
terminal_handoff_task_budget = 0U;
reserved_host_request_count = count > reserved_host_request_count
? 0U : reserved_host_request_count - count;
}
Expand All @@ -7037,14 +7037,6 @@ struct v8_dom_runtime::implementation final {
if (!request) return false;
std::lock_guard lock(host_request_mutex);
if (reserved_host_request_count == 0U) return false;
if (pending_terminal_host_requests.empty()) {
// Every zero-delay timer scheduled by beforeunload/pagehide or by
// their microtasks has an earlier deadline. Timers created by one
// of those tasks receive a later deadline and cannot indefinitely
// postpone a terminal navigation.
terminal_host_request_timer_cutoff =
std::chrono::steady_clock::now();
}
pending_terminal_host_requests.push_back(std::move(request));
return true;
}
Expand All @@ -7057,6 +7049,7 @@ struct v8_dom_runtime::implementation final {
// coalesced into the outer handoff.
if (window_close_lifecycle_dispatching) return true;
if (!reserve_typed_host_request_capacity()) return false;
terminal_handoff_task_budget = maximum_terminal_handoff_tasks;
struct lifecycle_dispatch_guard final {
bool& dispatching;
explicit lifecycle_dispatch_guard(bool& value) : dispatching(value)
Expand All @@ -7068,22 +7061,28 @@ struct v8_dom_runtime::implementation final {
const auto disposition =
request_window_close_in_current_realm(local_context);
if (disposition != WEBSCENE_WINDOW_CLOSE_ALLOW_V1) {
terminal_handoff_task_budget = 0U;
release_typed_host_request_capacity();
return disposition == WEBSCENE_WINDOW_CLOSE_VETO_V1;
}
if (defer_reserved_terminal_host_request(std::move(request))) return true;
terminal_handoff_task_budget = 0U;
release_typed_host_request_capacity();
return false;
}

bool has_lifecycle_timer_before_terminal_handoff() const noexcept
bool has_terminal_handoff_persistence_work() const noexcept
{
if (pending_terminal_host_requests.empty()
|| !terminal_host_request_timer_cutoff.has_value()) return false;
const auto cutoff = *terminal_host_request_timer_cutoff;
return std::any_of(timers.begin(), timers.end(), [&](const auto& timer) {
return !timer.animation_frame && timer.deadline <= cutoff;
});
if (pending_terminal_host_requests.empty()) return false;
if (std::any_of(timers.begin(), timers.end(), [](const auto& timer) {
return timer.terminal_handoff_critical;
})) return true;
return std::any_of(
pending_indexeddb_promises.begin(),
pending_indexeddb_promises.end(),
[](const auto& entry) {
return entry.second.terminal_handoff_critical;
});
}

bool drain_terminal_host_request_task()
Expand All @@ -7092,15 +7091,15 @@ struct v8_dom_runtime::implementation final {
{
std::lock_guard lock(host_request_mutex);
if (pending_terminal_host_requests.empty()) {
terminal_host_request_timer_cutoff.reset();
terminal_handoff_task_budget = 0U;
return true;
}
while (!pending_terminal_host_requests.empty()) {
if (reserved_host_request_count == 0U) {
last_error =
"Deferred terminal navigation lost its host-request reservation";
pending_terminal_host_requests.clear();
terminal_host_request_timer_cutoff.reset();
terminal_handoff_task_budget = 0U;
return false;
}
--reserved_host_request_count;
Expand All @@ -7109,7 +7108,7 @@ struct v8_dom_runtime::implementation final {
pending_terminal_host_requests.pop_front();
notify = true;
}
terminal_host_request_timer_cutoff.reset();
terminal_handoff_task_budget = 0U;
}
if (notify && host_request_available) host_request_available();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
v8::Global<v8::Context>(info.GetIsolate(), info.GetIsolate()->GetCurrentContext()),
v8::Global<v8::Function>(info.GetIsolate(), info[0].As<v8::Function>()),
{},
-1});
-1,
false});
#if defined(WEBSCENE_NATIVE_ENGINE_WITH_V8_INSPECTOR)
self->inspector_async_task_scheduled(
"requestAnimationFrame",
Expand Down Expand Up @@ -89,6 +90,11 @@
for (int index = 2; index < info.Length(); ++index) {
arguments.emplace_back(info.GetIsolate(), info[index]);
}
const auto terminal_handoff_critical = !repeating && delay <= 0
&& self->terminal_handoff_task_budget != 0U
&& (self->window_close_lifecycle_dispatching
|| self->terminal_handoff_task_active);
if (terminal_handoff_critical) --self->terminal_handoff_task_budget;
self->timers.push_back(timer_task{
id,
false,
Expand All @@ -99,7 +105,8 @@
v8::Global<v8::Context>(info.GetIsolate(), info.GetIsolate()->GetCurrentContext()),
v8::Global<v8::Function>(info.GetIsolate(), info[0].As<v8::Function>()),
std::move(arguments),
-1});
-1,
terminal_handoff_critical});
#if defined(WEBSCENE_NATIVE_ENGINE_WITH_V8_INSPECTOR)
self->inspector_async_task_scheduled(
repeating ? "setInterval" : "setTimeout",
Expand Down Expand Up @@ -148,7 +155,8 @@
v8::Global<v8::Function>(
info.GetIsolate(), info[0].As<v8::Function>()),
{},
-1});
-1,
false});
#if defined(WEBSCENE_NATIVE_ENGINE_WITH_V8_INSPECTOR)
self->inspector_async_task_scheduled("FileReader", id, false);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@
if (found == pending_indexeddb_promises.end()) return;
auto pending = std::move(found->second);
pending_indexeddb_promises.erase(found);
terminal_handoff_task_scope terminal_scope(
terminal_handoff_task_active,
pending.terminal_handoff_critical);
auto realm = pending.context.Get(isolate);
auto resolver = pending.resolver.Get(isolate);
if (realm.IsEmpty() || resolver.IsEmpty()) return;
Expand Down Expand Up @@ -240,6 +243,9 @@
if (found == pending_indexeddb_promises.end()) return;
auto pending = std::move(found->second);
pending_indexeddb_promises.erase(found);
terminal_handoff_task_scope terminal_scope(
terminal_handoff_task_active,
pending.terminal_handoff_critical);
auto realm = pending.context.Get(isolate);
auto resolver = pending.resolver.Get(isolate);
if (realm.IsEmpty() || resolver.IsEmpty()) return;
Expand Down Expand Up @@ -549,6 +555,9 @@
pending.context.Reset(info.GetIsolate(), realm);
pending.resolver.Reset(info.GetIsolate(), resolver);
pending.deserialize_data = operation == "load";
pending.terminal_handoff_critical =
self->window_close_lifecycle_dispatching
|| self->terminal_handoff_task_active;
pending.origin = origin;
pending.database = database;
if (operation == "load") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,14 @@
std::deque<std::string> host_requests;
std::deque<std::unique_ptr<native_host_request>> typed_host_requests;
// Top-level reload/navigation is published from a later browser task so
// lifecycle listeners can finish zero-delay persistence work before the
// host retires this realm. Entries consume reserved host-request capacity
// while they wait, keeping the combined queue bounded.
// lifecycle-descended persistence work can finish before the host retires
// this realm. Entries consume reserved host-request capacity while they
// wait, keeping the combined queue bounded.
std::deque<std::unique_ptr<native_host_request>>
pending_terminal_host_requests;
std::optional<std::chrono::steady_clock::time_point>
terminal_host_request_timer_cutoff;
static constexpr size_t maximum_terminal_handoff_tasks = 1024U;
size_t terminal_handoff_task_budget{};
bool terminal_handoff_task_active{};
bool primary_selection_publication_dirty{};
std::deque<std::unique_ptr<native_download_request>> download_requests;
std::deque<std::unique_ptr<native_outbound_drag_request>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@
v8::Global<v8::Function> callback;
std::vector<v8::Global<v8::Value>> arguments;
double animation_timestamp_ms{-1};
bool terminal_handoff_critical{false};
};

struct terminal_handoff_task_scope final {
bool& active;
bool previous;

terminal_handoff_task_scope(bool& target, bool inherit)
: active(target), previous(target)
{
active = active || inherit;
}

~terminal_handoff_task_scope() { active = previous; }
};

struct message_port_endpoint;
Expand Down Expand Up @@ -361,6 +375,7 @@
std::string failure_message;
std::string failure_name;
bool cleanup_after_failure{};
bool terminal_handoff_critical{};
};

struct indexeddb_durable_request_target final {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
}
activate_css_cascade(task_context);
v8::Context::Scope task_context_scope(task_context);
terminal_handoff_task_scope terminal_scope(
terminal_handoff_task_active,
task.terminal_handoff_critical);
auto callback = task.callback.Get(isolate);
if (callback.IsEmpty()) {
#if defined(WEBSCENE_NATIVE_ENGINE_WITH_V8_INSPECTOR)
Expand Down Expand Up @@ -828,10 +831,10 @@
// queue; otherwise hot application code remains on its initial tier.
pump_v8_platform_tasks();
// A top-level navigation is a browser task, not a synchronous tail of
// beforeunload/pagehide. Preserve timer-source order for zero-delay
// lifecycle work, then expose every bounded terminal request together.
// beforeunload/pagehide. Preserve lifecycle-descended zero-delay and
// IndexedDB persistence work, then expose every terminal request.
if (!pending_terminal_host_requests.empty()
&& !has_lifecycle_timer_before_terminal_handoff()) {
&& !has_terminal_handoff_persistence_work()) {
return drain_terminal_host_request_task();
}
const auto timer_due = has_due_timer();
Expand Down
Loading
Loading