Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions codex-rs/app-server-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1746,24 +1746,25 @@ mod tests {

#[tokio::test]
async fn remote_notifications_arrive_over_websocket() {
let (close_tx, close_rx) = tokio::sync::oneshot::channel();
let websocket_url = start_test_remote_server(|mut websocket| async move {
expect_remote_initialize(&mut websocket).await;
write_websocket_message(
&mut websocket,
JSONRPCMessage::Notification(
serde_json::from_value(
serde_json::to_value(ServerNotification::AccountUpdated(
AccountUpdatedNotification {
auth_mode: None,
plan_type: None,
},
))
.expect("notification should serialize"),
)
.expect("notification should convert to JSON-RPC"),
),
)
.await;
let notification = JSONRPCMessage::Notification(
serde_json::from_value(
serde_json::to_value(ServerNotification::AccountUpdated(
AccountUpdatedNotification {
auth_mode: None,
plan_type: None,
},
))
.expect("notification should serialize"),
)
.expect("notification should convert to JSON-RPC"),
);
// Only the first notification is consumed; the other can arrive during shutdown.
write_websocket_message(&mut websocket, notification.clone()).await;
write_websocket_message(&mut websocket, notification).await;
let _ = close_tx.send(websocket.next().await);
})
.await;
let mut client = RemoteAppServerClient::connect(test_remote_connect_args(websocket_url))
Expand All @@ -1778,6 +1779,14 @@ mod tests {
));

client.shutdown().await.expect("shutdown should complete");
assert_eq!(
close_rx
.await
.expect("server should observe shutdown")
.expect("server should receive a frame")
.expect("websocket should close cleanly"),
Message::Close(None),
);
}

#[tokio::test]
Expand Down
21 changes: 19 additions & 2 deletions codex-rs/app-server-client/src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,6 @@ impl RemoteAppServerClient {
worker_handle,
} = self;
let mut worker_handle = worker_handle;
drop(event_rx);
let (response_tx, response_rx) = oneshot::channel();
if command_tx
.send(RemoteClientCommand::Shutdown { response_tx })
Expand All @@ -619,6 +618,8 @@ impl RemoteAppServerClient {
worker_handle.abort();
let _ = worker_handle.await;
}
// Keep events deliverable until the worker has closed the websocket.
drop(event_rx);
Ok(())
}
}
Expand Down Expand Up @@ -1064,9 +1065,21 @@ mod tests {
#[tokio::test]
async fn shutdown_tolerates_worker_exit_after_command_is_queued() {
let (command_tx, mut command_rx) = mpsc::channel(1);
let (_event_tx, event_rx) = mpsc::unbounded_channel::<AppServerEvent>();
let (event_tx, event_rx) = mpsc::unbounded_channel::<AppServerEvent>();
let (delivered_tx, delivered_rx) = oneshot::channel();
let worker_handle = tokio::spawn(async move {
let _ = command_rx.recv().await;
// Force an event after shutdown is queued but before this worker has exited.
let result = deliver_event(
&event_tx,
AppServerEvent::ServerNotification(Box::new(ServerNotification::AccountUpdated(
codex_app_server_protocol::AccountUpdatedNotification {
auth_mode: None,
plan_type: None,
},
))),
);
let _ = delivered_tx.send(result);
});
let client = RemoteAppServerClient {
command_tx,
Expand All @@ -1080,5 +1093,9 @@ mod tests {
.shutdown()
.await
.expect("shutdown should complete when worker exits first");
delivered_rx
.await
.expect("worker should attempt delivery")
.expect("an in-flight event must not abort the worker during shutdown");
}
}
Loading