Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming memory leak #3548

Closed
euzu opened this issue Jan 8, 2025 · 2 comments
Closed

Streaming memory leak #3548

euzu opened this issue Jan 8, 2025 · 2 comments
Labels
N/A Not applicable or remedied without code change.

Comments

@euzu
Copy link

euzu commented Jan 8, 2025

I am opening with a reqwest client a media stream from a server, then i return this stream with HttpResponse::Ok().streaming(stream).

The Problem is that memory increases at each call and is not released.

rustc 1.83.0

actix-web = "4.9.0"
actix-server = "2.5.0"
reqwest = { version = "0.12.12", features = ["json", "stream", "rustls-tls"] }
async fn api_stream(
    req: HttpRequest,
    api_req: web::Query<StreamApiRequest>,
    app_state: web::Data<AppState>,
) -> HttpResponse {
    let (_, _, stream_url) = path.into_inner();
    stream_response(&app_state, stream_url, &req).await
}

pub async fn stream_response(app_state: &AppState, stream_url: &str, req: &HttpRequest) -> HttpResponse {
    if let Ok(url) = Url::parse(stream_url) {
        let client = reqwest::Client::new().get(url.clone());
        let headers = get_request_headers();
        let client = client.headers(headers)
        match client.send().await {
            Ok(response) => {
                let status = response.status();
                if status.is_success() {
                    return HttpResponse::Ok().body(actix_web::body::BodyStream::new(response.bytes_stream()));
                }
            }
            Err(err) => {
                error!("Received failure from server {stream_url}:  {err}");
            }
        }
    } else {
        error!("Url is malformed {stream_url}");
    }
    HttpResponse::BadRequest().finish()
}




pub fn api_register(cfg: &mut web::ServiceConfig) {
  cfg.service(web::resource(format!("/stream")).route(web::get().to(api_stream);
}


#[actix_web::main]
pub async fn start_server() -> futures::io::Result<()> {

    HttpServer::new(move || {
            App::new()
            ...
            ...

           .configure(api_register)
           })
    }).bind(format!("{host}:{port}"))?.run().await
}
@euzu
Copy link
Author

euzu commented Jan 8, 2025

I have replaced reqwest with awc = { version = "3", features = [ "openssl" ] } and the same problem, memory leak at each call. Memory increases but not releases.

@robjtede
Copy link
Member

robjtede commented Jan 8, 2025

  1. try sharing the client using App::app_data instead of creating a new one each call
  2. see Memory Leak #3198 for more info on expected memory usage due to allocators and object caching

@robjtede robjtede closed this as not planned Won't fix, can't repro, duplicate, stale Jan 8, 2025
@robjtede robjtede added the N/A Not applicable or remedied without code change. label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
N/A Not applicable or remedied without code change.
Projects
None yet
Development

No branches or pull requests

2 participants