Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket - potential bug, weird behavior with ping/pong, maybe I'm missing something #5588

Open
robobun opened this issue Sep 16, 2023 · 1 comment
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API

Comments

@robobun
Copy link

robobun commented Sep 16, 2023

minimal reproducible

const server = Bun.serve({
    fetch(req) {
        console.log('SERVER incoming request');
        server.upgrade(req);
    },
    websocket: {
        open(ws) {
            console.log(`SERVER open`);
            ws.ping(); // removing this fixes the issue - this is probably the buggy part
        },
        close(ws, code, reason) {
            console.log(`SERVER close: code=${code} reason=${reason}`);
        },
        message(ws, message) {
            console.log(`SERVER message: ${Bun.inspect(message)}`);
        }
    },
    port: 1337,
});

const ws = new WebSocket('ws://127.0.0.1:1337');

ws.onopen = async () => { // same behavior with addEventHandler
    ws.send('this message sends just fine');

    for await (const _ of console) { // removing this fixes the issue
        ws.send('hi'); // causes closure upon first line
    }
};

output with code shown above

% bun run index.js
SERVER incoming request
SERVER open
SERVER message: "this message sends just fine"

output with server ws.ping() or client for await (...) excluded

% bun run index.js
SERVER incoming request
SERVER open
SERVER message: "this message sends just fine"
  <-- ENTER (for await over console)
SERVER close: code=1006 reason=

I haven't a clue what's actually causing this, but the presence or absence of a ping doesn't seem like it should cause any problems. I previously had handlers for ping/pong messages, I was just testing them out (no intervals yet), but the issue was there before as well - I tried to reduce the fluff as much as possible to get a simple reproducible

Originally reported on Discord: websocket - potential bug, weird behavior with ping/pong, maybe I'm missing something

@robobun robobun added the bun.js Something to do with a Bun-specific API label Sep 16, 2023
@Electroid Electroid added the bug Something isn't working label Sep 16, 2023
@DeveloperHarris
Copy link

DeveloperHarris commented Oct 12, 2023

Encountering this issue as well. The bun websocket client seems to have a bug in regards to receiving pings that causes the connection to close?

I also have a simple ServerWebSocket setup and a WebSocket like you, and am able to get some intervaled pings to work, but not others.

ping("42") for example works, but ping() or ping("hello") does not.

Very strange.

// Server
let timer;

Bun.serve({
    fetch(req, server) {
        // upgrade the request to a WebSocket
        if (server.upgrade(req)) {
            return; // do not return a Response
        }
        return new Response("Upgrade failed :(", { status: 500 });
    },
    websocket: {
        open(ws) {
            timer = setInterval(() => {
                console.log("server tx'd ping. ");
                ws.ping("test");
            }, 2000);
        },
        message(ws, message) {
            let hexString = message.toString("hex");
            let hexWithSpaces = hexString.match(/.{1,2}/g)!.join(" ");
            console.log("server rx'd:", hexWithSpaces);
        },
        close(ws, code, message) {
            console.log("server closed:", code, message);
            clearInterval(timer!);
        },
    },
});

// Client
const socket = new WebSocket("ws://localhost:3000");

socket.addEventListener("message", (event) => {
    console.log("client rx'd:", event.data);
});

socket.addEventListener("error", (event) => {
    console.error("client error:", event);
});

socket.addEventListener("close", (event) => {
    console.log("client close:", event.code, event.reason);
});

ws.ping("test") fails:

server tx'd ping.
server closed: 1006
client close: 1006 Connection ended

but ws.ping("42") works:

server tx'd ping.
server tx'd ping.
server rx'd: 34 32 b6 67 88 c1 5c 13 bc f3
server tx'd ping.
server tx'd ping.
server rx'd: 34 32 4b f3 7a 4d bf ba 4e 7f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API
Projects
None yet
Development

No branches or pull requests

3 participants