fix(navigation): release superseded adopted parser continuations

An adopted native replacement can be superseded before protocol installation.
Stale completion used to leave its response-gated parser held on the live Page,
so the next beforeunload never ran. Release that continuation only for the
original Page residence and adopted Document agent.

Add deterministic beforeunload and newer-Document retention tests.
This commit is contained in:
ldm0
2026-11-20 22:48:00 +08:00
parent a7d0ba92b2
commit 90efd78ed2
2 changed files with 230 additions and 0 deletions
@@ -1486,3 +1486,216 @@ async fn verify_unprojected_request_policy(replace_existing: bool, policy: LateR
}
}
}
#[tokio::test]
async fn stale_replacement_completion_keeps_newer_document_parser_handoff() {
tokio::task::LocalSet::new()
.run_until(async {
let CommitFixture {
ctx,
token,
navigation,
prepared,
..
} = commit_fixture(true).await;
let commit = crate::conn::PreparedDocumentCommitState::new();
let mut driver = crate::conn::ProtocolWorkflowDriver::new(ctx.handle());
let loaded = commit
.commit_using(&mut driver, token.clone(), &navigation, prepared)
.await
.unwrap();
let previous_agent = ctx.with_fixture(|conn| {
conn.loaded_page_mut_for_owner(&navigation.owner)
.unwrap()
.renderer_devtools_agent_token()
});
let stale_completion = commit.complete(token, navigation.clone(), Ok(loaded));
let successor = ctx.with_fixture(|conn| {
conn.start_document_navigation_for_owner(&navigation.owner, "LID-newer".into())
.unwrap()
});
let mut newer = navigation.clone();
newer.loader_id = successor.loader_id.clone();
newer.requested_url =
Url::parse("data:text/html,<!doctype html><title>newer-document</title>").unwrap();
let job = ctx.with_fixture(|conn| {
conn.background_navigation_load_job_for_navigation(
&successor,
&newer,
Default::default(),
None,
)
.unwrap()
});
let (prepared, _) = job.run(None).await;
let NavigationLoadOutcome::ResponseCommitReady(prepared) = prepared.unwrap() else {
panic!("newer background response must retain its DocumentCommit boundary");
};
let next_commit = crate::conn::PreparedDocumentCommitState::new();
let loaded = tokio::time::timeout(
std::time::Duration::from_secs(10),
next_commit.commit_using(&mut driver, successor.clone(), &newer, *prepared),
)
.await
.unwrap()
.unwrap();
assert!(
loaded
.page_creation_artifacts
.lifecycle_snapshot
.load
.is_none()
);
let newer_agent = ctx.with_fixture(|conn| {
conn.loaded_page_mut_for_owner(&newer.owner)
.unwrap()
.renderer_devtools_agent_token()
});
assert_ne!(newer_agent, previous_agent);
let mut output = crate::conn::CommandDispatchContext::default();
let (mut context, _) = crate::conn::CommandWorkflowContext::take_from(
&mut output,
None,
Some("SID-1".into()),
);
let stale = context
.with_state(&mut driver, move |state, context| {
state.prepare_navigation_protocol_completion(stale_completion, context)
})
.unwrap();
stale
.run_into_context(&mut driver, &mut context)
.await
.unwrap();
let retained = ctx
.with_fixture(|conn| {
let page = conn.loaded_page_mut_for_owner(&newer.owner).unwrap();
assert_eq!(page.renderer_devtools_agent_token(), newer_agent);
page.take_committed_document_post_response_continuation()
})
.expect("a stale completion must not release the newer adopted Document's parser");
assert!(ctx.with_fixture(|conn| {
conn.accepts_pending_document_navigation_for_owner(&newer.owner, &successor)
}));
retained.release();
})
.await;
}
#[tokio::test]
async fn superseded_adopted_replacement_releases_its_parser_before_beforeunload() {
tokio::task::LocalSet::new()
.run_until(async {
let (mut ctx, initial, mut navigation) = super::navigation_error::navigation_fixture();
ctx.with_fixture(|conn| {
conn.finish_renderer_document_navigation_for_owner(&navigation.owner, &initial)
.unwrap();
conn.clear_pending_document_navigation_for_owner_if_loader_matches(
&navigation.owner,
&initial.loader_id,
);
});
ctx.install_navigation_fixture_for_session_owner("about:blank", Some("SID-1"))
.await;
let token = ctx.with_fixture(|conn| {
conn.start_document_navigation_for_owner(
&navigation.owner,
navigation.loader_id.clone(),
)
.unwrap()
});
navigation.requested_url = Url::parse(
"data:text/html,<!doctype html><title>adopted-before-publication</title>",
)
.unwrap();
let job = ctx.with_fixture(|conn| {
conn.background_navigation_load_job_for_navigation(
&token,
&navigation,
Default::default(),
None,
)
.unwrap()
});
let (prepared, _) = job.run(None).await;
let NavigationLoadOutcome::ResponseCommitReady(prepared) = prepared.unwrap() else {
panic!("background job must retain its native DocumentCommit boundary");
};
let old_agent = ctx.with_fixture(|conn| {
conn.loaded_page_mut_for_owner(&navigation.owner)
.unwrap()
.renderer_devtools_agent_token()
});
let commit = crate::conn::PreparedDocumentCommitState::new();
let mut driver = crate::conn::ProtocolWorkflowDriver::new(ctx.handle());
let loaded = tokio::time::timeout(
std::time::Duration::from_secs(10),
commit.commit_using(&mut driver, token.clone(), &navigation, *prepared),
)
.await
.expect("native replacement must be adopted before supersession")
.unwrap();
assert!(
loaded
.page_creation_artifacts
.lifecycle_snapshot
.load
.is_none()
);
let adopted_agent = ctx.with_fixture(|conn| {
conn.loaded_page_mut_for_owner(&navigation.owner)
.unwrap()
.renderer_devtools_agent_token()
});
assert_ne!(adopted_agent, old_agent);
let completion = commit.complete(token, navigation.clone(), Ok(loaded));
let successor = ctx.with_fixture(|conn| {
conn.start_document_navigation_for_owner(&navigation.owner, "LID-successor".into())
.unwrap()
});
let mut output = crate::conn::CommandDispatchContext::default();
let (mut context, _) = crate::conn::CommandWorkflowContext::take_from(
&mut output,
None,
Some("SID-1".into()),
);
let stale = context
.with_state(&mut driver, move |state, context| {
state.prepare_navigation_protocol_completion(completion, context)
})
.unwrap();
assert!(stale.source_terminal_is_retained());
stale
.run_into_context(&mut driver, &mut context)
.await
.unwrap();
let pending = ctx.with_fixture(|conn| {
conn.loaded_page_mut_for_owner(&navigation.owner)
.unwrap()
.start_check_main_document_beforeunload()
.unwrap()
});
let completed =
tokio::time::timeout(std::time::Duration::from_secs(10), pending.wait())
.await
.expect("a stale adopted navigation must release its parser handoff")
.unwrap();
assert_eq!(completed.bool_reply_value(), Some(true));
ctx.with_fixture(|conn| {
assert_eq!(
conn.loaded_page_mut_for_owner(&navigation.owner)
.unwrap()
.renderer_devtools_agent_token(),
adopted_agent
);
assert!(
conn.accepts_pending_document_navigation_for_owner(
&navigation.owner,
&successor,
)
);
});
})
.await;
}
@@ -172,6 +172,23 @@ impl PreparedNavigationProtocolCompletion {
prefix_output: None,
};
if !conn.accepts_pending_document_navigation_for_owner(&result.state.owner, &result.token) {
// Native replacement adoption can precede this finite protocol
// completion. Supersession skips installation below, so the Page
// still owns this Document's response-gated parser continuation.
// Release only that adopted Document; beforeunload for the next
// navigation must not wait for an abandoned publication.
if let (Some(expected), Some(transaction)) = (expected.as_ref(), transaction.as_ref())
&& conn
.target_page_residence_identity_for_owner(&result.state.owner)
.as_ref()
== Some(&expected.target_page)
&& conn.renderer_page_residence_identity_for_owner(&result.state.owner)
== Some(expected.renderer_page)
&& let Some(page) = conn.loaded_page_mut_for_owner(&result.state.owner)
&& page.renderer_devtools_agent_token() == transaction.current().agent_token()
{
drop(page.take_committed_document_post_response_continuation());
}
super::super::push_superseded_navigation_result(&mut output, &result.state);
} else {
match navigation {