Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/bun-usockets/src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,18 @@ void us_internal_socket_after_open(struct us_socket_t *s, int error) {
}
}
}
#else
/* A failed non-blocking connect() is reported as "writable" on every
* platform; the actual errno lives in SO_ERROR. epoll also sets
* EPOLLERR/EPOLLHUP so the dispatch flag is non-zero there, but kqueue
* signals it via EVFILT_WRITE + EV_EOF, which the dispatch loop no longer
* maps (EV_EOF on an established socket is a half-close, not an error).
* When the dispatch flag is zero, probe SO_ERROR — the portable contract
* for async connect completion — at the cost of one getsockopt on this
* cold path. */
if (error == 0) {
error = us_socket_get_error(s);
}
#endif
if (error) {
if (c) {
Expand Down
81 changes: 61 additions & 20 deletions packages/bun-usockets/src/eventing/epoll_kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,34 @@ static void us_internal_dispatch_ready_polls(struct us_loop_t *loop) {
/* Kqueue delivers each filter (READ, WRITE, TIMER, etc.) as a separate kevent,
* so the same fd/poll can appear twice in ready_polls. We coalesce them into a
* single set of flags per poll before dispatching, matching epoll's behavior
* where each fd appears once with a combined bitmask. */
* where each fd appears once with a combined bitmask.
*
* EV_EOF is intentionally NOT mapped to dispatch's `eof`. Kqueue sets EV_EOF on
* EVFILT_READ for a half-close (peer FIN — analogous to EPOLLRDHUP, which the
* epoll path also does not map) and on EVFILT_WRITE when the peer's read side is
* gone. Treating either as `eof` made loop.c close the socket before the recv
* loop had drained the kernel buffer — e.g. an EVFILT_WRITE-only event carrying
* EV_EOF skips recv entirely, and a busy-loop bail-out can leave bytes unread in
* the same tick the flag arrived. EVFILT_READ is level-triggered and keeps
* firing after a FIN, so recv()==0 (loop.c) is the single source of `eof`,
* same as Linux. RST/hard errors surface via the recv()/send() error paths;
* connect failures via getsockopt(SO_ERROR) in us_internal_socket_after_open.
*
* The exception is a poll watching nothing (us_socket_pause followed by
* us_socket_shutdown — node:http does exactly this for an unconsumed
* request body). It will never recv(), so EV_EOF is the only close signal;
* kqueue_change arms an edge-triggered EVFILT_READ for that state and we
* forward EV_EOF as `eof` only when us_poll_events == 0. The recv loop is
* still skipped, so buffered data the caller paused for is left untouched.
*
* (EV_ERROR only flags changelist-processing failures — the wait kevent64
* calls below pass a NULL changelist, so `error` is effectively always 0
* here; it is kept for symmetry with the epoll path.) */
struct kevent_flags {
uint8_t readable : 1;
uint8_t writable : 1;
uint8_t error : 1;
uint8_t eof : 1;
uint8_t ev_eof : 1;
uint8_t skip : 1;
uint8_t _pad : 3;
};
Expand All @@ -246,7 +268,7 @@ static void us_internal_dispatch_ready_polls(struct us_loop_t *loop) {
#endif
.writable = (filter == EVFILT_WRITE),
.error = !!(flags & EV_ERROR),
.eof = !!(flags & EV_EOF),
.ev_eof = !!(flags & EV_EOF),
};

/* Look backward for a prior entry with the same poll to coalesce into.
Expand All @@ -257,7 +279,7 @@ static void us_internal_dispatch_ready_polls(struct us_loop_t *loop) {
coalesced[j].readable |= bits.readable;
coalesced[j].writable |= bits.writable;
coalesced[j].error |= bits.error;
coalesced[j].eof |= bits.eof;
coalesced[j].ev_eof |= bits.ev_eof;
coalesced[i] = (struct kevent_flags){ .skip = 1 };
merged = 1;
break;
Expand Down Expand Up @@ -285,9 +307,16 @@ static void us_internal_dispatch_ready_polls(struct us_loop_t *loop) {
int events = (bits.readable ? LIBUS_SOCKET_READABLE : 0)
| (bits.writable ? LIBUS_SOCKET_WRITABLE : 0);

events &= us_poll_events(poll);
if (events || bits.error || bits.eof) {
us_internal_dispatch_ready_poll(poll, bits.error, bits.eof, events);
const int watched = us_poll_events(poll);
events &= watched;
/* EV_EOF only stands in for eof on a shut-down poll watching nothing —
* see the comment above. Any READABLE poll discovers eof via recv()==0;
* a non-SHUT_DOWN poll at watched==0 (allow_half_open after FIN, or a
* plain pause) must not re-enter on_end. */
const int eof = bits.ev_eof && watched == 0
&& us_internal_poll_type(poll) == POLL_TYPE_SOCKET_SHUT_DOWN;
if (events || bits.error || eof) {
us_internal_dispatch_ready_poll(poll, bits.error, eof, events);
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
}
}
#endif
Expand Down Expand Up @@ -442,21 +471,33 @@ int kqueue_change(int kqfd, int fd, int old_events, int new_events, void *user_d
struct kevent64_s change_list[2];
int change_length = 0;

/* Do they differ in readable? */
int is_readable = (new_events & LIBUS_SOCKET_READABLE);
int is_writable = (new_events & LIBUS_SOCKET_WRITABLE);
if ((new_events & LIBUS_SOCKET_READABLE) != (old_events & LIBUS_SOCKET_READABLE)) {
EV_SET64(&change_list[change_length++], fd, EVFILT_READ, is_readable ? EV_ADD : EV_DELETE, 0, 0, (uint64_t)(void*)user_data, 0, 0);
}
if (new_events == 0 && user_data != NULL) {
/* A poll watching nothing still needs to learn about peer close — the
* epoll path gets EPOLLHUP for free here. Arm an edge-triggered
* EVFILT_READ: it fires once per state transition (data arrival or FIN)
* so buffered bytes the caller paused for don't cause a level-triggered
* spin, and the dispatch loop above forwards its EV_EOF as `eof` only
* for watched==0, never entering the recv loop. EV_ADD on an existing
* level-triggered registration just changes it to EV_CLEAR. */
EV_SET64(&change_list[change_length++], fd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (uint64_t)(void*)user_data, 0, 0);
if (old_events & LIBUS_SOCKET_WRITABLE) {
EV_SET64(&change_list[change_length++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, (uint64_t)(void*)user_data, 0, 0);
}
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
} else {
/* Do they differ in readable? Transitioning from new_events==0 lands
* here too: EV_ADD without EV_CLEAR returns the filter to level-
* triggered. (old_events==0 with READABLE in new_events always differs,
* so the EV_CLEAR sentinel is overwritten.) */
if ((new_events & LIBUS_SOCKET_READABLE) != (old_events & LIBUS_SOCKET_READABLE)) {
EV_SET64(&change_list[change_length++], fd, EVFILT_READ, (new_events & LIBUS_SOCKET_READABLE) ? EV_ADD : EV_DELETE, 0, 0, (uint64_t)(void*)user_data, 0, 0);
}

if(!is_readable && !is_writable) {
if(!(old_events & LIBUS_SOCKET_WRITABLE)) {
// if we are not reading or writing, we need to add writable to receive FIN
EV_SET64(&change_list[change_length++], fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, (uint64_t)(void*)user_data, 0, 0);
/* Do they differ in writable? EVFILT_WRITE is always one-shot;
* EV_DELETE on an already-fired one-shot returns ENOENT, which
* KEVENT_FLAG_ERROR_EVENTS routes into the result list and we ignore. */
if ((new_events & LIBUS_SOCKET_WRITABLE) != (old_events & LIBUS_SOCKET_WRITABLE)) {
EV_SET64(&change_list[change_length++], fd, EVFILT_WRITE, (new_events & LIBUS_SOCKET_WRITABLE) ? EV_ADD | EV_ONESHOT : EV_DELETE, 0, 0, (uint64_t)(void*)user_data, 0, 0);
}
} else if ((new_events & LIBUS_SOCKET_WRITABLE) != (old_events & LIBUS_SOCKET_WRITABLE)) {
/* Do they differ in writable? */
EV_SET64(&change_list[change_length++], fd, EVFILT_WRITE, (new_events & LIBUS_SOCKET_WRITABLE) ? EV_ADD | EV_ONESHOT : EV_DELETE, 0, 0, (uint64_t)(void*)user_data, 0, 0);
}
int ret;
do {
Expand Down
7 changes: 5 additions & 2 deletions packages/bun-usockets/src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,12 @@
if (s->flags.is_paused) return;
// closed cannot be paused because it is already closed
if (us_socket_is_closed(s)) return;
// we are readable and writable so we can just pause readable side
us_poll_change(&s->p, s->group->loop, LIBUS_SOCKET_WRITABLE);
s->flags.is_paused = 1;
/* A shut-down socket can't write either; watch nothing so the kqueue
* watched==0 sentinel (or EPOLLHUP on epoll) detects peer close without
* reading the data the caller paused for. */
us_poll_change(&s->p, s->group->loop,
us_socket_is_shut_down(s) ? 0 : LIBUS_SOCKET_WRITABLE);

Check failure on line 667 in packages/bun-usockets/src/socket.c

View check run for this annotation

Claude / Claude Code Review

us_socket_pause SSL-aware shut-down check mismatches raw-poll-type dispatch gate

The arm-side and consume-side of the new `watched==0` sentinel use mismatched definitions of "shut down" for SSL sockets. `us_socket_pause()` tests the SSL-aware `us_socket_is_shut_down(s)` (true once `SSL_SENT_SHUTDOWN` is set, even though `us_internal_ssl_shutdown` does **not** call `raw_shutdown` when `SSL_shutdown()` returns ≥0, so the poll type stays `POLL_TYPE_SOCKET`), but the dispatch gate at epoll_kqueue.c:316-317 requires the raw `us_internal_poll_type(poll) == POLL_TYPE_SOCKET_SHUT_DO
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
}

void us_socket_resume(struct us_socket_t *s) {
Expand Down
79 changes: 79 additions & 0 deletions test/js/web/fetch/fetch-chunked-fin-same-tick-fixture.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions test/js/web/fetch/fetch-chunked-fin-same-tick.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { join } from "node:path";

// kqueue used to map EV_EOF (from either filter) straight to dispatch's `eof`,
// so an EVFILT_WRITE event carrying EV_EOF — peer fully closed while we were
// still uploading — closed the socket before the recv loop drained the response
// bytes the same FIN carried. Linux never hit this because the epoll path only
// maps EPOLLHUP and discovers half-close via recv()==0.
test("fetch() drains a chunked body when the server FINs in the same tick (kqueue EV_EOF)", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), join(import.meta.dir, "fetch-chunked-fin-same-tick-fixture.ts")],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect({ stdout: stdout.trim(), stderr: stderr.trim(), exitCode }).toEqual({
stdout: "OK 200",
stderr: "",
exitCode: 0,
});
});
Loading