Skip to content

Comments

fix(dev-mode): prevent connection breakage from large MQTT payloads#13313

Merged
czubocha merged 3 commits intomainfrom
sc-3783
Feb 4, 2026
Merged

fix(dev-mode): prevent connection breakage from large MQTT payloads#13313
czubocha merged 3 commits intomainfrom
sc-3783

Conversation

@czubocha
Copy link
Contributor

@czubocha czubocha commented Feb 3, 2026

Problem

When requests or responses exceed AWS IoT Core's 128 KB MQTT payload limit during serverless dev, the connection silently breaks with a write ECONNRESET error. This leaves developers in a "zombie state" where the CLI appears connected but all subsequent invocations fail.

Solution

Added proactive payload size checks (125 KB safety limit) in both directions:

Request Path (Shim → Local)

  • Check payload size before publishing to MQTT
  • If too large, publish error notification to /<functionName>/error topic
  • Throw error to preserve Lambda semantics (retries, DLQ for SQS/SNS/EventBridge)

Response Path (Local → Shim)

  • Check response size using Buffer.byteLength() for accurate UTF-8 handling
  • If too large, send PayloadTooLargeError back to shim and log to CLI

CLI Feedback

  • Subscribe to /<functionName>/error topics for each function
  • Display clear error messages with full invocation context (method, path, etc.)
  • Consistent coloring using awsRequestId

Changes

  • shim.js: Added buildMinimalEvent() and handlePayloadTooLarge() helper functions
  • shim.js: Fixed topic replacement to use regex /\/request$/ (prevents issues if service/stage is named "request")
  • index.js: Subscribe to error topics and handle notifications
  • index.js: Use Buffer.byteLength() instead of .length for accurate byte counting

Testing

Verified manually:

  • ✅ Small requests/responses work normally
  • ✅ Large requests (>125 KB) show clear error in CLI, connection remains healthy
  • ✅ Large responses (>125 KB) show clear error in CLI, connection remains healthy
  • ✅ Recovery works without restarting serverless dev
  • ✅ Lambda error semantics preserved (no try-catch wrapper swallowing errors)

Summary by CodeRabbit

  • New Features
    • Enforced a 125 KB maximum payload size limit for serverless function responses
    • Added structured error notifications when response payloads exceed the size limit
    • Provided user guidance on handling oversized payloads

@Mmarzex
Copy link
Contributor

Mmarzex commented Feb 3, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 3, 2026

📝 Walkthrough

Walkthrough

The changes implement MQTT payload size limit enforcement (125 KB) across the serverless dev environment. The shim serializes messages and validates size before publishing; oversized payloads trigger structured error notifications instead of normal responses. The dev server subscribes to error topics to receive and process these limit notifications.

Changes

Cohort / File(s) Summary
MQTT Payload Limit Enforcement
packages/serverless/lib/plugins/aws/dev/index.js, packages/serverless/lib/plugins/aws/dev/shim.js
Introduces 125 KB MQTT payload limit with pre-publish size validation. Shim checks serialized payload size and emits structured error notifications when limit exceeded. Dev server subscribes to error topics, handles limit notifications, and enforces limit on response publishing with fallback PayloadTooLargeError.

Sequence Diagram(s)

sequenceDiagram
    participant Function as Function Handler
    participant Shim as Shim (Dev Mode)
    participant MQTT as MQTT Broker
    participant DevServer as Dev Server

    Function->>Shim: publish(message)
    Shim->>Shim: serialize payload
    Shim->>Shim: check size ≤ 125 KB
    
    alt Payload within limit
        Shim->>MQTT: publish to function topic
        MQTT->>DevServer: deliver message
        DevServer->>DevServer: process normal response
        Note over DevServer: Add requestId & publish
    else Payload exceeds limit
        Shim->>Shim: buildMinimalEvent()
        Shim->>MQTT: publish error to /error topic
        MQTT->>DevServer: deliver error notification
        DevServer->>DevServer: process error notification
        DevServer->>MQTT: publish PayloadTooLargeError
        Shim->>Shim: throw error
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested reviewers

  • eahefnawy

Poem

🐰 A limit most splendid, one hundred and twenty-five,
Keeps payloads nimble and MQTT alive,
When messages swell past the size we can bear,
An error hops gently through messages fair! 🍕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: implementing MQTT payload size limit enforcement (125 KB) to prevent connection breakage from oversized payloads in dev mode.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sc-3783

Important

Action Needed: IP Allowlist Update

If your organization protects your Git platform with IP whitelisting, please add the new CodeRabbit IP address to your allowlist:

  • 136.113.208.247/32 (new)
  • 34.170.211.100/32
  • 35.222.179.152/32

Failure to add the new IP will result in interrupted reviews.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@czubocha czubocha changed the title Update AWS dev plugin helpers and test fixtures fix(dev-mode): prevent connection breakage from large MQTT payloads Feb 3, 2026
@czubocha czubocha requested a review from eahefnawy February 3, 2026 17:30
@czubocha czubocha merged commit 3b8628f into main Feb 4, 2026
12 checks passed
@czubocha czubocha deleted the sc-3783 branch February 4, 2026 09:22
@github-actions github-actions bot locked and limited conversation to collaborators Feb 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants