Skip to content

Commit 629c56b

Browse files
committed
fix(openai): retain warm websocket through cancellation recovery
1 parent fa9b486 commit 629c56b

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎crates/jcode-provider-openai-runtime/src/openai_tests/persistent_cancellation.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,14 @@ async fn persistent_cancellation_public_drop_releases_chain_and_reconnects() {
417417
.expect("fixture must accept the request");
418418
tokio::task::yield_now().await;
419419
drop(stream);
420-
tokio::time::timeout(Duration::from_secs(1), async {
420+
tokio::time::timeout(Duration::from_secs(12), async {
421421
assert!(
422422
provider.persistent_ws.lock().await.is_none(),
423423
"cancelled response must be invalidated before mutex handoff"
424424
);
425425
})
426426
.await
427-
.expect("dropping the consumer must release the persistent lock promptly");
427+
.expect("dropping the consumer must release the persistent lock after bounded recovery");
428428
tokio::time::timeout(Duration::from_secs(2), async {
429429
let mut next = provider
430430
.complete(&messages, &[], "fixture", None)
@@ -515,7 +515,7 @@ async fn persistent_cancellation_aborted_future_invalidates_before_handoff() {
515515
tokio::task::yield_now().await;
516516
task.abort();
517517
assert!(matches!(task.await, Err(error) if error.is_cancelled()));
518-
tokio::time::timeout(Duration::from_secs(1), waiter)
518+
tokio::time::timeout(Duration::from_secs(12), waiter)
519519
.await
520520
.unwrap()
521521
.unwrap();

‎crates/jcode-provider-openai-runtime/src/openai_ws_cancel_recovery.rs‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
//! Recover transport affinity, never the response chain, after an interrupted turn.
22
use super::*;
33

4+
// This is background recovery, not the latency of acknowledging Escape. A
5+
// subsecond deadline discards healthy sockets before ordinary generations can
6+
// finish, losing cache affinity on the next turn. Bound the wait for a successor
7+
// without assuming the provider will finish within a single network round trip.
8+
const CANCEL_DRAIN_TIMEOUT: Duration = Duration::from_secs(10);
9+
410
#[derive(Clone, Copy, PartialEq, Eq)]
511
pub(super) enum RecoveryPhase {
612
Idle,
@@ -52,7 +58,7 @@ impl Drop for RecoverableAttempt {
5258
return;
5359
};
5460
let completed = self.phase == RecoveryPhase::Completed;
55-
let deadline = tokio::time::Instant::now() + Duration::from_millis(500);
61+
let deadline = tokio::time::Instant::now() + CANCEL_DRAIN_TIMEOUT;
5662
runtime.spawn(async move {
5763
// Absolute deadline plus frame/byte budgets: activity cannot keep a
5864
// cancelled generation alive indefinitely. No response events are
@@ -77,7 +83,10 @@ impl Drop for RecoverableAttempt {
7783
vec![
7884
("transport_retained", recovered.to_string()),
7985
("response_chain_retained", "false".to_string()),
80-
("drain_timeout_ms", "500".to_string()),
86+
(
87+
"drain_timeout_ms",
88+
CANCEL_DRAIN_TIMEOUT.as_millis().to_string(),
89+
),
8190
],
8291
);
8392
});
@@ -277,6 +286,36 @@ mod tests {
277286
server.await.unwrap();
278287
}
279288

289+
#[tokio::test]
290+
async fn cancellation_retains_affinity_when_completion_takes_over_half_a_second() {
291+
let _env_lock = jcode_base::storage::lock_test_env();
292+
let (state, mut peer) = pair().await;
293+
let connected_at = state.connected_at;
294+
let server = tokio::spawn(async move {
295+
tokio::time::sleep(Duration::from_millis(650)).await;
296+
peer.send(WsMessage::Text(
297+
serde_json::json!({"type":"response.completed"}).to_string(),
298+
))
299+
.await
300+
.unwrap();
301+
peer
302+
});
303+
let (attempt, slot) = attempt(state, RecoveryPhase::InFlight);
304+
drop(attempt);
305+
let guard = tokio::time::timeout(Duration::from_secs(2), slot.lock())
306+
.await
307+
.unwrap();
308+
let state = guard
309+
.as_ref()
310+
.expect("delayed completion retains warm socket");
311+
assert_eq!(state.connected_at, connected_at);
312+
assert_eq!(state.cache_key.as_deref(), Some("fixture-cache"));
313+
assert!(state.last_response_id.is_empty());
314+
assert!(state.last_response_output.is_empty());
315+
assert!(state.last_input_item_hashes.is_empty());
316+
server.await.unwrap();
317+
}
318+
280319
#[tokio::test]
281320
async fn cancellation_after_terminal_consumed_does_not_drain_next_response() {
282321
let _env_lock = jcode_base::storage::lock_test_env();

0 commit comments

Comments
 (0)