Skip to content

feat: add lambda-runtime-invocation-id header - #1159

Open
darklight3it wants to merge 5 commits into
mainfrom
feat/add-runtime-invocation-id-header-support
Open

feat: add lambda-runtime-invocation-id header#1159
darklight3it wants to merge 5 commits into
mainfrom
feat/add-runtime-invocation-id-header-support

Conversation

@darklight3it

@darklight3it darklight3it commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Add Lambda-Runtime-Invocation-Id header support for cross-wiring protection.

The RIC now echoes the invocation ID received from RAPID on /next back on /response and /error, enabling RAPID to detect and reject stale responses from timed-out invocations.

Problem

On Lambda Managed Instances (LMI) and On-Demand (OD), when an invoke times out, the runtime process continues running in the background. If a new invoke arrives with the same requestId, RAPID accepts it. The still-running old invocation eventually posts its response, and RAPID matches it to the new invoke — delivering the wrong response (cross-wiring).

Solution

RAPID sends a unique per-invoke nonce via Lambda-Runtime-Invocation-Id header on /next. The runtime echoes it back on /response and /error. RAPID validates the match before accepting the response.

Backward Compatibility

Fully backward compatible in both directions:

  • If RAPID doesn't send the header → RIC doesn't see it → doesn't echo → no behavior change
  • If RIC doesn't echo it (old version) → RAPID skips validation → no behavior change

Testing

  • added unit test
  • added a new dockerized test working with the new RIE.

@darklight3it
darklight3it force-pushed the feat/add-runtime-invocation-id-header-support branch from b60f865 to 048c1e8 Compare August 9, 2026 18:16
@darklight3it
darklight3it force-pushed the feat/add-runtime-invocation-id-header-support branch from 4c28d43 to afb6708 Compare August 26, 2026 16:15
@darklight3it
darklight3it marked this pull request as ready for review August 26, 2026 16:18
@darklight3it
darklight3it force-pushed the feat/add-runtime-invocation-id-header-support branch from afb6708 to de1f2e4 Compare August 26, 2026 16:23
@darklight3it darklight3it added the enhancement New feature or request label Aug 27, 2026
@darklight3it darklight3it self-assigned this Aug 27, 2026
@darklight3it
darklight3it force-pushed the feat/add-runtime-invocation-id-header-support branch 5 times, most recently from c403f80 to 31c22d2 Compare August 27, 2026 12:49
@darklight3it
darklight3it force-pushed the feat/add-runtime-invocation-id-header-support branch from 31c22d2 to 2cabdac Compare August 27, 2026 13:11
Comment thread lambda-runtime/src/types.rs Outdated
Comment thread examples/invocation-id-concurrent/src/main.rs Outdated
return scenarios


def get_invocation_id_scenarios():

@vip-amzn vip-amzn Aug 27, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not testing the invocation id cross wiring situation. the assertion is on request id, which is going to be the same. But we want to check that response is coming appropriately.

batches = [
    [Request.create(
        payload={"command": "invoke-A", "sleep": TIMEOUT + 2},
        assertions=[{"transform": ".errorType", "error": "Sandbox.Timedout"}],
        headers={"X-Amzn-RequestId": SAME_REQUEST_ID},
    )],
    [Request.create(
        payload={"command": "invoke-B", "sleep": TIMEOUT - 1},
        assertions={"response": {"from": "invoke-B"}},   
        headers={"X-Amzn-RequestId": SAME_REQUEST_ID},
    )],
]

@darklight3it
darklight3it force-pushed the feat/add-runtime-invocation-id-header-support branch from fc33a71 to 1152b7c Compare August 27, 2026 16:04

@jlizen jlizen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core approach is good, but some small tweaks.

Also: currently we log Lambda function timeout! for any 410, but we now will have a 410 if a stale response is rejected. We should tweak the message.

let mut req = build_request().method(Method::POST).uri(uri).body(body)?;

if let Some(id) = self.invocation_id {
req.headers_mut().insert(LAMBDA_RUNTIME_INVOCATION_ID, id.parse()?);

@jlizen jlizen Aug 28, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will error out and crash the runtime if a malformed invocation id header is sent.

We originally decode with String::from_utf8_lossy(), but that replaces non-utf8 bytes with U+FFFD which is anyway not ascii. So then this parse will fail.

I did a quick check and didn't find any other round trips, this new code is the only place impacted.

I know we control the sender but we should be defensive against malformed inputs anyway. I would suggest a rate-limited log warning if we have bad bytes.

struct Request {
#[serde(rename = "command")]
_command: String,
sleep: u32,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have #[serde(default)], otherwise the example command in the header will actually have a deserialization failure.

@@ -1,2 +1,2 @@
"""
Multi-concurrency test scenarios for basic-lambda-concurrent.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is stale now i think?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IE, there are multiple lambda handlers now?

TIMEOUT = 5


def _make_env(concurrency: int = DEFAULT_CONCURRENCY) -> dict:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should accept a handler input too, right now it makes it seem like everything is the basic concurrent

let mut req = build_request().method(Method::POST).uri(uri).body(body)?;

if let Some(id) = self.invocation_id {
req.headers_mut().insert(LAMBDA_RUNTIME_INVOCATION_ID, id.parse()?);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we use insert() here but append() on the streaming branch, I would make them consistent

use tower::{service_fn, Service};

#[tokio::test]
async fn forwards_invocation_id_from_next_response_headers() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have a test covering the deserialization error path as well

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants