Skip to content

Commit c2933b2

Browse files
committed
fix remote control re-enable reconnect
1 parent b89ce9a commit c2933b2

4 files changed

Lines changed: 241 additions & 11 deletions

File tree

‎codex-rs/app-server-transport/src/transport/remote_control/mod.rs‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub(super) struct QueuedServerEnvelope {
7373
#[derive(Clone)]
7474
pub struct RemoteControlHandle {
7575
enabled_tx: Arc<watch::Sender<bool>>,
76+
reconnect_tx: Arc<watch::Sender<u64>>,
7677
status_tx: Arc<watch::Sender<RemoteControlStatusChangedNotification>>,
7778
state_db_available: bool,
7879
state_db: Option<Arc<StateRuntime>>,
@@ -189,13 +190,20 @@ impl RemoteControlHandle {
189190
server_name = %status.server_name,
190191
"remote control enable requested"
191192
);
192-
if matches!(
193-
status.status,
194-
RemoteControlConnectionStatus::Connected | RemoteControlConnectionStatus::Connecting
195-
) {
193+
if matches!(status.status, RemoteControlConnectionStatus::Connecting) {
196194
return Ok(status);
197195
}
198196

197+
if status.status == RemoteControlConnectionStatus::Connected {
198+
let next_status = self.publish_status(RemoteControlConnectionStatus::Connecting);
199+
let reconnect_generation = {
200+
let current_generation = *self.reconnect_tx.borrow();
201+
current_generation.saturating_add(1)
202+
};
203+
let _ = self.reconnect_tx.send(reconnect_generation);
204+
return Ok(next_status);
205+
}
206+
199207
Ok(self.publish_status(RemoteControlConnectionStatus::Connecting))
200208
}
201209

@@ -777,6 +785,7 @@ pub async fn start_remote_control(
777785
};
778786

779787
let (enabled_tx, enabled_rx) = watch::channel(initial_enabled);
788+
let (reconnect_tx, reconnect_rx) = watch::channel(0);
780789
let current_enrollment = Arc::new(RemoteControlEnrollmentState::new(/*enrollment*/ None));
781790
let websocket_current_enrollment = current_enrollment.clone();
782791
let pairing_persistence_key_required = app_server_client_name_rx.is_some();
@@ -837,6 +846,7 @@ pub async fn start_remote_control(
837846
},
838847
shutdown_token,
839848
enabled_rx,
849+
reconnect_rx,
840850
)
841851
.run(app_server_client_name_rx);
842852
match AssertUnwindSafe(websocket_task).catch_unwind().await {
@@ -876,6 +886,7 @@ pub async fn start_remote_control(
876886
join_handle,
877887
RemoteControlHandle {
878888
enabled_tx: Arc::new(enabled_tx),
889+
reconnect_tx: Arc::new(reconnect_tx),
879890
status_tx: Arc::new(status_tx),
880891
state_db_available,
881892
state_db: handle_state_db,

‎codex-rs/app-server-transport/src/transport/remote_control/tests.rs‎

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ fn remote_control_handle_with_current_enrollment(
165165
)));
166166
RemoteControlHandle {
167167
enabled_tx: Arc::new(enabled_tx),
168+
reconnect_tx: Arc::new(watch::channel(0).0),
168169
status_tx: Arc::new(status_tx),
169170
state_db_available: true,
170171
state_db: None,
@@ -943,6 +944,184 @@ async fn remote_control_handle_enable_disable_stops_and_restarts_connections() {
943944
let _ = remote_task.await;
944945
}
945946

947+
#[tokio::test]
948+
async fn remote_control_enable_reconnects_when_already_connected() {
949+
let listener = TcpListener::bind("127.0.0.1:0")
950+
.await
951+
.expect("listener should bind");
952+
let remote_control_url = remote_control_url_for_listener(&listener);
953+
let codex_home = TempDir::new().expect("temp dir should create");
954+
let (transport_event_tx, _transport_event_rx) =
955+
mpsc::channel::<TransportEvent>(CHANNEL_CAPACITY);
956+
let shutdown_token = CancellationToken::new();
957+
let (remote_task, remote_handle) = start_remote_control(
958+
RemoteControlStartConfig {
959+
remote_control_url,
960+
installation_id: TEST_INSTALLATION_ID.to_string(),
961+
},
962+
Some(remote_control_state_runtime(&codex_home).await),
963+
remote_control_auth_manager(),
964+
transport_event_tx,
965+
shutdown_token.clone(),
966+
/*app_server_client_name_rx*/ None,
967+
/*initial_enabled*/ true,
968+
)
969+
.await
970+
.expect("remote control should start");
971+
let mut status_rx = remote_handle.status_receiver();
972+
973+
let enroll_request = accept_http_request(&listener).await;
974+
assert_eq!(
975+
enroll_request.request_line,
976+
"POST /backend-api/wham/remote/control/server/enroll HTTP/1.1"
977+
);
978+
respond_with_json(
979+
enroll_request.stream,
980+
remote_control_server_token_response(
981+
"srv_e_test",
982+
"env_test",
983+
TEST_REMOTE_CONTROL_SERVER_TOKEN,
984+
),
985+
)
986+
.await;
987+
let mut first_websocket = accept_remote_control_connection(&listener).await;
988+
expect_remote_control_status_snapshot(
989+
&mut status_rx,
990+
RemoteControlStatusChangedNotification {
991+
status: RemoteControlConnectionStatus::Connected,
992+
server_name: test_server_name(),
993+
installation_id: TEST_INSTALLATION_ID.to_string(),
994+
environment_id: Some("env_test".to_string()),
995+
},
996+
)
997+
.await;
998+
assert_eq!(
999+
remote_handle.enable().expect("enable should succeed"),
1000+
RemoteControlStatusChangedNotification {
1001+
status: RemoteControlConnectionStatus::Connecting,
1002+
server_name: test_server_name(),
1003+
installation_id: TEST_INSTALLATION_ID.to_string(),
1004+
environment_id: Some("env_test".to_string()),
1005+
}
1006+
);
1007+
let mut second_websocket = timeout(
1008+
Duration::from_secs(1),
1009+
accept_remote_control_connection(&listener),
1010+
)
1011+
.await
1012+
.expect("re-enabling connected remote control should reconnect");
1013+
let first_websocket_message = timeout(Duration::from_secs(1), first_websocket.next())
1014+
.await
1015+
.expect("re-enabling connected remote control should close the websocket");
1016+
assert!(
1017+
matches!(
1018+
first_websocket_message,
1019+
Some(Ok(tungstenite::Message::Close(_)))
1020+
),
1021+
"expected first websocket to receive close frame, got {first_websocket_message:?}"
1022+
);
1023+
expect_remote_control_status(
1024+
&mut status_rx,
1025+
/*expected_status*/ None,
1026+
Some("env_test"),
1027+
)
1028+
.await;
1029+
second_websocket
1030+
.close(None)
1031+
.await
1032+
.expect("second websocket should close");
1033+
1034+
shutdown_token.cancel();
1035+
timeout(Duration::from_secs(1), remote_task)
1036+
.await
1037+
.expect("remote control task should stop after shutdown")
1038+
.expect("remote control task should join");
1039+
}
1040+
1041+
#[tokio::test]
1042+
async fn remote_control_enable_reconnect_does_not_replay_after_disconnect_race() {
1043+
let listener = TcpListener::bind("127.0.0.1:0")
1044+
.await
1045+
.expect("listener should bind");
1046+
let remote_control_url = remote_control_url_for_listener(&listener);
1047+
let codex_home = TempDir::new().expect("temp dir should create");
1048+
let (transport_event_tx, _transport_event_rx) =
1049+
mpsc::channel::<TransportEvent>(CHANNEL_CAPACITY);
1050+
let shutdown_token = CancellationToken::new();
1051+
let (remote_task, remote_handle) = start_remote_control(
1052+
RemoteControlStartConfig {
1053+
remote_control_url,
1054+
installation_id: TEST_INSTALLATION_ID.to_string(),
1055+
},
1056+
Some(remote_control_state_runtime(&codex_home).await),
1057+
remote_control_auth_manager(),
1058+
transport_event_tx,
1059+
shutdown_token.clone(),
1060+
/*app_server_client_name_rx*/ None,
1061+
/*initial_enabled*/ true,
1062+
)
1063+
.await
1064+
.expect("remote control should start");
1065+
let mut status_rx = remote_handle.status_receiver();
1066+
1067+
let enroll_request = accept_http_request(&listener).await;
1068+
respond_with_json(
1069+
enroll_request.stream,
1070+
remote_control_server_token_response(
1071+
"srv_e_test",
1072+
"env_test",
1073+
TEST_REMOTE_CONTROL_SERVER_TOKEN,
1074+
),
1075+
)
1076+
.await;
1077+
let mut first_websocket = accept_remote_control_connection(&listener).await;
1078+
expect_remote_control_status_snapshot(
1079+
&mut status_rx,
1080+
RemoteControlStatusChangedNotification {
1081+
status: RemoteControlConnectionStatus::Connected,
1082+
server_name: test_server_name(),
1083+
installation_id: TEST_INSTALLATION_ID.to_string(),
1084+
environment_id: Some("env_test".to_string()),
1085+
},
1086+
)
1087+
.await;
1088+
1089+
first_websocket
1090+
.close(None)
1091+
.await
1092+
.expect("first websocket should close");
1093+
assert_eq!(
1094+
remote_handle.enable().expect("enable should succeed"),
1095+
RemoteControlStatusChangedNotification {
1096+
status: RemoteControlConnectionStatus::Connecting,
1097+
server_name: test_server_name(),
1098+
installation_id: TEST_INSTALLATION_ID.to_string(),
1099+
environment_id: Some("env_test".to_string()),
1100+
}
1101+
);
1102+
1103+
let mut second_websocket = accept_remote_control_connection(&listener).await;
1104+
expect_remote_control_status(
1105+
&mut status_rx,
1106+
/*expected_status*/ None,
1107+
Some("env_test"),
1108+
)
1109+
.await;
1110+
timeout(Duration::from_millis(100), listener.accept())
1111+
.await
1112+
.expect_err("stale reconnect request should not trigger a third connection");
1113+
second_websocket
1114+
.close(None)
1115+
.await
1116+
.expect("second websocket should close");
1117+
1118+
shutdown_token.cancel();
1119+
timeout(Duration::from_secs(1), remote_task)
1120+
.await
1121+
.expect("remote control task should stop after shutdown")
1122+
.expect("remote control task should join");
1123+
}
1124+
9461125
#[tokio::test]
9471126
async fn remote_control_transport_clears_outgoing_buffer_when_backend_acks() {
9481127
let listener = TcpListener::bind("127.0.0.1:0")

‎codex-rs/app-server-transport/src/transport/remote_control/tests/clients_tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn client_management_handle(
2222
});
2323
RemoteControlHandle {
2424
enabled_tx: Arc::new(enabled_tx),
25+
reconnect_tx: Arc::new(watch::channel(0).0),
2526
status_tx: Arc::new(status_tx),
2627
state_db_available: false,
2728
state_db: None,

0 commit comments

Comments
 (0)