Docker中Rust Tokio执行FFmpeg后Warp服务curl 52连接异常
Docker容器中Rust Tokio执行FFmpeg后HTTP请求返回curl 52错误
在Docker容器环境中,通过Rust的Tokio进程执行FFmpeg音频拼接命令后,后续发起的HTTP请求会失败,出现“curl 52 empty response from server”错误,连接被关闭。该问题仅在Docker环境下、执行FFmpeg后立即请求时触发,非Docker环境无此异常;若执行FFmpeg后不发起HTTP请求,也不会出现curl 52错误。
curl详细输出
curl -v "http://localhost:3030" * Trying 127.0.0.1:3030... * Connected to localhost (127.0.0.1) port 3030 (#0) > GET / HTTP/1.1 > Host: localhost:3030 > User-Agent: curl/8.1.2 > Accept: */* > * Empty reply from server * Closing connection 0 curl: (52) Empty reply from server
Docker日志
[2024-06-03T05:26:58Z INFO minimal_docker_webserver_post_error] Starting server on 0.0.0.0:3030 [2024-06-03T05:26:58Z INFO warp::server] Server::run; addr=0.0.0.0:3030 [2024-06-03T05:26:58Z INFO warp::server] listening on http://0.0.0.0:3030 [2024-06-03T05:27:07Z INFO minimal_docker_webserver_post_error] WAV files concatenated successfully [Running 'cargo run'] Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s Running `target/debug/minimal_docker_webserver_post_error` [2024-06-03T05:27:08Z INFO minimal_docker_webserver_post_error] Starting server on 0.0.0.0:3030 [2024-06-03T05:27:08Z INFO warp::server] Server::run; addr=0.0.0.0:3030 [2024-06-03T05:27:08Z INFO warp::server] listening on http://0.0.0.0:3030
已尝试的方案
- 更换Web框架(Warp、Actix-web)和请求库(reqwest、ureq)
- 非Docker环境运行无异常
- Docker环境下不发起后续HTTP请求则无错误
- 向httpbin发送最小请求仍存在问题
最小复现代码
main.rs
use warp::Filter; use reqwest::Client; use std::convert::Infallible; use log::{info, error}; use env_logger; use tokio::process::Command; #[tokio::main] async fn main() { std::env::set_var("RUST_LOG", "debug"); env_logger::init(); let route = warp::path::end() .and_then(handle_request); info!("Starting server on 0.0.0.0:3030"); warp::serve(route) .run(([0, 0, 0, 0], 3030)) .await; } async fn handle_request() -> Result<impl warp::Reply, Infallible> { let client = Client::new(); let output = Command::new("ffmpeg") .args(&[ "y", "-i", "concat:/usr/src/minimal_docker_webserver_post_error/file1.wav|/usr/src/minimal_docker_webserver_post_error/file2.wav", "-c", "copy", "/usr/src/minimal_docker_webserver_post_error/combined.wav" ]) .output() .await; match output { Ok(output) => { if output.status.success() { info!("WAV files concatenated successfully"); } else { error!("Failed to concatenate WAV files: {:?}", output); return Ok(warp::reply::with_status("Failed to concatenate WAV files", warp::http::StatusCode::INTERNAL_SERVER_ERROR)); } }, Err(e) => { error!("Failed to execute ffmpeg: {:?}", e); return Ok(warp::reply::with_status("Failed to execute ffmpeg", warp::http::StatusCode::INTERNAL_SERVER_ERROR)); } } // ISSUE: Connection closes with curl: (52) Empty reply from server match client.get("https://httpbin.org/get").send().await { Ok(response) => info!("GET request successful: {:?}", response), Err(e) => error!("GET request failed: {:?}", e), } match client.post("https://httpbin.org/post") .body("field1=value1&field2=value2") .send().await { Ok(response) => info!("POST request successful: {:?}", response), Err(e) => error!("POST request failed: {:?}", e), } Ok(warp::reply::with_status("Request handled", warp::http::StatusCode::OK)) }
生成测试用WAV文件的FFmpeg命令
ffmpeg -f lavfi -i "sine=frequency=1000:duration=5" file1.wav && ffmpeg -f lavfi -i "sine=frequency=500:duration=5" file2.wav
Dockerfile
# Use the official Rust image as the base image FROM rust:latest # Install cargo-watch RUN cargo install cargo-watch # Install ffmpeg RUN apt-get update && apt-get install -y ffmpeg # Set the working directory inside the container WORKDIR /usr/src/minimal_docker_webserver_post_error # Copy the Cargo.toml and Cargo.lock files COPY Cargo.toml Cargo.lock ./ # Copy the source code COPY src ./src # Copy wav files COPY file1.wav /usr/src/minimal_docker_webserver_post_error/file1.wav COPY file2.wav /usr/src/minimal_docker_webserver_post_error/file2.wav # Install dependencies RUN cargo build --release # Expose the port that the application will run on EXPOSE 3030 # Set the entry point to use cargo-watch CMD ["cargo", "watch", "-x", "run"]
Cargo.toml
[package] name = "minimal_docker_webserver_post_error" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] warp = "0.3" reqwest = { version = "0.12.4", features = ["json"] } tokio = { version = "1", features = ["full"] } log = "0.4" env_logger = "0.11.3"
请求Warp服务的命令
curl -v "http://localhost:3030"
内容的提问来源于stack exchange,提问作者user762345
相关产品推荐
相关产品推荐

