fix(service): lift cached_host to outer loop to prevent duplicate helper pages

🤖 Generated with [Qoder][https://qoder.com]
This commit is contained in:
木炎
2026-04-14 09:15:33 +08:00
parent adb64429ee
commit 8decd9554c
2 changed files with 12 additions and 8 deletions

View File

@@ -7,6 +7,7 @@ use std::sync::Arc;
use tungstenite::accept;
use crate::agent::AgentRuntimeContext;
use crate::browser::callback_host::LiveBrowserCallbackHost;
use crate::pipe::PipeError;
use crate::security::MacPolicy;
@@ -14,7 +15,7 @@ const DEFAULT_BROWSER_WS_URL: &str = "ws://127.0.0.1:12345";
const DEFAULT_SERVICE_WS_LISTEN_ADDR: &str = "127.0.0.1:42321";
pub use protocol::{ClientMessage, ServiceMessage};
pub use server::{serve_client, ServiceEventSink, ServiceSession};
pub use server::{ServiceEventSink, ServiceSession};
pub(crate) mod browser_ws_client {
pub(crate) use super::server::{initial_request_url_for_submit_task, ServiceWsClient};
@@ -69,6 +70,11 @@ pub fn run() -> Result<(), PipeError> {
browser_ws_url,
);
// Cache the browser callback host across client sessions so the helper
// page tab is opened only once per process lifetime instead of once per
// WebSocket reconnection.
let mut cached_host: Option<Arc<LiveBrowserCallbackHost>> = None;
loop {
let (stream, _) = listener.accept()?;
let websocket = accept(stream)
@@ -76,12 +82,13 @@ pub fn run() -> Result<(), PipeError> {
let sink = Arc::new(ServiceEventSink::from_websocket(websocket));
match session.try_attach_client() {
Ok(()) => {
let result = serve_client(
let result = server::serve_client(
&runtime_context,
&session,
sink.clone(),
browser_ws_url,
&mac_policy,
&mut cached_host,
);
session.detach_client();
match result {

View File

@@ -229,17 +229,14 @@ fn send_status_changed(sink: &ServiceEventSink, state: &str) -> Result<(), PipeE
})
}
pub fn serve_client(
pub(crate) fn serve_client(
context: &AgentRuntimeContext,
session: &ServiceSession,
sink: Arc<ServiceEventSink>,
browser_ws_url: &str,
mac_policy: &MacPolicy,
cached_host: &mut Option<Arc<LiveBrowserCallbackHost>>,
) -> Result<(), PipeError> {
// Cache the browser callback host across tasks so the helper page tab is
// opened only once per client session instead of once per task.
let mut cached_host: Option<Arc<LiveBrowserCallbackHost>> = None;
loop {
let Some(message) = sink.recv_client_message()? else {
return Ok(());
@@ -293,7 +290,7 @@ pub fn serve_client(
false, // use_hidden_domain: visible tab for now
) {
Ok(host) => {
cached_host = Some(Arc::new(host));
*cached_host = Some(Arc::new(host));
}
Err(err) => {
session.finish_task();