Repository navigation
Send the whole email when it is larger than the socket's send buffer - #419
Merged
abhishekgahlot2 merged 1 commit intoNov 5, 2026
Conversation
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
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
sendSmtpwrote every SMTP line with onesocket.write(...)call and ignored its result. That included the whole message afterDATA. Bun's TCP and TLS sockets keep nothing back.writehands 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 endsDATA. The relay then waited for the rest, nothing was delivered, and the send failed withSMTP 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:writeencodes each line to bytes and adds them to anunsentbuffer. It writes as much as the socket takes and keeps the rest. A newdrainhandler 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.SMTP timeoutas before.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 localBun.listenSMTP 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
mainthis test fails witherror: 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: passesbun test test/smtp-message.test.ts test/smtp-timeout.test.tsplus the invitation and connector tests: 30 pass, 0 failcd 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-rejectionand the Codex config tests). Run alone on unchangedmainon 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.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.