mirror of
https://github.com/lexmount/moli.git
synced 2026-11-27 08:02:48 +00:00
fix(protocol): retain worker attachment ownership through delayed commits
This commit is contained in:
@@ -12,6 +12,13 @@ use crate::domains::target::session_disposal::{
|
||||
use crate::{conn::SessionDisposalPlan, domains::session::PreparedSessionHandlers};
|
||||
|
||||
impl ProtocolWorkflowState<'_> {
|
||||
pub(crate) fn prepare_removed_shared_worker_attachment(
|
||||
self,
|
||||
attachment: crate::domains::target::RemovedSharedWorkerAttachment,
|
||||
) -> Result<()> {
|
||||
attachment.prepare_captured(self.connection)
|
||||
}
|
||||
|
||||
pub(crate) fn finish_removed_shared_worker_attachment(
|
||||
self,
|
||||
cleanup: Option<crate::domains::target::session_disposal::PreparedRemovedWorkerSession>,
|
||||
|
||||
@@ -20,6 +20,21 @@ pub(crate) struct TargetControlPlane {
|
||||
}
|
||||
|
||||
impl TargetControlPlane {
|
||||
pub(crate) fn bind_worker_attachment(
|
||||
&mut self,
|
||||
session_id: &str,
|
||||
attachment: super::TargetWorkerProtocolAttachmentIdentity,
|
||||
) {
|
||||
self.sessions.bind_worker_attachment(session_id, attachment);
|
||||
}
|
||||
|
||||
pub(crate) fn worker_attachment(
|
||||
&self,
|
||||
session_id: &str,
|
||||
) -> Option<&super::TargetWorkerProtocolAttachmentIdentity> {
|
||||
self.sessions.worker_attachment(session_id)
|
||||
}
|
||||
|
||||
pub(crate) fn close_session_waiters(&self) {
|
||||
self.sessions.close_session_waiters();
|
||||
}
|
||||
|
||||
@@ -160,6 +160,7 @@ struct AttachedTargetSession {
|
||||
waiting_for_debugger: bool,
|
||||
closing: bool,
|
||||
closed: SessionClosed,
|
||||
worker_attachment: Option<Box<super::TargetWorkerProtocolAttachmentIdentity>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -213,6 +214,30 @@ pub(crate) struct TargetSessionRegistry {
|
||||
}
|
||||
|
||||
impl TargetSessionRegistry {
|
||||
pub(crate) fn bind_worker_attachment(
|
||||
&mut self,
|
||||
session_id: &str,
|
||||
attachment: super::TargetWorkerProtocolAttachmentIdentity,
|
||||
) {
|
||||
let session = self
|
||||
.attached_sessions
|
||||
.get_mut(session_id)
|
||||
.expect("worker provenance binds an admitted session");
|
||||
assert_eq!(attachment.session_id(), session_id);
|
||||
assert_eq!(attachment.target_id(), session.target_id);
|
||||
session.worker_attachment = Some(Box::new(attachment));
|
||||
}
|
||||
|
||||
pub(crate) fn worker_attachment(
|
||||
&self,
|
||||
session_id: &str,
|
||||
) -> Option<&super::TargetWorkerProtocolAttachmentIdentity> {
|
||||
self.attached_sessions
|
||||
.get(session_id)?
|
||||
.worker_attachment
|
||||
.as_deref()
|
||||
}
|
||||
|
||||
pub(crate) fn close_session_waiters(&self) {
|
||||
for session in self.attached_sessions.values() {
|
||||
session.closed.0.send_replace(true);
|
||||
@@ -266,6 +291,7 @@ impl TargetSessionRegistry {
|
||||
waiting_for_debugger,
|
||||
closing: false,
|
||||
closed: SessionClosed::default(),
|
||||
worker_attachment: None,
|
||||
},
|
||||
);
|
||||
self.attached_sessions_by_target
|
||||
|
||||
@@ -396,13 +396,33 @@ impl CdpConnection {
|
||||
emit_attached_state_delta: bool,
|
||||
) -> TargetEventPlan {
|
||||
let (target_id, target_info, sessions) = prepared.into_parts();
|
||||
let should_emit_attached_state_delta = emit_attached_state_delta && !sessions.is_empty();
|
||||
let attached_state_delta_plan = should_emit_attached_state_delta
|
||||
.then(|| self.exact_target_info_changed_event_plan_for_target_delta(&target_id));
|
||||
let mut attached_state_delta_plan = None;
|
||||
let mut plan = TargetEventPlan::default();
|
||||
for session in sessions {
|
||||
let worker_attachment = session.worker_attachment().cloned();
|
||||
assert!(
|
||||
worker_attachment.is_some()
|
||||
|| !matches!(
|
||||
session.route(),
|
||||
CdpSessionRoute::SharedWorkerTarget { .. }
|
||||
| CdpSessionRoute::DedicatedWorkerTarget { .. }
|
||||
| CdpSessionRoute::ServiceWorkerTarget { .. }
|
||||
),
|
||||
"worker attach must freeze its original native attachment before waiting"
|
||||
);
|
||||
if worker_attachment
|
||||
.as_ref()
|
||||
.is_some_and(|attachment| !attachment.is_current())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if emit_attached_state_delta && attached_state_delta_plan.is_none() {
|
||||
attached_state_delta_plan =
|
||||
Some(self.exact_target_info_changed_event_plan_for_target_delta(&target_id));
|
||||
}
|
||||
let (session_id, owner_session_id, route, auto_attached, waiting_for_debugger) =
|
||||
session.into_parts();
|
||||
let binding_session_id = session_id.clone();
|
||||
if auto_attached {
|
||||
self.target_control
|
||||
.ensure_owner(owner_session_id.as_deref());
|
||||
@@ -416,6 +436,10 @@ impl CdpConnection {
|
||||
waiting_for_debugger,
|
||||
target_info.clone(),
|
||||
));
|
||||
if let Some(attachment) = worker_attachment {
|
||||
self.target_control
|
||||
.bind_worker_attachment(&binding_session_id, attachment);
|
||||
}
|
||||
}
|
||||
if let Some(attached_state_delta_plan) = attached_state_delta_plan {
|
||||
plan.extend(attached_state_delta_plan);
|
||||
@@ -492,13 +516,16 @@ impl CdpConnection {
|
||||
self.rollback_prepared_attach_session_sync_without_event(&prepared_session);
|
||||
return Err("UnknownTargetId");
|
||||
};
|
||||
Ok(
|
||||
self.commit_prepared_attach_event_plan(PreparedTargetAttach::new(
|
||||
target_id,
|
||||
target_info,
|
||||
[prepared_session],
|
||||
)),
|
||||
)
|
||||
let attachment = self
|
||||
.worker_protocol_attachment_identity_for_route(
|
||||
prepared_session.route(),
|
||||
prepared_session.session_id(),
|
||||
)
|
||||
.expect("synchronous worker attachment retains its original native scope");
|
||||
Ok(self.commit_prepared_attach_event_plan(
|
||||
PreparedTargetAttach::new(target_id, target_info, [prepared_session])
|
||||
.with_worker_attachment(attachment),
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn attach_service_worker_target_session_event_plan(
|
||||
@@ -530,13 +557,16 @@ impl CdpConnection {
|
||||
self.rollback_prepared_attach_session_sync_without_event(&prepared_session);
|
||||
return Err("UnknownTargetId");
|
||||
};
|
||||
Ok(
|
||||
self.commit_prepared_attach_event_plan(PreparedTargetAttach::new(
|
||||
target_id,
|
||||
target_info,
|
||||
[prepared_session],
|
||||
)),
|
||||
)
|
||||
let attachment = self
|
||||
.worker_protocol_attachment_identity_for_route(
|
||||
prepared_session.route(),
|
||||
prepared_session.session_id(),
|
||||
)
|
||||
.expect("synchronous worker attachment retains its original native scope");
|
||||
Ok(self.commit_prepared_attach_event_plan(
|
||||
PreparedTargetAttach::new(target_id, target_info, [prepared_session])
|
||||
.with_worker_attachment(attachment),
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn attach_dedicated_worker_target_session_event_plan(
|
||||
@@ -568,13 +598,16 @@ impl CdpConnection {
|
||||
self.rollback_prepared_attach_session_sync_without_event(&prepared_session);
|
||||
return Err("UnknownTargetId");
|
||||
};
|
||||
Ok(
|
||||
self.commit_prepared_dedicated_worker_attach_event_plan(PreparedTargetAttach::new(
|
||||
target_id,
|
||||
target_info,
|
||||
[prepared_session],
|
||||
)),
|
||||
)
|
||||
let attachment = self
|
||||
.worker_protocol_attachment_identity_for_route(
|
||||
prepared_session.route(),
|
||||
prepared_session.session_id(),
|
||||
)
|
||||
.expect("synchronous worker attachment retains its original native scope");
|
||||
Ok(self.commit_prepared_dedicated_worker_attach_event_plan(
|
||||
PreparedTargetAttach::new(target_id, target_info, [prepared_session])
|
||||
.with_worker_attachment(attachment),
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn prepare_auto_attached_tab_session_binding(
|
||||
|
||||
@@ -180,6 +180,7 @@ impl PreparedTargetHostClosure {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct TargetAttachSessionCommit {
|
||||
worker_attachment: Option<Box<super::TargetWorkerProtocolAttachmentIdentity>>,
|
||||
session_id: String,
|
||||
owner_session_id: Option<String>,
|
||||
route: CdpSessionRoute,
|
||||
@@ -188,6 +189,12 @@ pub(crate) struct TargetAttachSessionCommit {
|
||||
}
|
||||
|
||||
impl TargetAttachSessionCommit {
|
||||
pub(crate) fn worker_attachment(
|
||||
&self,
|
||||
) -> Option<&super::TargetWorkerProtocolAttachmentIdentity> {
|
||||
self.worker_attachment.as_deref()
|
||||
}
|
||||
|
||||
pub(crate) fn new(
|
||||
session_id: impl Into<String>,
|
||||
owner_session_id: Option<String>,
|
||||
@@ -196,6 +203,7 @@ impl TargetAttachSessionCommit {
|
||||
waiting_for_debugger: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
worker_attachment: None,
|
||||
session_id: session_id.into(),
|
||||
owner_session_id,
|
||||
route,
|
||||
@@ -266,6 +274,20 @@ pub(crate) struct PreparedTargetAttach {
|
||||
}
|
||||
|
||||
impl PreparedTargetAttach {
|
||||
pub(crate) fn with_worker_attachment(
|
||||
mut self,
|
||||
attachment: super::TargetWorkerProtocolAttachmentIdentity,
|
||||
) -> Self {
|
||||
assert_eq!(attachment.target_id(), self.target_id);
|
||||
let session = self
|
||||
.sessions
|
||||
.iter_mut()
|
||||
.find(|session| session.session_id() == attachment.session_id())
|
||||
.expect("worker attachment belongs to its prepared session");
|
||||
session.worker_attachment = Some(Box::new(attachment));
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn new(
|
||||
target_id: impl Into<String>,
|
||||
target_info: DevToolsTargetInfo,
|
||||
|
||||
@@ -37,6 +37,47 @@ impl TargetWorkerProtocolAttachmentIdentity {
|
||||
}
|
||||
|
||||
impl CdpConnection {
|
||||
pub(crate) fn worker_protocol_attachment_identity_for_route(
|
||||
&self,
|
||||
route: &CdpSessionRoute,
|
||||
session_id: &str,
|
||||
) -> Option<TargetWorkerProtocolAttachmentIdentity> {
|
||||
match route {
|
||||
CdpSessionRoute::SharedWorkerTarget {
|
||||
browser_context_id,
|
||||
target_id,
|
||||
} => self
|
||||
.browser_context_by_id(browser_context_id)?
|
||||
.shared_worker_target(target_id)?
|
||||
.protocol_attachment_identity(browser_context_id, session_id)
|
||||
.map(TargetWorkerProtocolAttachmentIdentity::SharedOrDedicated),
|
||||
CdpSessionRoute::DedicatedWorkerTarget {
|
||||
browser_context_id,
|
||||
target_id,
|
||||
} => self
|
||||
.browser_context_by_id(browser_context_id)?
|
||||
.dedicated_worker_target(target_id)?
|
||||
.protocol_attachment_identity(browser_context_id, session_id)
|
||||
.map(TargetWorkerProtocolAttachmentIdentity::SharedOrDedicated),
|
||||
CdpSessionRoute::ServiceWorkerTarget {
|
||||
browser_context_id,
|
||||
target_id,
|
||||
} => self
|
||||
.browser_context_by_id(browser_context_id)?
|
||||
.service_worker_target(target_id)?
|
||||
.protocol_attachment_identity(browser_context_id, session_id)
|
||||
.map(TargetWorkerProtocolAttachmentIdentity::Service),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn committed_worker_protocol_attachment_identity(
|
||||
&self,
|
||||
session_id: &str,
|
||||
) -> Option<&TargetWorkerProtocolAttachmentIdentity> {
|
||||
self.target_control.worker_attachment(session_id)
|
||||
}
|
||||
|
||||
/// Captures the exact live Worker attachment addressed by one renderer
|
||||
/// output stream and DevTools session.
|
||||
///
|
||||
|
||||
@@ -35,7 +35,7 @@ mod tests;
|
||||
mod worker_target;
|
||||
pub(crate) use worker_target::{
|
||||
CapturedWorkerAttachment, DedicatedWorkerRetirementIdentity, PreparedDedicatedWorkerRetirement,
|
||||
WorkerRetirementFailureCleanup,
|
||||
RemovedSharedWorkerAttachment, WorkerRetirementFailureCleanup,
|
||||
};
|
||||
|
||||
pub(crate) enum TargetCommandFailureCleanup {
|
||||
|
||||
@@ -84,19 +84,26 @@ pub(super) fn auto_attach_related(conn: &mut CdpConnection, cmd: &Cmd<'_>) -> Co
|
||||
&& let Some((session_id, target_info)) =
|
||||
attach_service_worker_target_for_auto_attach_related(conn, &target.target_id)
|
||||
{
|
||||
let event_plan = conn.commit_prepared_attach_event_plan(PreparedTargetAttach::new(
|
||||
&target.target_id,
|
||||
target_info,
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
session_id,
|
||||
cmd.session_id.map(str::to_owned),
|
||||
CdpSessionRoute::ServiceWorkerTarget {
|
||||
browser_context_id: target.browser_context_id.clone(),
|
||||
target_id: target.target_id.clone(),
|
||||
},
|
||||
false,
|
||||
)],
|
||||
));
|
||||
let route = CdpSessionRoute::ServiceWorkerTarget {
|
||||
browser_context_id: target.browser_context_id.clone(),
|
||||
target_id: target.target_id.clone(),
|
||||
};
|
||||
let attachment = conn
|
||||
.worker_protocol_attachment_identity_for_route(&route, &session_id)
|
||||
.expect("related worker attach retains its original native scope");
|
||||
let event_plan = conn.commit_prepared_attach_event_plan(
|
||||
PreparedTargetAttach::new(
|
||||
&target.target_id,
|
||||
target_info,
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
session_id,
|
||||
cmd.session_id.map(str::to_owned),
|
||||
route,
|
||||
false,
|
||||
)],
|
||||
)
|
||||
.with_worker_attachment(attachment),
|
||||
);
|
||||
for event in event_plan {
|
||||
plan.push_background_event(event);
|
||||
}
|
||||
@@ -637,7 +644,26 @@ async fn set_auto_attach_inner_async(
|
||||
route.is_some(),
|
||||
"attached shared worker target must remain addressable"
|
||||
);
|
||||
route.map(|route| (target_id, session_id, route))
|
||||
let route = route?;
|
||||
let attachment = conn
|
||||
.worker_protocol_attachment_identity_for_route(&route, &session_id)
|
||||
.expect("prepared worker attach retains its original native scope");
|
||||
let info = conn
|
||||
.browser_context_by_id(route.browser_context_id()?)?
|
||||
.devtools_target_info(&target_id)?;
|
||||
Some(
|
||||
PreparedTargetAttach::new(
|
||||
target_id,
|
||||
info,
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
session_id,
|
||||
owner_session_id.map(str::to_owned),
|
||||
route,
|
||||
waiting_for_debugger,
|
||||
)],
|
||||
)
|
||||
.with_worker_attachment(attachment),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let attached_dedicated_worker_targets = attached_dedicated_worker_targets
|
||||
@@ -651,7 +677,26 @@ async fn set_auto_attach_inner_async(
|
||||
route.is_some(),
|
||||
"attached dedicated worker target must remain addressable"
|
||||
);
|
||||
route.map(|route| (target_id, session_id, route))
|
||||
let route = route?;
|
||||
let attachment = conn
|
||||
.worker_protocol_attachment_identity_for_route(&route, &session_id)
|
||||
.expect("prepared worker attach retains its original native scope");
|
||||
let info = conn
|
||||
.browser_context_by_id(route.browser_context_id()?)?
|
||||
.devtools_target_info(&target_id)?;
|
||||
Some(
|
||||
PreparedTargetAttach::new(
|
||||
target_id,
|
||||
info,
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
session_id,
|
||||
owner_session_id.map(str::to_owned),
|
||||
route,
|
||||
waiting_for_debugger,
|
||||
)],
|
||||
)
|
||||
.with_worker_attachment(attachment),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let attached_service_worker_targets = attached_service_worker_targets
|
||||
@@ -665,7 +710,26 @@ async fn set_auto_attach_inner_async(
|
||||
route.is_some(),
|
||||
"attached service worker target must remain addressable"
|
||||
);
|
||||
route.map(|route| (target_id, session_id, route))
|
||||
let route = route?;
|
||||
let attachment = conn
|
||||
.worker_protocol_attachment_identity_for_route(&route, &session_id)
|
||||
.expect("prepared worker attach retains its original native scope");
|
||||
let info = conn
|
||||
.browser_context_by_id(route.browser_context_id()?)?
|
||||
.devtools_target_info(&target_id)?;
|
||||
Some(
|
||||
PreparedTargetAttach::new(
|
||||
target_id,
|
||||
info,
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
session_id,
|
||||
owner_session_id.map(str::to_owned),
|
||||
route,
|
||||
waiting_for_debugger,
|
||||
)],
|
||||
)
|
||||
.with_worker_attachment(attachment),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let attached_tab_targets = attached_tab_targets
|
||||
@@ -762,67 +826,16 @@ async fn set_auto_attach_inner_async(
|
||||
));
|
||||
out.extend_background_events(event_plan);
|
||||
}
|
||||
for (target_id, session_id, route) in attached_shared_worker_targets {
|
||||
let ti = {
|
||||
let bc = conn
|
||||
.browser_context
|
||||
.as_ref()
|
||||
.expect("browser context must exist when emitting attach events");
|
||||
bc.devtools_target_info(&target_id)
|
||||
.expect("attached shared worker target must remain addressable")
|
||||
};
|
||||
let event_plan = conn.commit_prepared_attach_event_plan(PreparedTargetAttach::new(
|
||||
&target_id,
|
||||
ti,
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
session_id,
|
||||
owner_session_id.map(str::to_owned),
|
||||
route,
|
||||
waiting_for_debugger,
|
||||
)],
|
||||
));
|
||||
for prepared in attached_shared_worker_targets {
|
||||
let event_plan = conn.commit_prepared_attach_event_plan(prepared);
|
||||
out.extend_background_events(event_plan);
|
||||
}
|
||||
for (target_id, session_id, route) in attached_dedicated_worker_targets {
|
||||
let ti = {
|
||||
let bc = conn
|
||||
.browser_context
|
||||
.as_ref()
|
||||
.expect("browser context must exist when emitting attach events");
|
||||
bc.devtools_target_info(&target_id)
|
||||
.expect("attached dedicated worker target must remain addressable")
|
||||
};
|
||||
let event_plan = conn.commit_prepared_attach_event_plan(PreparedTargetAttach::new(
|
||||
&target_id,
|
||||
ti,
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
session_id,
|
||||
owner_session_id.map(str::to_owned),
|
||||
route,
|
||||
waiting_for_debugger,
|
||||
)],
|
||||
));
|
||||
for prepared in attached_dedicated_worker_targets {
|
||||
let event_plan = conn.commit_prepared_attach_event_plan(prepared);
|
||||
out.extend_background_events(event_plan);
|
||||
}
|
||||
for (target_id, session_id, route) in attached_service_worker_targets {
|
||||
let ti = {
|
||||
let bc = conn
|
||||
.browser_context
|
||||
.as_ref()
|
||||
.expect("browser context must exist when emitting attach events");
|
||||
bc.devtools_target_info(&target_id)
|
||||
.expect("attached service worker target must remain addressable")
|
||||
};
|
||||
let event_plan = conn.commit_prepared_attach_event_plan(PreparedTargetAttach::new(
|
||||
&target_id,
|
||||
ti,
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
session_id,
|
||||
owner_session_id.map(str::to_owned),
|
||||
route,
|
||||
waiting_for_debugger,
|
||||
)],
|
||||
));
|
||||
for prepared in attached_service_worker_targets {
|
||||
let event_plan = conn.commit_prepared_attach_event_plan(prepared);
|
||||
out.extend_background_events(event_plan);
|
||||
}
|
||||
continue;
|
||||
|
||||
@@ -67,7 +67,9 @@ pub(crate) use dedicated_worker_retirement::{
|
||||
WorkerRetirementFailureCleanup,
|
||||
};
|
||||
mod shared_worker_retirement;
|
||||
pub(crate) use shared_worker_retirement::PreparedSharedWorkerRetirement;
|
||||
pub(crate) use shared_worker_retirement::{
|
||||
PreparedSharedWorkerRetirement, RemovedSharedWorkerAttachment,
|
||||
};
|
||||
mod shared_worker;
|
||||
use shared_worker::*;
|
||||
mod service_worker_targets;
|
||||
|
||||
@@ -188,6 +188,9 @@ pub(super) fn record_dedicated_worker_main_script(
|
||||
},
|
||||
waiting_for_debugger,
|
||||
);
|
||||
let attachment = conn
|
||||
.worker_protocol_attachment_identity_for_route(prepared_session.route(), &session_id)
|
||||
.expect("prepared worker attach captures its original renderer attachment");
|
||||
if waiting_for_debugger
|
||||
&& let Some(context) = conn.browser_context_by_id_mut(browser_context_id)
|
||||
&& let Some(target) = context
|
||||
@@ -205,7 +208,8 @@ pub(super) fn record_dedicated_worker_main_script(
|
||||
target_id.clone(),
|
||||
target_info,
|
||||
[prepared_session],
|
||||
),
|
||||
)
|
||||
.with_worker_attachment(attachment),
|
||||
});
|
||||
}
|
||||
let changed_snapshot = should_emit_info_changed
|
||||
|
||||
@@ -98,8 +98,6 @@ pub(super) fn register_service_worker_target_with_active_run(
|
||||
},
|
||||
waiting_for_debugger,
|
||||
);
|
||||
let prepared_attach =
|
||||
PreparedTargetAttach::new(target_id.clone(), target_info, [prepared_session]);
|
||||
let attachment = conn
|
||||
.browser_context_by_id(browser_context_id)
|
||||
.and_then(|context| context.service_worker_target(&target_id))
|
||||
@@ -107,6 +105,13 @@ pub(super) fn register_service_worker_target_with_active_run(
|
||||
target.protocol_attachment_identity(browser_context_id, &session_id)
|
||||
})
|
||||
.expect("auto-attached service-worker session must own an exact attachment");
|
||||
let prepared_attach =
|
||||
PreparedTargetAttach::new(target_id.clone(), target_info, [prepared_session])
|
||||
.with_worker_attachment(
|
||||
crate::conn::TargetWorkerProtocolAttachmentIdentity::Service(
|
||||
attachment.clone(),
|
||||
),
|
||||
);
|
||||
attached_outputs.push((attachment, prepared_attach));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,10 +83,14 @@ pub(super) fn register_shared_worker_target(
|
||||
),
|
||||
"shared-worker auto-attach must freeze its exact target route at capture"
|
||||
);
|
||||
attached_outputs.push((
|
||||
attachment,
|
||||
PreparedTargetAttach::new(target_id.clone(), target_info, [prepared_session]),
|
||||
));
|
||||
let prepared_attach =
|
||||
PreparedTargetAttach::new(target_id.clone(), target_info, [prepared_session])
|
||||
.with_worker_attachment(
|
||||
crate::conn::TargetWorkerProtocolAttachmentIdentity::SharedOrDedicated(
|
||||
attachment.clone(),
|
||||
),
|
||||
);
|
||||
attached_outputs.push((attachment, prepared_attach));
|
||||
}
|
||||
}
|
||||
if let Some(target_info) = created_snapshot {
|
||||
|
||||
@@ -17,9 +17,10 @@ pub(crate) struct PreparedSharedWorkerRetirement {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RemovedSharedWorkerAttachment {
|
||||
pub(crate) struct RemovedSharedWorkerAttachment {
|
||||
retirement: TargetSharedWorkerProtocolAttachmentRetirement,
|
||||
cleanup: Option<PreparedRemovedWorkerSession>,
|
||||
cleanup: Rc<RefCell<Option<PreparedRemovedWorkerSession>>>,
|
||||
captured: Option<TargetSessionDetachCleanupPlan>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -72,17 +73,27 @@ impl PreparedSharedWorkerRetirement {
|
||||
cleanup_plan,
|
||||
} => {
|
||||
if retirement.is_current() {
|
||||
let cleanup =
|
||||
let (cleanup, captured) = if conn
|
||||
.session_route(Some(cleanup_plan.session_id()))
|
||||
.is_none()
|
||||
{
|
||||
// The frozen attach may still be held by its original
|
||||
// Runtime output barrier. Its native scope, rather
|
||||
// than a later literal SID, identifies any late binding.
|
||||
(None, Some(cleanup_plan))
|
||||
} else {
|
||||
match PreparedRemovedWorkerSession::prepare(conn, cleanup_plan) {
|
||||
Ok(cleanup) => Some(cleanup),
|
||||
Ok(cleanup) => (Some(cleanup), None),
|
||||
Err(error) => {
|
||||
prepared.preparation_error.get_or_insert(error);
|
||||
None
|
||||
(None, None)
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
prepared.sessions.push(RemovedSharedWorkerAttachment {
|
||||
retirement,
|
||||
cleanup,
|
||||
cleanup: Rc::new(RefCell::new(cleanup)),
|
||||
captured,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -170,6 +181,32 @@ impl PreparedSharedWorkerRetirement {
|
||||
}
|
||||
}
|
||||
|
||||
impl RemovedSharedWorkerAttachment {
|
||||
pub(crate) fn prepare_captured(&self, conn: &mut CdpConnection) -> Result<()> {
|
||||
if self.cleanup.borrow().is_some() || !self.retirement.is_current() {
|
||||
return Ok(());
|
||||
}
|
||||
let Some(plan) = self.captured.as_ref() else {
|
||||
return Ok(());
|
||||
};
|
||||
let original = crate::conn::TargetWorkerProtocolAttachmentIdentity::SharedOrDedicated(
|
||||
self.retirement.identity().clone(),
|
||||
);
|
||||
if conn.committed_worker_protocol_attachment_identity(plan.session_id()) == Some(&original)
|
||||
{
|
||||
let cleanup = PreparedRemovedWorkerSession::prepare(conn, plan.clone())?;
|
||||
// Register the owned native wait in the original recovery receipt
|
||||
// before this finite operation can lose its reply.
|
||||
*self.cleanup.borrow_mut() = Some(cleanup);
|
||||
} else {
|
||||
// No original binding remains. Retire the native authority in the
|
||||
// same turn; a held attach cannot create a new phantom afterward.
|
||||
self.retirement.clone().retire();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl SharedWorkerRetirementCompletion {
|
||||
async fn run(
|
||||
self,
|
||||
@@ -177,15 +214,22 @@ impl SharedWorkerRetirementCompletion {
|
||||
context: &mut crate::conn::CommandWorkflowContext,
|
||||
) -> Result<()> {
|
||||
for session in self.sessions {
|
||||
if let Some(cleanup) = &session.cleanup {
|
||||
if session.captured.is_some() {
|
||||
let preparation = session.clone();
|
||||
context
|
||||
.with_state(driver, move |state, _| {
|
||||
state.prepare_removed_shared_worker_attachment(preparation)
|
||||
})
|
||||
.await??;
|
||||
}
|
||||
let cleanup = session.cleanup.borrow().clone();
|
||||
if let Some(cleanup) = &cleanup {
|
||||
cleanup.wait().await;
|
||||
}
|
||||
context
|
||||
.with_state(driver, move |state, context| {
|
||||
let events = state.finish_removed_shared_worker_attachment(
|
||||
session.cleanup,
|
||||
session.retirement,
|
||||
);
|
||||
let events =
|
||||
state.finish_removed_shared_worker_attachment(cleanup, session.retirement);
|
||||
// SharedWorker removal has always exposed its complete
|
||||
// retirement prefix before the close acknowledgement.
|
||||
context.context_mut().protocol_events_mut().extend(events);
|
||||
@@ -262,6 +306,27 @@ mod tests {
|
||||
conn.start_parsed_command_dispatch_with_context(&command, context)
|
||||
}
|
||||
|
||||
fn capture_peer_attach(conn: &CdpConnection) -> PreparedTargetAttach {
|
||||
let route = CdpSessionRoute::SharedWorkerTarget {
|
||||
browser_context_id: "BID-shared-retirement".into(),
|
||||
target_id: TARGET.into(),
|
||||
};
|
||||
let attachment = conn
|
||||
.worker_protocol_attachment_identity_for_route(&route, PEER)
|
||||
.unwrap();
|
||||
PreparedTargetAttach::new(
|
||||
TARGET,
|
||||
conn.devtools_target_info(TARGET).unwrap(),
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
PEER.to_owned(),
|
||||
None,
|
||||
route,
|
||||
false,
|
||||
)],
|
||||
)
|
||||
.with_worker_attachment(attachment)
|
||||
}
|
||||
|
||||
async fn drive(
|
||||
conn: &mut CdpConnection,
|
||||
requests: &mut ProtocolStateRequestReceiver,
|
||||
@@ -509,6 +574,247 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn shared_worker_close_retires_an_original_attach_committed_after_admission() {
|
||||
let mut conn = fixture();
|
||||
let attachment = conn
|
||||
.shared_worker_target_for_session(Some(PEER))
|
||||
.unwrap()
|
||||
.protocol_attachment_identity("BID-shared-retirement", PEER)
|
||||
.unwrap();
|
||||
let info = conn.devtools_target_info(TARGET).unwrap();
|
||||
let prepared_attach = PreparedTargetAttach::new(
|
||||
TARGET,
|
||||
info,
|
||||
[TargetAttachSessionCommit::auto_attached(
|
||||
PEER.to_owned(),
|
||||
None,
|
||||
CdpSessionRoute::SharedWorkerTarget {
|
||||
browser_context_id: "BID-shared-retirement".into(),
|
||||
target_id: TARGET.into(),
|
||||
},
|
||||
false,
|
||||
)],
|
||||
);
|
||||
let prepared_attach = prepared_attach.with_worker_attachment(
|
||||
crate::conn::TargetWorkerProtocolAttachmentIdentity::SharedOrDedicated(
|
||||
attachment.clone(),
|
||||
),
|
||||
);
|
||||
conn.rollback_attached_session_without_event(PEER);
|
||||
assert!(conn.session_route(Some(PEER)).is_none());
|
||||
assert!(
|
||||
attachment.is_current(),
|
||||
"capture still owns its original target attachment"
|
||||
);
|
||||
let mut requests = conn.take_protocol_state_requests().unwrap();
|
||||
let mut context = CommandDispatchContext::default();
|
||||
let CdpCommandTaskStep::Pending(pending) = start(
|
||||
&mut conn,
|
||||
1001,
|
||||
"Target.closeTarget",
|
||||
json!({"targetId":TARGET}),
|
||||
&mut context,
|
||||
) else {
|
||||
panic!("close owns its cleanup outside the actor");
|
||||
};
|
||||
// The real held attach can be published while native cleanup waits.
|
||||
// Its retirement scope is still current, even though the target has
|
||||
// already been removed from the live inventory.
|
||||
assert!(attachment.is_current());
|
||||
let attached = conn
|
||||
.commit_prepared_attach_event_plan(prepared_attach)
|
||||
.into_background_events();
|
||||
assert!(
|
||||
attached
|
||||
.iter()
|
||||
.any(|event| event.clone().into_protocol_message()["method"]
|
||||
== "Target.attachedToTarget")
|
||||
);
|
||||
let original_binding = conn.session_binding_lifetime(PEER).unwrap();
|
||||
let completed = drive(&mut conn, &mut requests, *pending).await;
|
||||
let mut step = conn
|
||||
.try_complete_ready_command_dispatch_with_context(completed, &mut context)
|
||||
.unwrap_or_else(|_| panic!("finite retirement completion"));
|
||||
let messages = loop {
|
||||
match step {
|
||||
CdpCommandTaskStep::Pending(pending) => {
|
||||
let completed = drive(&mut conn, &mut requests, *pending).await;
|
||||
step = conn
|
||||
.try_complete_ready_command_dispatch_with_context(completed, &mut context)
|
||||
.unwrap_or_else(|_| panic!("finite recovery completion"));
|
||||
}
|
||||
CdpCommandTaskStep::Complete(outcome) => {
|
||||
let (events, _, _) = outcome.into_protocol_event_parts();
|
||||
break events
|
||||
.into_iter()
|
||||
.map(BackgroundProtocolEvent::into_protocol_message)
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
}
|
||||
};
|
||||
assert!(
|
||||
conn.session_route(Some(PEER)).is_none(),
|
||||
"the original late attachment must not survive target retirement"
|
||||
);
|
||||
assert!(original_binding.is_closed());
|
||||
assert!(!attachment.is_current());
|
||||
assert_eq!(
|
||||
messages
|
||||
.iter()
|
||||
.filter(|message| message["method"] == "Target.detachedFromTarget"
|
||||
&& message["params"]["sessionId"] == PEER)
|
||||
.count(),
|
||||
1,
|
||||
"{messages:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
messages
|
||||
.iter()
|
||||
.find(|message| message["id"] == 1001)
|
||||
.unwrap()["result"]["success"],
|
||||
true,
|
||||
"a captured attach is not an invalid-session close failure: {messages:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn shared_worker_close_retires_captured_authority_without_closing_its_successor() {
|
||||
for rebind in [false, true] {
|
||||
let mut conn = fixture();
|
||||
let prepared = capture_peer_attach(&conn);
|
||||
let original = conn
|
||||
.shared_worker_target_for_session(Some(PEER))
|
||||
.unwrap()
|
||||
.protocol_attachment_identity("BID-shared-retirement", PEER)
|
||||
.unwrap();
|
||||
conn.rollback_attached_session_without_event(PEER);
|
||||
let mut requests = conn.take_protocol_state_requests().unwrap();
|
||||
let mut context = CommandDispatchContext::default();
|
||||
let CdpCommandTaskStep::Pending(close) = start(
|
||||
&mut conn,
|
||||
1101,
|
||||
"Target.closeTarget",
|
||||
json!({"targetId":TARGET}),
|
||||
&mut context,
|
||||
) else {
|
||||
panic!("retirement owns the captured attachment");
|
||||
};
|
||||
let successor = if rebind {
|
||||
// Reuse all literal routing ids, but create a new native scope.
|
||||
let mut worker = SharedWorkerTargetState::new(
|
||||
moli_core::RendererOwnerLocalHostId::new_for_testing(107),
|
||||
SharedWorkerInstanceId::from_u64(307),
|
||||
TARGET.into(),
|
||||
Some("TID-owner".into()),
|
||||
String::new(),
|
||||
String::new(),
|
||||
);
|
||||
worker.attach_session(PEER.into());
|
||||
conn.browser_context_by_id_mut("BID-shared-retirement")
|
||||
.unwrap()
|
||||
.insert_shared_worker_target(worker);
|
||||
let prepared = capture_peer_attach(&conn);
|
||||
let _ = conn.commit_prepared_attach_event_plan(prepared);
|
||||
Some((
|
||||
conn.committed_worker_protocol_attachment_identity(PEER)
|
||||
.unwrap()
|
||||
.clone(),
|
||||
conn.session_binding_lifetime(PEER).unwrap(),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let completed = drive(&mut conn, &mut requests, *close).await;
|
||||
let messages = finish(&mut conn, completed, &mut context);
|
||||
assert_eq!(
|
||||
messages
|
||||
.iter()
|
||||
.find(|message| message["id"] == 1101)
|
||||
.unwrap()["result"]["success"],
|
||||
true,
|
||||
"{messages:?}"
|
||||
);
|
||||
assert!(!original.is_current());
|
||||
assert!(
|
||||
conn.commit_prepared_attach_event_plan(prepared)
|
||||
.into_background_events()
|
||||
.is_empty(),
|
||||
"an expired captured attach cannot create a phantom or overwrite a successor"
|
||||
);
|
||||
if let Some((attachment, lifetime)) = successor {
|
||||
assert!(attachment.is_current());
|
||||
assert!(!lifetime.is_closed() && !conn.session_is_closing(PEER));
|
||||
assert_eq!(
|
||||
conn.committed_worker_protocol_attachment_identity(PEER),
|
||||
Some(&attachment)
|
||||
);
|
||||
} else {
|
||||
assert!(conn.session_route(Some(PEER)).is_none());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn shared_worker_late_attach_cleanup_retains_its_wait_after_owner_reply_loss() {
|
||||
let mut conn = fixture();
|
||||
let prepared = capture_peer_attach(&conn);
|
||||
conn.rollback_attached_session_without_event(PEER);
|
||||
let mut requests = conn.take_protocol_state_requests().unwrap();
|
||||
let mut context = CommandDispatchContext::default();
|
||||
let CdpCommandTaskStep::Pending(close) = start(
|
||||
&mut conn,
|
||||
1201,
|
||||
"Target.closeTarget",
|
||||
json!({"targetId":TARGET}),
|
||||
&mut context,
|
||||
) else {
|
||||
panic!("close owns late attachment cleanup");
|
||||
};
|
||||
let _ = conn.commit_prepared_attach_event_plan(prepared);
|
||||
let binding = conn.session_binding_lifetime(PEER).unwrap();
|
||||
let mut close = Box::pin(close.wait());
|
||||
for _ in 0..2 {
|
||||
assert!(close.as_mut().now_or_never().is_none());
|
||||
requests.recv().await.unwrap().apply(&mut conn);
|
||||
}
|
||||
assert!(conn.session_route(Some(SESSION)).is_none());
|
||||
assert!(conn.session_is_closing(PEER) && !binding.is_closed());
|
||||
assert!(close.as_mut().now_or_never().is_none());
|
||||
drop(requests.recv().await.unwrap());
|
||||
let completed = tokio::time::timeout(std::time::Duration::from_secs(10), close)
|
||||
.await
|
||||
.unwrap();
|
||||
let CdpCommandTaskStep::Pending(recovery) = conn
|
||||
.try_complete_ready_command_dispatch_with_context(completed, &mut context)
|
||||
.unwrap_or_else(|_| panic!("finite rejected completion"))
|
||||
else {
|
||||
panic!("cleanup remains owned by the original command");
|
||||
};
|
||||
let completed = drive(&mut conn, &mut requests, *recovery).await;
|
||||
let messages = finish(&mut conn, completed, &mut context);
|
||||
assert!(binding.is_closed());
|
||||
assert!(conn.session_route(Some(PEER)).is_none());
|
||||
assert_eq!(
|
||||
messages
|
||||
.iter()
|
||||
.find(|message| message["id"] == 1201)
|
||||
.unwrap()["error"]["message"],
|
||||
"protocol owner closed: channel closed"
|
||||
);
|
||||
for session in [SESSION, PEER] {
|
||||
assert_eq!(
|
||||
messages
|
||||
.iter()
|
||||
.filter(|message| message["method"] == "Target.detachedFromTarget"
|
||||
&& message["params"]["sessionId"] == session)
|
||||
.count(),
|
||||
1,
|
||||
"{messages:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn shared_worker_close_uses_direct_with_state_while_original_cleanup_waits() {
|
||||
let owner = crate::conn::ProtocolStateOwner::new(fixture());
|
||||
@@ -605,6 +911,8 @@ mod tests {
|
||||
.unwrap()
|
||||
});
|
||||
let _ = conn.rollback_attached_session_without_event(PEER);
|
||||
conn.register_session_route_for_test(PEER, CdpSessionRoute::Browser);
|
||||
let successor = conn.session_binding_lifetime(PEER).unwrap();
|
||||
let mut requests = conn.take_protocol_state_requests().unwrap();
|
||||
let mut context = CommandDispatchContext::default();
|
||||
let CdpCommandTaskStep::Pending(pending) = start(
|
||||
@@ -649,9 +957,14 @@ mod tests {
|
||||
.iter()
|
||||
.find(|message| message["id"] == 801)
|
||||
.unwrap()["error"]["message"],
|
||||
"InvalidSessionId"
|
||||
"UnknownTargetId"
|
||||
);
|
||||
assert!(conn.session_route(Some(SESSION)).is_none());
|
||||
assert_eq!(
|
||||
conn.session_route(Some(PEER)),
|
||||
Some(CdpSessionRoute::Browser)
|
||||
);
|
||||
assert!(!successor.is_closed() && !conn.session_is_closing(PEER));
|
||||
assert!(identities.iter().all(|identity| !identity.is_current()));
|
||||
assert_eq!(
|
||||
messages
|
||||
|
||||
Reference in New Issue
Block a user