mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Retry transient OpenAI file blob upload failures (#47393)
## Why A single blob upload failure previously aborted the file upload, even for a temporary `503` response or an interrupted stream. ## What changed - Retry `503` responses and timeout, connection, body, or request errors, with at most five attempts within a shared five-minute deadline. - Honor `x-ms-retry-after-ms` and `Retry-After`, falling back to backoff when no valid server delay is available. - Reopen the contents for each retry while reusing the same file record and upload URL. Preserve the initial read permission check and reopen MCP file streams under the same sandbox policy. - Record retry, recovery, and terminal failure events with a fresh client request ID for each attempt. ## Testing Add coverage for recovery after `503` responses and interrupted streams, attempt and deadline limits, non-retryable responses, retry delay parsing, and unique request IDs. Extend the restricted-read MCP upload test to verify that both attempts send the complete file contents. GitOrigin-RevId: 78089ecfd5076313f5784cbbc864df12d082e3b1
This commit is contained in:
@@ -23,6 +23,7 @@ use codex_sandboxing::policy_transforms::effective_file_system_sandbox_policy;
|
||||
use codex_sandboxing::policy_transforms::merge_permission_profiles;
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
struct FileArgumentLocation<'a> {
|
||||
field_name: &'a str,
|
||||
@@ -213,10 +214,12 @@ async fn build_uploaded_argument_value(
|
||||
OPENAI_FILE_UPLOAD_LIMIT_BYTES,
|
||||
)));
|
||||
}
|
||||
let contents = fs
|
||||
.read_file_stream(&path_uri, sandbox.as_ref())
|
||||
.await
|
||||
.map_err(|error| contextualize_error(error.to_string()))?;
|
||||
// Keep the initial read permission check before allocating a remote file record.
|
||||
let mut first_contents = Some(
|
||||
fs.read_file_stream(&path_uri, sandbox.as_ref())
|
||||
.await
|
||||
.map_err(|error| contextualize_error(error.to_string()))?,
|
||||
);
|
||||
let file_name = path_uri
|
||||
.basename()
|
||||
.or_else(|| {
|
||||
@@ -228,6 +231,18 @@ async fn build_uploaded_argument_value(
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(|| "file".to_string());
|
||||
let open_contents = move || {
|
||||
let first_contents = first_contents.take();
|
||||
let fs = Arc::clone(&fs);
|
||||
let path_uri = path_uri.clone();
|
||||
let sandbox = sandbox.clone();
|
||||
async move {
|
||||
match first_contents {
|
||||
Some(contents) => Ok(contents),
|
||||
None => fs.read_file_stream(&path_uri, sandbox.as_ref()).await,
|
||||
}
|
||||
}
|
||||
};
|
||||
let upload_auth = codex_model_provider::auth_provider_from_auth(auth);
|
||||
let uploaded = upload_openai_file(
|
||||
turn_context.config.chatgpt_base_url.trim_end_matches('/'),
|
||||
@@ -235,7 +250,7 @@ async fn build_uploaded_argument_value(
|
||||
&sess.services.openai_file_upload_client_pool,
|
||||
file_name,
|
||||
metadata.size,
|
||||
contents,
|
||||
open_contents,
|
||||
hosted_upload,
|
||||
)
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user