From 5c1e27fc4705725d93a7edbed01adf4ae0c4751e Mon Sep 17 00:00:00 2001 From: Zyy Date: Mon, 30 Nov 2026 20:33:31 +0800 Subject: [PATCH] fix(plugins): account for peer queues when awaiting cancellation Use the existing queue timeout for cancellation confirmations so normal peer backpressure does not terminate healthy sessions. Cover delayed acknowledgements and bounded cleanup of unresponsive peers. Co-authored-by: Codex --- .../gateway-plugin/runtime/src/rpc/calls.rs | 3 +- .../runtime/src/rpc/contract.rs | 2 +- .../runtime/tests/rpc/session.rs | 72 ++++++++++++++++++- backend/crates/gateway-plugin/sdk/README.md | 2 +- 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/backend/crates/gateway-plugin/runtime/src/rpc/calls.rs b/backend/crates/gateway-plugin/runtime/src/rpc/calls.rs index f7bc2f19f..cf8d497ac 100644 --- a/backend/crates/gateway-plugin/runtime/src/rpc/calls.rs +++ b/backend/crates/gateway-plugin/runtime/src/rpc/calls.rs @@ -488,7 +488,8 @@ impl Shared { return; }; cancellation.transmitted = true; - cancellation.deadline = tokio::time::Instant::now() + Duration::from_millis(250); + // 写入管道不代表对端已处理,确认还需经过插件输入与输出队列 + cancellation.deadline = tokio::time::Instant::now() + self.maximum_queue_timeout; drop(state); self.cancellations_changed.notify_waiters(); } diff --git a/backend/crates/gateway-plugin/runtime/src/rpc/contract.rs b/backend/crates/gateway-plugin/runtime/src/rpc/contract.rs index a6cc9536a..716912e6d 100644 --- a/backend/crates/gateway-plugin/runtime/src/rpc/contract.rs +++ b/backend/crates/gateway-plugin/runtime/src/rpc/contract.rs @@ -14,7 +14,7 @@ pub struct RpcLimits { pub maximum_calls: usize, pub maximum_executing_calls: usize, pub maximum_queued_calls: usize, - /// 初次准入、恢复及取消控制帧发送排队各自使用此期限 + /// 初次准入、恢复、取消发送排队及确认等待各自使用此期限 pub maximum_queue_timeout: Duration, pub maximum_callbacks: usize, pub handshake_timeout: Duration, diff --git a/backend/crates/gateway-plugin/runtime/tests/rpc/session.rs b/backend/crates/gateway-plugin/runtime/tests/rpc/session.rs index 3428400a8..05cadfb92 100644 --- a/backend/crates/gateway-plugin/runtime/tests/rpc/session.rs +++ b/backend/crates/gateway-plugin/runtime/tests/rpc/session.rs @@ -895,9 +895,70 @@ async fn queued_cancellation_survives_backpressure_but_a_stalled_writer_is_bound } } +#[tokio::test] +async fn cancellation_confirmation_survives_peer_backpressure() { + let (cache, session) = session(Arc::new(Callbacks::default())).await; + let entered = cache.path().join("cancel-entered"); + let release = cache.path().join("cancel-release"); + let mut hanging = Box::pin(session.call( + "hang", + session.context(Stage::Request, Duration::from_secs(10)), + json!({}), + vec![], + )); + assert!(futures::poll!(&mut hanging).is_pending()); + session + .call( + "pause_next_frame", + session.context(Stage::Request, Duration::from_secs(10)), + json!({"entered": entered, "release": release}), + vec![], + ) + .await + .unwrap(); + drop(hanging); + tokio::time::timeout(Duration::from_secs(5), async { + while !entered.exists() { + tokio::time::sleep(Duration::from_millis(5)).await; + } + }) + .await + .unwrap(); + // 小控制帧已写入管道,但对端仍可能在处理此前排队的调用与确认 + tokio::time::sleep(Duration::from_millis(350)).await; + assert!( + session.is_ready(), + "pending confirmation closed the session" + ); + std::fs::write(release, b"ready").unwrap(); + let reply = session + .call( + "echo", + session.context(Stage::Request, Duration::from_secs(2)), + json!("after-cancel"), + vec![], + ) + .await + .unwrap(); + assert_eq!(reply.result, json!("after-cancel")); + session.shutdown(Duration::from_secs(1)).await; +} + #[tokio::test] async fn ignored_cancellation_closes_the_incarnation_and_settles_other_calls() { - let (_cache, session) = session(Arc::new(Callbacks::default())).await; + let (_cache, prepared, handshake, processes) = prepared_session(); + let session = RpcSession::start( + prepared, + handshake, + RpcLimits { + maximum_queue_timeout: Duration::from_millis(350), + ..Default::default() + }, + &processes, + Arc::new(Callbacks::default()), + ) + .await + .unwrap(); let first = session.call( "hang_uncancellable", session.context(Stage::Request, Duration::from_millis(100)), @@ -906,13 +967,18 @@ async fn ignored_cancellation_closes_the_incarnation_and_settles_other_calls() { ); let second = session.call( "hang", - session.context(Stage::Request, Duration::from_secs(2)), + session.context(Stage::Request, Duration::from_secs(10)), json!({}), vec![], ); - let (first, second) = tokio::join!(first, second); + let (first, second) = tokio::time::timeout(Duration::from_secs(3), async { + tokio::join!(first, second) + }) + .await + .expect("unacknowledged cancellation must close the session before other calls expire"); assert!(matches!(first, Err(RpcError::Timeout))); assert!(matches!(second, Err(RpcError::Timeout))); + assert!(!session.is_ready()); session.shutdown(Duration::from_secs(1)).await; } diff --git a/backend/crates/gateway-plugin/sdk/README.md b/backend/crates/gateway-plugin/sdk/README.md index 7c70ba885..c71d8a932 100644 --- a/backend/crates/gateway-plugin/sdk/README.md +++ b/backend/crates/gateway-plugin/sdk/README.md @@ -84,7 +84,7 @@ SDK 和宿主的 `maximum_callbacks` 默认 320、最大 512,统一限制全 取消或丢弃回调 future 会释放该回调;宿主不保证已发出的业务操作可以撤销,不自动重试或补偿。 已返回响应头的流继续占用总在途预算,不占首结果执行名额,由正文或连接的 owner 管理真实寿命 -取消控制帧的发送排队沿用宿主排队期限,确认期限从控制帧完整写出后开始计算。 +取消控制帧的发送排队与确认等待分别使用宿主排队期限,默认各 30 秒;确认期限从控制帧完整写出后开始计算。 发送排队或确认超时会终止该插件会话并回收在途调用,运行日志区分这两个阶段 实际容量由宿主预算与 SDK 会话配置共同限制,较小的插件侧上限会提前拒绝新调用。