Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(devtools): make renderer teardown interrupt-safe

ldm04e52f654e62b
diff --git a/moli-protocol/src/conn/browser_context/fetch_owner.rs b/moli-protocol/src/conn/browser_context/fetch_owner.rs
index 2d4b25e61..32da82a23 100644
--- a/moli-protocol/src/conn/browser_context/fetch_owner.rs
+++ b/moli-protocol/src/conn/browser_context/fetch_owner.rs
@@ -1724,14 +1724,15 @@ impl CdpConnection {
let Some(mut owner) = self.target_session_owner_mut(session_id) else {
return Ok(None);
};
- let Some((pending, (subresource_enabled, subresource_resource_type))) =
+ let Some((pending, (subresource_enabled, subresource_resource_type), page_update_required)) =
owner.reset_fetch_config_for_session_and_drain_pending_state(session_id)
else {
return Ok(None);
};
- let page_command = if let Some(page) = owner
- .runtime_slot_mut()
- .and_then(TargetRuntimeSlot::loaded_page_mut)
+ let page_command = if page_update_required
+ && let Some(page) = owner
+ .runtime_slot_mut()
+ .and_then(TargetRuntimeSlot::loaded_page_mut)
{
Some(
page.start_set_fetch_subresource_interception(
diff --git a/moli-protocol/src/conn/browser_context/target_session_owner.rs b/moli-protocol/src/conn/browser_context/target_session_owner.rs
index 6fd552c0f..31be78636 100644
--- a/moli-protocol/src/conn/browser_context/target_session_owner.rs
+++ b/moli-protocol/src/conn/browser_context/target_session_owner.rs
@@ -64,6 +64,7 @@ pub(super) enum TargetSessionOwnerRef<'a> {
type FetchDisableStateWithSubresourceConfig = (
super::fetch_owner::SessionOwnerPendingFetchState,
(bool, Option<SubresourceResourceType>),
+ bool,
);
fn empty_pending_fetch_state() -> super::fetch_owner::SessionOwnerPendingFetchState {
@@ -1259,6 +1260,10 @@ impl<'a> TargetSessionOwnerMut<'a> {
Self::ActiveTarget {
browser_context, ..
} => {
+ let previous_subresource_config = browser_context
+ .active_target
+ .fetch_owner
+ .subresource_interception_config();
let removed = browser_context
.active_target
.fetch_owner
@@ -1275,13 +1280,19 @@ impl<'a> TargetSessionOwnerMut<'a> {
} else {
empty_pending_fetch_state()
};
- Some((pending, subresource_config))
+ let page_update_required =
+ removed && previous_subresource_config != subresource_config;
+ Some((pending, subresource_config, page_update_required))
}
Self::BackgroundTarget {
browser_context,
target_id,
..
} => {
+ let previous_subresource_config = browser_context
+ .parked_page_session_state(target_id)
+ .map(|state| state.fetch_config.subresource_interception_config())
+ .unwrap_or((false, None));
let mut subresource_config = (false, None);
let mut removed = false;
browser_context.mutate_parked_page_session_state(target_id, |state| {
@@ -1297,7 +1308,9 @@ impl<'a> TargetSessionOwnerMut<'a> {
} else {
empty_pending_fetch_state()
};
- Some((pending, subresource_config))
+ let page_update_required =
+ removed && previous_subresource_config != subresource_config;
+ Some((pending, subresource_config, page_update_required))
}
Self::NoLoadedBrowserContext => None,
}
@@ -3884,10 +3897,11 @@ mod tests {
"FETCH-active".to_owned(),
pending_subresource_fetch(11),
));
- let (pending, subresource_config) = owner
+ let (pending, subresource_config, page_update_required) = owner
.reset_fetch_config_for_session_and_drain_pending_state(Some("SID-active"))
.expect("active pending fetch state should drain");
assert_eq!(subresource_config, (false, None));
+ assert!(page_update_required);
assert_eq!(pending.3.len(), 1);
}
assert!(
@@ -3920,10 +3934,11 @@ mod tests {
"FETCH-parked".to_owned(),
pending_subresource_fetch(22),
));
- let (pending, subresource_config) = owner
+ let (pending, subresource_config, page_update_required) = owner
.reset_fetch_config_for_session_and_drain_pending_state(Some("SID-parked"))
.expect("parked pending fetch state should drain");
assert_eq!(subresource_config, (false, None));
+ assert!(page_update_required);
assert_eq!(pending.3.len(), 1);
}
assert!(
diff --git a/moli-protocol/src/domains/page/termination.rs b/moli-protocol/src/domains/page/termination.rs
index bbbad0216..a90f82a7b 100644
--- a/moli-protocol/src/domains/page/termination.rs
+++ b/moli-protocol/src/domains/page/termination.rs
@@ -1,6 +1,8 @@
use crate::conn::{
- BackgroundProtocolEvent, CdpConnection, Cmd, CommandOwnerScope, DocumentNavigationToken,
- NavigationDispatchState, PendingFetchNavigation, PendingSubresourceFetchRequest,
+ BackgroundProtocolEvent, CdpConnection, Cmd, CommandOwnerScope, DEFAULT_LOADER_ID,
+ DocumentNavigationToken, NavigationDispatchState, PendingFetchNavigation,
+ PendingSubresourceFetchAuthRequest, PendingSubresourceFetchRequest,
+ PendingSubresourceFetchResponseRequest, monotonic_timestamp_seconds,
};
use crate::domains::{activity, network};
use moli_core::RendererOutputFence;
@@ -10,7 +12,6 @@ use crate::domains::command_output::{CommandOutputBuffer, CommandOutputPlan};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PageTargetTerminationKind {
- Crash,
PageClose,
TargetClose,
}
@@ -271,6 +272,73 @@ pub(crate) async fn fail_pending_fetch_state_background_events_async(
renderer_output_predecessor
}
+/// Completes protocol-owned subresource pauses after the renderer has crashed.
+///
+/// A normal Fetch failure is first applied to the Page owner and then projected
+/// from its network backlog. `Page.crash` cannot use that path: the Page owner
+/// may be blocked in JavaScript, and the IO termination which unblocks it also
+/// retires the Page residence. The pending Fetch residences were already
+/// claimed by [`take_pending_fetch_state`], so emitting their terminal network
+/// state here is both race-free and independent of the renderer owner.
+fn fail_crashed_subresource_fetches_background_events(
+ conn: &CdpConnection,
+ out: &mut Vec<BackgroundProtocolEvent>,
+ session_id: Option<&str>,
+ error_text: &str,
+ pending_subresource_fetches: Vec<(String, PendingSubresourceFetchRequest)>,
+ pending_subresource_auths: Vec<(String, PendingSubresourceFetchAuthRequest)>,
+ pending_subresource_responses: Vec<(String, PendingSubresourceFetchResponseRequest)>,
+) {
+ let loader_id = conn
+ .current_document_loader_id_for_session_owner(session_id)
+ .unwrap_or_else(|| DEFAULT_LOADER_ID.to_owned());
+ let event_session_ids = conn.network_event_session_ids_for_session_owner(session_id);
+ let timestamp = monotonic_timestamp_seconds();
+
+ let mut emit_failure =
+ |network_request_id: &str,
+ frame_id: &str,
+ resource_type: moli_core::page::SubresourceResourceType| {
+ for event_session_id in &event_session_ids {
+ network::emit_loading_failed(
+ out,
+ event_session_id.as_deref(),
+ network_request_id,
+ frame_id,
+ &loader_id,
+ timestamp,
+ error_text,
+ resource_type.into(),
+ );
+ }
+ };
+
+ for (_, pending) in pending_subresource_fetches {
+ if let Some(continuation) = pending.detached_parser_script_fetch_continuation() {
+ let _ = continuation.fail(error_text.to_owned());
+ }
+ emit_failure(
+ &pending.network_request_id,
+ &pending.frame_id,
+ pending.resource_type,
+ );
+ }
+ for (_, pending) in pending_subresource_auths {
+ emit_failure(
+ &pending.network_request_id,
+ &pending.frame_id,
+ pending.resource_type,
+ );
+ }
+ for (_, pending) in pending_subresource_responses {
+ emit_failure(
+ &pending.network_request_id,
+ &pending.frame_id,
+ pending.resource_type,
+ );
+ }
+}
+
pub(super) fn try_start_stop_loading_command_dispatch(
conn: &CdpConnection,
cmd: &Cmd<'_>,
@@ -406,13 +474,32 @@ pub(super) async fn complete_crash_command_dispatch(
pending_subresource_responses,
) = take_pending_fetch_state(conn, session_id);
- let mut pending_await_events = Vec::new();
- conn.fail_pending_inspector_awaits_for_session_owner_background_events_into(
- &mut pending_await_events,
- command_context.protocol_events_mut(),
+ // Page.crash is an IO-agent command in Chromium. Capture protocol-owned
+ // request residences first, then cooperatively terminate active V8 before
+ // retiring the Page so teardown cannot wait behind the JavaScript stack it
+ // is meant to destroy.
+ if let Ok(termination) = conn.start_runtime_io_protocol_message_for_session_owner(
session_id,
- "Page crashed",
- );
+ r#"{"id":0,"method":"Runtime.terminateExecution"}"#.to_owned(),
+ ) {
+ let _ = termination.wait().await;
+ }
+
+ // Page.crash retires the target, not merely the DevTools session which
+ // issued the command. Settle every attached session before dropping the
+ // Page; otherwise a late completion from (for example) the primary
+ // session can wait forever on a response sender owned by the retired
+ // renderer while the crash was issued by an auxiliary session.
+ let target_inspector_session_ids = conn.page_event_session_ids_for_session_owner(session_id);
+ let mut pending_await_events = Vec::new();
+ for inspector_session_id in &target_inspector_session_ids {
+ conn.fail_pending_inspector_awaits_for_session_owner_background_events_into(
+ &mut pending_await_events,
+ command_context.protocol_events_mut(),
+ inspector_session_id.as_deref(),
+ "Page crashed",
+ );
+ }
out.extend(pending_await_events);
let renderer_output_predecessor = fail_pending_fetch_state_background_events_async(
@@ -423,27 +510,59 @@ pub(super) async fn complete_crash_command_dispatch(
pending_navigations,
pending_auth_navigations,
pending_response_navigations,
+ Vec::new(),
+ Vec::new(),
+ Vec::new(),
+ )
+ .await;
+ fail_crashed_subresource_fetches_background_events(
+ conn,
+ &mut out,
+ fail_session_id,
+ "Page crashed",
pending_subresource_fetches,
pending_subresource_auths,
pending_subresource_responses,
- )
- .await;
+ );
if let Some(predecessor) = renderer_output_predecessor {
command_context.set_renderer_output_predecessor(predecessor);
}
- // A crash must not retire the Page route while the terminal renderer
- // publication produced above is still in transport. The command's exact
- // cursor admits that publication first; this protocol-owner continuation
- // then marks the target crashed and emits target lifecycle notifications.
- conn.publish_page_target_termination_owner_action(PageTargetTerminationOwnerAction::new(
- CommandOwnerScope::capture(conn, session_id),
- target_id,
- PageTargetTerminationKind::Crash,
- ));
+ out.extend(
+ mark_page_target_crashed_background_events_async(conn, session_id, &target_id).await,
+ );
complete_success_with_background_events(out)
}
+async fn mark_page_target_crashed_background_events_async(
+ conn: &mut CdpConnection,
+ session_id: Option<&str>,
+ target_id: &str,
+) -> Vec<BackgroundProtocolEvent> {
+ let inspector_session_ids = conn.page_event_session_ids_for_session_owner(session_id);
+ for inspector_session_id in &inspector_session_ids {
+ let _ = conn.with_target_devtools_session_state_for_session_mut(
+ inspector_session_id.as_deref(),
+ |state| {
+ state
+ .runtime_session_state
+ .record_inspector_target_crashed();
+ },
+ );
+ }
+ let _ = conn
+ .mark_target_crashed_for_session_owner_async(session_id)
+ .await;
+ let mut out = inspector_session_ids
+ .into_iter()
+ .map(|inspector_session_id| {
+ BackgroundProtocolEvent::inspector_target_crashed(inspector_session_id.as_deref())
+ })
+ .collect::<Vec<_>>();
+ out.extend(conn.target_crashed_events_for_all_discovery_owners(target_id, "crashed", 1));
+ out
+}
+
pub(super) fn try_start_close_command_dispatch(
conn: &CdpConnection,
cmd: &Cmd<'_>,
@@ -543,43 +662,8 @@ pub(crate) async fn complete_page_target_termination_owner_action_async(
conn.take_scheduler_events(),
);
}
- if kind == PageTargetTerminationKind::Crash {
- let inspector_session_ids =
- conn.page_event_session_ids_for_session_owner(owner_scope.session_id());
- for inspector_session_id in &inspector_session_ids {
- let _ = conn.with_target_devtools_session_state_for_session_mut(
- inspector_session_id.as_deref(),
- |state| {
- state
- .runtime_session_state
- .record_inspector_target_crashed();
- },
- );
- }
- let _ = conn
- .mark_target_crashed_for_session_owner_async(owner_scope.session_id())
- .await;
- for inspector_session_id in inspector_session_ids {
- out.push(BackgroundProtocolEvent::inspector_target_crashed(
- inspector_session_id.as_deref(),
- ));
- }
- out.extend(conn.target_crashed_events_for_all_discovery_owners(
- &expected_target_id,
- "crashed",
- 1,
- ));
- return crate::conn::CdpTurnOutcome::new_with_protocol_events(
- out,
- conn.take_scheduler_events(),
- );
- }
-
let target_host_closure = conn.prepare_target_host_closure(&expected_target_id);
let closed = match kind {
- PageTargetTerminationKind::Crash => {
- unreachable!("crash termination returns before the close branch")
- }
PageTargetTerminationKind::PageClose => {
conn.close_page_target_for_session_owner_async(owner_scope.session_id())
.await
diff --git a/moli-protocol/src/domains/runtime/dispatcher.rs b/moli-protocol/src/domains/runtime/dispatcher.rs
index 1251740e0..88e065850 100644
--- a/moli-protocol/src/domains/runtime/dispatcher.rs
+++ b/moli-protocol/src/domains/runtime/dispatcher.rs
@@ -8201,6 +8201,25 @@ async fn complete_pending_runtime_inspector_command(
if saw_current_response {
renderer_response_rx.take();
}
+ // Target teardown can settle the frontend command from a different
+ // command turn before this renderer completion reaches the scheduler. In
+ // that case the renderer-call correlation has already been consumed and
+ // the deferred response sender can remain alive in the retired Page
+ // command. Waiting on its receiver here would stall the entire CDP owner
+ // after, for example, Page.crash interrupted an active Runtime.evaluate.
+ // Treat the missing correlation as the command's already-settled
+ // tombstone; any non-response Inspector output in this late completion is
+ // still projected below.
+ if !saw_current_response
+ && renderer_response_rx.is_some()
+ && completed.command_id.is_some_and(|command_id| {
+ conn.renderer_runtime_command_cause_for_frontend(completed.session_id(), command_id)
+ .is_none()
+ })
+ {
+ renderer_response_rx.take();
+ saw_current_response = true;
+ }
if !completed.wait_for_deferred_reply
&& let Some(renderer_response_rx) = renderer_response_rx.take()
{
diff --git a/moli-renderer-v8/src/runtime/owner.rs b/moli-renderer-v8/src/runtime/owner.rs
index 597493fc4..7b3a5e21c 100644
--- a/moli-renderer-v8/src/runtime/owner.rs
+++ b/moli-renderer-v8/src/runtime/owner.rs
@@ -3197,7 +3197,15 @@ impl RendererOwnerHandle {
// Freeze this owner turn before making the Page resident again. The
// concrete publication is already source-bound, so a later lifecycle
// turn never needs to rescan or publish output on this turn's behalf.
- let output = entry.page_vm_mut().settle_renderer_output_publication();
+ // Cancellation of a committed phase-one navigation can intentionally
+ // retire the last PageVm before this entry is restored. In that case
+ // there is no live producer left to settle; the owner-local restore
+ // below performs the retired-entry teardown.
+ let output = entry
+ .active_page_vm()
+ .is_some()
+ .then(|| entry.page_vm_mut().settle_renderer_output_publication())
+ .flatten();
restore_entry_after_command_on_bound_owner_local_store(token, entry);
if let Some(output) = output {
self.publish_renderer_output(output);
diff --git a/moli-renderer-v8/src/runtime/owner_local.rs b/moli-renderer-v8/src/runtime/owner_local.rs
index ca08b1832..fd1fd9bc4 100644
--- a/moli-renderer-v8/src/runtime/owner_local.rs
+++ b/moli-renderer-v8/src/runtime/owner_local.rs
@@ -426,11 +426,15 @@ impl RendererPageHandle {
.close("Inspector target closed with its Page handle");
self.inspector_io_ingress
.close("Inspector target closed with its Page handle");
+ let terminated_active_execution = self
+ .inspector_io_ingress
+ .terminate_execution_for_target_close();
self.javascript_dialog_broker.dismiss_pending();
self.page_context_cancel_tx
.cancel(RendererPageContextCancelReason::PageClosed);
tracing::debug!(
page_id = token.page_id.as_u64(),
+ terminated_active_execution,
"closing renderer page handle"
);
diff --git a/moli-renderer-v8/src/script_vm/inspector_io.rs b/moli-renderer-v8/src/script_vm/inspector_io.rs
index a514993c5..ecccb00ee 100644
--- a/moli-renderer-v8/src/script_vm/inspector_io.rs
+++ b/moli-renderer-v8/src/script_vm/inspector_io.rs
@@ -276,6 +276,19 @@ impl RendererInspectorIoIngress {
.map(|route| route.target.route_id())
}
+ /// Breaks an active V8 call so target teardown can reach the Page owner.
+ ///
+ /// Closing the ingress prevents queued IO work from being claimed, but
+ /// the owner may still be inside non-yielding JavaScript. Target close
+ /// owns this isolate's lifetime, so teardown can terminate that execution
+ /// directly instead of depending on another DevTools command.
+ pub(crate) fn terminate_execution_for_target_close(&self) -> bool {
+ self.shared
+ .interrupt_route
+ .as_ref()
+ .is_some_and(|route| route.isolate.terminate_execution())
+ }
+
pub(crate) fn configure_owner_wake(
&self,
owner_wake_tx: tokio::sync::mpsc::UnboundedSender<RendererInspectorIoOwnerWake>,
@@ -720,6 +733,66 @@ mod tests {
);
}
+ #[test]
+ fn concurrent_owner_interrupt_and_pause_claim_exactly_once_under_stress() {
+ let ingress = ingress();
+ let agent = RendererDevToolsAgentToken::allocate();
+
+ for round in 0..128 {
+ let route = enqueue(
+ &ingress,
+ agent,
+ Some("session-race"),
+ &format!("command-{round}"),
+ );
+ let barrier = Arc::new(std::sync::Barrier::new(4));
+ let (owner, interrupt, pause) = std::thread::scope(|scope| {
+ let owner_ingress = ingress.clone();
+ let owner_barrier = Arc::clone(&barrier);
+ let owner = scope.spawn(move || {
+ owner_barrier.wait();
+ owner_ingress.claim_for_owner()
+ });
+ let interrupt_ingress = ingress.clone();
+ let interrupt_barrier = Arc::clone(&barrier);
+ let interrupt = scope.spawn(move || {
+ interrupt_barrier.wait();
+ interrupt_ingress.claim_for_interrupt()
+ });
+ let pause_ingress = ingress.clone();
+ let pause_barrier = Arc::clone(&barrier);
+ let pause = scope.spawn(move || {
+ pause_barrier.wait();
+ pause_ingress.claim_for_pause()
+ });
+ barrier.wait();
+ (
+ owner.join().expect("owner claimant thread"),
+ interrupt.join().expect("interrupt claimant thread"),
+ pause.join().expect("pause claimant thread"),
+ )
+ });
+ let mut claimed = [owner, interrupt, pause]
+ .into_iter()
+ .flatten()
+ .collect::<Vec<_>>();
+ assert_eq!(
+ claimed.len(),
+ 1,
+ "round {round} must have one successful consumer"
+ );
+ let command = claimed.pop().expect("exactly one claimed command");
+ assert_eq!(command.raw_json(), format!("command-{round}"));
+ ingress.first_dispatch_guard(&command).release();
+ drop(route);
+ }
+
+ assert!(
+ ingress.shared.state.lock().sessions.is_empty(),
+ "every stressed first-dispatch lane must retire"
+ );
+ }
+
#[test]
fn one_session_is_fifo_while_another_session_is_independent() {
let ingress = ingress();
@@ -742,11 +815,13 @@ mod tests {
}
#[tokio::test]
- async fn detach_cancels_the_queue_while_an_active_first_dispatch_guard_finishes_safely() {
+ async fn detach_cancels_all_queued_commands_while_active_first_dispatch_retires_safely() {
let ingress = ingress();
let agent = RendererDevToolsAgentToken::allocate();
- let first_route = enqueue(&ingress, agent, Some("session-a"), "a1");
- let second_route = enqueue(&ingress, agent, Some("session-a"), "a2");
+ let mut routes = (0..64)
+ .map(|index| enqueue(&ingress, agent, Some("session-a"), &format!("a-{index}")))
+ .collect::<Vec<_>>();
+ let first_route = routes.remove(0);
let first = ingress
.claim_for_interrupt()
@@ -758,16 +833,66 @@ mod tests {
);
ingress.detach_session(agent, &DevToolsSessionKey::Attached("session-a".to_owned()));
+ for (index, route) in routes.into_iter().enumerate() {
+ assert_eq!(
+ route.wait_for_claim().await,
+ Ok(RendererRuntimeInspectorIoCommandClaim::Canceled),
+ "detach must cancel queued command {}",
+ index + 1
+ );
+ }
+ assert!(ingress.claim_for_owner().is_none());
+ assert!(ingress.claim_for_interrupt().is_none());
+ assert!(ingress.claim_for_pause().is_none());
+
+ first_dispatch.release();
+ assert!(
+ ingress.shared.state.lock().sessions.is_empty(),
+ "the detached lane must retire after its active first dispatch releases"
+ );
+ }
+
+ #[tokio::test]
+ async fn close_cancels_every_session_and_rejects_late_io_commands() {
+ let ingress = ingress();
+ let agent = RendererDevToolsAgentToken::allocate();
+ let a1_route = enqueue(&ingress, agent, Some("session-a"), "a1");
+ let a2_route = enqueue(&ingress, agent, Some("session-a"), "a2");
+ let b1_route = enqueue(&ingress, agent, Some("session-b"), "b1");
+ let b2_route = enqueue(&ingress, agent, Some("session-b"), "b2");
+
+ let active = ingress
+ .claim_for_owner()
+ .expect("one session head should become active");
+ let mut first_dispatch = ingress.first_dispatch_guard(&active);
assert_eq!(
- second_route.wait_for_claim().await,
- Ok(RendererRuntimeInspectorIoCommandClaim::Canceled),
- "detach must cancel commands that have not been claimed"
+ a1_route.wait_for_claim().await,
+ Ok(RendererRuntimeInspectorIoCommandClaim::Inspector)
);
+ ingress.close("test target closed");
+ for route in [a2_route, b1_route, b2_route] {
+ assert_eq!(
+ route.wait_for_claim().await,
+ Ok(RendererRuntimeInspectorIoCommandClaim::Canceled),
+ "close must cancel every unclaimed session command"
+ );
+ }
+ assert!(ingress.claim_for_owner().is_none());
+ assert!(ingress.claim_for_interrupt().is_none());
+ assert!(ingress.claim_for_pause().is_none());
+
first_dispatch.release();
assert!(
ingress.shared.state.lock().sessions.is_empty(),
- "the detached lane must retire after its active first dispatch releases"
+ "the active lane must retire safely after target close"
+ );
+
+ let late = enqueue(&ingress, agent, Some("session-late"), "late");
+ assert_eq!(
+ late.wait_for_claim().await,
+ Ok(RendererRuntimeInspectorIoCommandClaim::Canceled),
+ "a closed target must reject late IO ingress"
);
}