Skip to content

Commit 53100d1

Browse files
committed
minimal fix
1 parent cd91484 commit 53100d1

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

packages/bun-usockets/src/eventing/epoll_kqueue.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,11 @@ int kqueue_change(int kqfd, int fd, int old_events, int new_events, void *user_d
360360
if(!is_readable && !is_writable) {
361361
if(!(old_events & LIBUS_SOCKET_WRITABLE)) {
362362
// if we are not reading or writing, we need to add writable to receive FIN
363-
EV_SET64(&change_list[change_length++], fd, EVFILT_WRITE, EV_ADD, 0, 0, (uint64_t)(void*)user_data, 0, 0);
363+
EV_SET64(&change_list[change_length++], fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, (uint64_t)(void*)user_data, 0, 0);
364364
}
365365
} else if ((new_events & LIBUS_SOCKET_WRITABLE) != (old_events & LIBUS_SOCKET_WRITABLE)) {
366366
/* Do they differ in writable? */
367-
EV_SET64(&change_list[change_length++], fd, EVFILT_WRITE, (new_events & LIBUS_SOCKET_WRITABLE) ? EV_ADD : EV_DELETE, 0, 0, (uint64_t)(void*)user_data, 0, 0);
367+
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);
368368
}
369369
int ret;
370370
do {

test/js/bun/http/bun-server.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import type { Server, ServerWebSocket, Socket } from "bun";
22
import { describe, expect, test } from "bun:test";
3-
import { bunEnv, bunExe, rejectUnauthorizedScope, tempDirWithFiles, tls } from "harness";
3+
import { bunEnv, bunExe, bunRun, rejectUnauthorizedScope, tempDirWithFiles, tls } from "harness";
44
import path from "path";
55

66
describe.concurrent("Server", () => {
7+
test("should not use 100% CPU when websocket is idle", async () => {
8+
const { stderr } = bunRun(path.join(import.meta.dir, "bun-websocket-cpu-fixture.js"));
9+
expect(stderr).toBe("");
10+
});
711
test("normlizes incoming request URLs", async () => {
812
using server = Bun.serve({
913
fetch(request) {

test/js/bun/http/bun-websocket-cpu-fixture.js

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)