Skip to content

fix(io): send empty UDP datagrams in Bun - #998

Merged
nicolas-abril merged 2 commits into
bendlang:mainfrom
vicmcorrea:dev/udp-empty-datagram
Sep 25, 2026
Merged

nicolas-abril merged 2 commits into
bendlang:mainfrom
vicmcorrea:dev/udp-empty-datagram

Conversation

@vicmcorrea

@vicmcorrea vicmcorrea commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Why

UDP.send_to with an empty string crashes on Bun before sending a packet. Bun's FFI rejects a pointer to an empty byte array. Native Bend sends the same zero-byte datagram successfully.

i'm evaluating Bend alongside our TypeScript monorepo and found this while checking Bun and native behavior.

What changed

this passes a null pointer when the payload is empty and still calls sendto. Nonempty payloads keep the existing path.

The UDP round-trip test keeps the original bend-udp ping and adds an empty ping through the same helpers. It checks both replies, bend-udp-pong and -pong, including delivery and the sender's return address. Two files changed, six net lines added.

Validation

Checked on macOS arm64 with Bun 1.3.11 and clang 23.1.1:

  • On the original Bun backend, the test prints bend-udp-pong and then fails on the empty ping. With the fix, Bun and native C print both expected replies and exit 0.
  • All six UDP fixtures and port_bound.bend pass checking and execution under Bun and native C.
  • A local receiver confirmed the actual bytes for empty, ASCII, Unicode and embedded-NUL payloads.
  • Local repository gate passed, 46/46, and git diff --check passed.

Cluster gates were not run.

Run the regression

From this PR's checkout, with Bun on PATH:

(
  set -e
  udp_check_dir="$(mktemp -d)"
  BEND_NO_TELEMETRY=1 bun bend2/main.ts tests/io/udp_send_recv.bend -o "$udp_check_dir/udp.js"
  bun "$udp_check_dir/udp.js"
  rm "$udp_check_dir/udp.js"
  rmdir "$udp_check_dir"
)

The program exits 0 and prints:

bend-udp-pong
-pong

With the original Bun backend, it prints the first line and exits 1 with the empty-memory pointer error.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

The regressions test should probably be an addition. Either modify it to just add another (empty) message in that test or make a small new regression test just for this

@vicmcorrea

Copy link
Copy Markdown
Contributor Author

The regressions test should probably be an addition. Either modify it to just add another (empty) message in that test or make a small new regression test just for this

okss 🫡

@nicolas-abril nicolas-abril self-assigned this Sep 24, 2026

@nicolas-abril nicolas-abril 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.

.

@vicmcorrea

Copy link
Copy Markdown
Contributor Author

.

hey, saw the change request. i thought the latest update covered your feedback, but might be missing something.... could you point me to what still needs changing?

@nicolas-abril

Copy link
Copy Markdown
Collaborator

sorry i didn't see that there was alredy a commit

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks @vicmcorrea, and sorry again for the stale change request! The empty ping reproduces the Bun crash without the fix and passes with it, and every UDP test passes in the JS and C lanes on top of current main. Merging 🙏

@nicolas-abril
nicolas-abril merged commit 6fed2cb into bendlang:main Sep 25, 2026
@Lorenzobattistela Lorenzobattistela mentioned this pull request Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants