Skip to content

fix: race condition causing exec mode to hang with --image flag - #5814

Closed
jet-ds wants to merge 6 commits into
openai:mainfrom
jet-ds:fix/exec-image-race-condition
Closed

jet-ds wants to merge 6 commits into
openai:mainfrom
jet-ds:fix/exec-image-race-condition

Conversation

@jet-ds

@jet-ds jet-ds commented Oct 27, 2025 •

Copy link
Copy Markdown

What

Fixes a race condition and dropped events in codex exec when using the --image flag that causes the process to hang indefinitely.

Why

Race Condition: When images are submitted via codex exec --json --image <path>, two threads compete for the same event stream:

  1. Background thread (line 298): conversation.next_event() → forwards to channel rx
  2. Main thread (line 330): conversation.next_event() → waits for TaskComplete

Both consume from the same async_channel::Receiver (cloned via conversation.clone()). When the background thread wins the race (which happens deterministically), the main thread hangs forever.

Dropped Events: Additionally, even after fixing the race by reading from rx, events were not being processed during the wait. This silently dropped the assistant's response to image uploads.

Impact: Makes codex exec --image completely unusable. Affects any programmatic integration using images (CI/CD, API wrappers, etc.).

How

The fix has two parts:

  1. Changed line 330 to read from rx.recv() instead of conversation.next_event() (fixes the race)
  2. Added event_processor.process_event(event) inside the loop (fixes dropped events)

This ensures events are both consumed from the correct channel and properly processed.

Credit

Race condition diagnosed by @AdsQnn in #5773.
Dropped events issue caught by @etraut-openai during review.

Testing

  • ✅ cargo fmt --all -- --config imports_granularity=Item - passed
  • ✅ cargo clippy --tests - passed (no warnings)
  • ✅ cargo test --package codex-exec - all tests passed
  • ✅ Manual testing: codex exec --json --image <path> now completes successfully

Tested on macOS ARM (aarch64-apple-darwin).

Fixes #5773

When images are submitted in exec mode, two threads compete for events
from the same conversation stream: the background event-forwarding thread
and the main thread waiting for TaskComplete. This causes the main thread
to hang indefinitely when the background thread wins the race.

Fix by reading from the rx channel instead of calling conversation.next_event()
directly, ensuring all events flow through the background thread's channel.

Credit to @AdsQnn for diagnosing this issue in openai#5773.

Signed-off-by: Jet Sanchez <[email protected]>
@github-actions

github-actions Bot commented Oct 27, 2025 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@jet-ds

jet-ds commented Oct 27, 2025

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Oct 27, 2025
@jet-ds jet-ds changed the title Fix race condition causing exec mode to hang with --image flag fix: race condition causing exec mode to hang with --image flag Oct 27, 2025
@etraut-openai

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@etraut-openai

Copy link
Copy Markdown
Contributor

Thanks for the contribution!

I agree with your diagnosis, but I think the proposed fix has a problem. By switching the image-upload wait loop to rx.recv() the code consumes events from the same channel that feeds the main event-processing loop. Any events read here are removed from the channel and never delivered to event_processor.process_event. When a user supplies --images, the assistant’s response for that upload is now silently dropped.

@adsqx

adsqx commented Oct 27, 2025 •

Copy link
Copy Markdown

Right !

The Rust fix resolves a race condition, but it introduces a new problem.

❌ Incorrect fix:

while let Some(event) = rx.recv().await {
    if event.id == initial_images_event_id
        && matches!(event.msg, EventMsg::TaskComplete(..)) {
        break;
    }
    // ❌ The event disappears! It never reaches the event_processor
}

The main loop will never see these events:

while let Some(event) = rx.recv().await {
    event_processor.process_event(event);  // Won’t receive events from the loop above
}

Effect: All events between sending the images and TaskComplete (e.g. assistant responses about the images)
are lost — they never reach the event_processor.

Correct solution:

✅ Read from rx AND process every event

while let Some(event) = rx.recv().await {
    // Check if this is the completion event for images
    let is_images_complete = event.id == initial_images_event_id
        && matches!(event.msg, EventMsg::TaskComplete(..));

    // ALWAYS process the event (so nothing gets lost)
    event_processor.process_event(event);

    // If this was the TaskComplete for images, stop waiting
    if is_images_complete {
        break;
    }
}

Key difference: Every event MUST go through the event_processor,
even while waiting for a specific event — otherwise, user-facing output is lost.

Good point — it might help with something.
However, I’m only checking this occasionally, so it needs further analysis.
I’m not able to fully verify it right now :))
Maybe the author will consider committing it :)

What I posted earlier wasn’t meant as a complete, ready-to-deploy solution — it was more of a suggestion pointing to where the problem lies :)

@jet-ds

jet-ds commented Oct 27, 2025

Copy link
Copy Markdown
Author

Thanks both for catching that. You're right, I was consuming events without processing them. I'll update the fix to process events in the wait loop.

When images are submitted in exec mode, two threads compete for events
from the same conversation stream: the background event-forwarding thread
and the main thread waiting for TaskComplete. This causes the main thread
to hang indefinitely when the background thread wins the race.

Additionally, events consumed during the wait were not being processed,
silently dropping the assistant's response to image uploads.

Fix by reading from the rx channel (avoiding the race) and processing
each event before checking if it is the TaskComplete signal.

Credit to @AdsQnn for diagnosing the race condition in openai#5773.
Thanks to @etraut-openai for catching the dropped events issue.

Signed-off-by: Jet Sanchez <[email protected]>
@etraut-openai

Copy link
Copy Markdown
Contributor

Thanks, the logic looks good to me. Just need to fix some remaining lint errors.

@jet-ds

jet-ds commented Oct 28, 2025

Copy link
Copy Markdown
Author

Merged latest upstream/main which already included a fix for the content_items compilation error (d7b333b). All tests passing locally. :)

@etraut-openai

Copy link
Copy Markdown
Contributor

Closing this pull request because we decided to go with a different (simpler) solution.

@github-actions github-actions Bot locked and limited conversation to collaborators Oct 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

codex exec --json hangs indefinitely when --image flag is used

3 participants