Adding "content-length" header to response causes closed connection. · Issue #862 · cargo-lambda/cargo-lambda · GitHub
Skip to content

Adding "content-length" header to response causes closed connection. #862

Closed

Description

@Jimmy-Z

This is adapted from the streaming example from here
https://github.com/awslabs/aws-lambda-rust-runtime/blob/main/examples/basic-streaming-response/src/main.rs

use lambda_runtime::{
	Error, LambdaEvent, service_fn,
	streaming::{Body, Response},
	tracing,
};
use serde_json::Value;

async fn func(_event: LambdaEvent<Value>) -> Result<Response<Body>, Error> {
	let body = "hello\n";
	let len = body.as_bytes().len();

	let mut resp = Response::from(Body::from(body));
	// resp.metadata_prelude.headers.append("content-type", "text/plain".parse().unwrap());
	resp.metadata_prelude.headers.append("content-length", len.into());

	Ok(resp)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
	// required to enable CloudWatch error logging by the runtime
	tracing::init_default_subscriber();

	lambda_runtime::run(service_fn(func)).await?;
	Ok(())
}

run it with cargo lambda watch, then curl -v

>curl -v http://127.0.0.1:9000
*   Trying 127.0.0.1:9000...
* Connected to 127.0.0.1 (127.0.0.1) port 9000
> GET / HTTP/1.1
> Host: 127.0.0.1:9000
> User-Agent: curl/8.9.1
> Accept: */*
>
* Empty reply from server
* shutting down connection #0
curl: (52) Empty reply from server

remove the header will fix it, but I wonder why?

upload the binary on aws, it works just fine, except the header becomes "x-amzn-remapped-content-length", so I guess it's not lambda-runtime's fault.

Activity

  1. calavera commented on Jun 26, 2025

    @calavera
    Collaborator

    upload the binary on aws, it works just fine, except the header becomes "x-amzn-remapped-content-length", so I guess it's not lambda-runtime's fault.

    It's probably some modification AWS does at the proxy level. Which service are you integrating your Lambda function with?

  2. Jimmy-Z commented on Jun 26, 2025

    @Jimmy-Z
    Author

    Nothing, maybe I'm too new to AWS to understand the question, I deployed it on AWS, and call it as a function url.

  3. calavera commented on Jun 26, 2025

    @calavera
    Collaborator

    call it as a function url.

    This is what I was looking for. There are usually a few ways to call the Lambda function, and unfortunately, they sometimes differ in behavior.

    I think it'd be possible to remove the header from the response payload to prevent the server from closing the connection, I'll have to investigate it further.

  4. github-actions commented on Jul 17, 2025

    @github-actions
    Contributor

    This issue has been automatically locked. Create a new discussion if you'd like to continue the conversation. https://github.com/cargo-lambda/cargo-lambda/discussions/new?category=general

  5. locked as resolved and limited conversation to collaborators on Jul 17, 2025
  6. calavera commented on Jul 18, 2025

    @calavera
    Collaborator

    Version 1.8.6 released with a fix for this issue: https://github.com/cargo-lambda/cargo-lambda/releases/tag/v1.8.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions