Skip to content

Send the whole email when it is larger than the socket's send buffer - #419

Merged
abhishekgahlot2 merged 1 commit into
useagenthq:mainfrom
officialasishkumar:fix/smtp-large-message-writes
Nov 5, 2026
Merged

abhishekgahlot2 merged 1 commit into
useagenthq:mainfrom
officialasishkumar:fix/smtp-large-message-writes

Conversation

@officialasishkumar

@officialasishkumar officialasishkumar commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

By opening this pull request you agree to the Contributor License Agreement. On your first pull request a bot asks you to sign it with a one-line comment.

Fixes #416

sendSmtp wrote every SMTP line with one socket.write(...) call and ignored its result. That included the whole message after DATA. Bun's TCP and TLS sockets keep nothing back. write hands the kernel what fits in the send buffer right now and returns that byte count. When a message was bigger than the free send buffer, its tail was dropped, including the \r\n. that ends DATA. The relay then waited for the rest, nothing was delivered, and the send failed with SMTP timeout. On loopback on macOS, write("x".repeat(20_000_000)) returns about 330,000 and the peer receives exactly that many bytes.

What changed

  • backend/src/connectors/email/smtp.ts: write encodes each line to bytes and adds them to an unsent buffer. It writes as much as the socket takes and keeps the rest. A new drain handler on the socket writes more each time the kernel frees room. Slicing is by bytes, not string characters, so multibyte text survives a partial write.
  • The existing deadline still bounds the whole dialog. If the relay stops reading, the send fails with SMTP timeout as before.
  • Every caller goes through this one function: run digests (connectors/email/transport.ts), invitations and sign-up confirmations (auth-invitations.ts).

Tests

New case in backend/test/smtp-message.test.ts. It sends a 16 MiB message of multibyte text (Grüße, 東京 🙂 ...) through a real socket to a local Bun.listen SMTP server. The size is well past Linux's 4 MiB default send buffer limit. The test checks that the received DATA section ends with the whole CRLF-normalized body followed by \r\n.\r\n.

On main this test fails with error: SMTP timeout. With the change it passes in about 140 ms.

Checks, run with Bun 1.3.14 against pgvector/pgvector:0.8.6-pg16-bookworm:

  • cd backend && bunx -p typescript@5.9.3 tsc --noEmit: passes
  • bun test test/smtp-message.test.ts test/smtp-timeout.test.ts plus the invitation and connector tests: 30 pass, 0 fail
  • cd backend && bun run test: 3543 pass, 29 fail. The failures are all in the runtime and provider-bridge files (runtime-provider-bridge, codex-subscription-runtime, runtime-environment, provider-gateway/sandbox-config, runtime-command-catalog, gateway-role-spend, provider-key-rejection and the Codex config tests). Run alone on unchanged main on the same machine, those files fail the same way (31 fail), so the failures come from that machine and not from this change. Nothing in them touches SMTP.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

sendSmtp wrote every SMTP line, the whole message after DATA included,
with a single socket.write call and ignored its result. Bun's TCP and
TLS sockets keep nothing back: write hands the kernel what fits in the
send buffer at that moment and returns that byte count. A message larger
than the free send buffer lost its tail, including the "\r\n." that ends
DATA, so the relay kept waiting, nothing was delivered and the send
failed with "SMTP timeout" once the deadline fired. A run digest with a
long answer is enough to hit this on a real relay.

The client now encodes each line to bytes and keeps the part write did
not take. The socket's drain handler writes the rest as the kernel frees
room. Slicing by bytes rather than by string characters keeps multibyte
text intact across partial writes. The existing deadline still bounds
the whole dialog.

The new case in test/smtp-message.test.ts sends a 16 MiB multibyte
message to a local SMTP server and checks that every byte arrives,
followed by the terminating dot. Without the change it fails with
"SMTP timeout".

Fixes useagenthq#416
@abhishekgahlot2
abhishekgahlot2 merged commit 848e81c into useagenthq:main Nov 5, 2026
20 checks passed
@abhishekgahlot2

Copy link
Copy Markdown
Contributor

Merged, thanks @officialasishkumar. Losing the tail and the final "." on a long message was a nasty silent failure; the 16 MiB multibyte test pins it down well.

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 5, 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.

Send the whole email when it is larger than the socket's send buffer

2 participants