Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

fix: add callback to map before sending request to fix race condition (#3146)

Michael Bolinbd4fa8550781
diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs
index 287bfe8f43..2028c4240e 100644
--- a/codex-rs/core/src/codex.rs
+++ b/codex-rs/core/src/codex.rs
@@ -584,9 +584,19 @@ impl Session {
cwd: PathBuf,
reason: Option<String>,
) -> oneshot::Receiver<ReviewDecision> {
+ // Add the tx_approve callback to the map before sending the request.
let (tx_approve, rx_approve) = oneshot::channel();
+ let event_id = sub_id.clone();
+ let prev_entry = {
+ let mut state = self.state.lock_unchecked();
+ state.pending_approvals.insert(sub_id, tx_approve)
+ };
+ if prev_entry.is_some() {
+ warn!("Overwriting existing pending approval for sub_id: {event_id}");
+ }
+
let event = Event {
- id: sub_id.clone(),
+ id: event_id,
msg: EventMsg::ExecApprovalRequest(ExecApprovalRequestEvent {
call_id,
command,
@@ -595,10 +605,6 @@ impl Session {
}),
};
let _ = self.tx_event.send(event).await;
- {
- let mut state = self.state.lock_unchecked();
- state.pending_approvals.insert(sub_id, tx_approve);
- }
rx_approve
}
@@ -610,9 +616,19 @@ impl Session {
reason: Option<String>,
grant_root: Option<PathBuf>,
) -> oneshot::Receiver<ReviewDecision> {
+ // Add the tx_approve callback to the map before sending the request.
let (tx_approve, rx_approve) = oneshot::channel();
+ let event_id = sub_id.clone();
+ let prev_entry = {
+ let mut state = self.state.lock_unchecked();
+ state.pending_approvals.insert(sub_id, tx_approve)
+ };
+ if prev_entry.is_some() {
+ warn!("Overwriting existing pending approval for sub_id: {event_id}");
+ }
+
let event = Event {
- id: sub_id.clone(),
+ id: event_id,
msg: EventMsg::ApplyPatchApprovalRequest(ApplyPatchApprovalRequestEvent {
call_id,
changes: convert_apply_patch_to_protocol(action),
@@ -621,10 +637,6 @@ impl Session {
}),
};
let _ = self.tx_event.send(event).await;
- {
- let mut state = self.state.lock_unchecked();
- state.pending_approvals.insert(sub_id, tx_approve);
- }
rx_approve
}