oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.01k stars 2.66k forks source link

Websocket handler does not register Closing event. #2071

Closed mynlexi closed 1 week ago

mynlexi commented 1 year ago

What version of Bun is running?

0.5.6

What platform is your computer?

Darwin 22.1.0 arm64 arm

What steps can reproduce the bug?

Using bao.js (but straight import for ServerWebsocket from bun)

We send json from Golang server to bun server Golang:

    conn, _, _, err := ws.DefaultDialer.Dial(context.Background(), httpposturl)
    err = wsutil.WriteClientMessage(conn, ws.OpText, msg) (msg is a bunch of json in a loop)
    err = conn.Close()

Bun:

    open:(ws)=>{
        console.log("ready to recieve data ws \n")
        message ="";
        ws.send("ready")
    },
    close: (ws) => {
        console.log('\n want to close')

        console.log("closed download ws")
    },
    message:(ws, msg)=>{
        if (typeof msg === "string") {
            message += msg;
        }
        // @ts-ignore
        console.write(".")
    },

}

What is the expected behavior?

status sync 201ready to recieve data ws 

........................................
 want to close
closed download ws

What do you see instead?

% bun main.ts
Listening on 0.0.0.0:3005
status sync 201ready to recieve data ws 

........................................

close never fires

but on golang side: 2023/02/14 11:37:30 Client started, loops 39, rest: 3100, total: 162844 2023/02/14 11:37:30 2023-02-14 11:37:30.895731 +0100 CET m=+682.737986668 Connected to server 2023/02/14 11:37:30 1.065167ms Disconnected from server 2023/02/14 11:37:30 Cannot close the connection: close tcp 127.0.0.1:56007->127.0.0.1:3005: use of closed network connection Downloaded integrations/downloadDir/crm_1 162814 bytes outside

Additional information

this consistently fails like every 10th try. just no close by bun.

mynlexi commented 1 year ago

if I manually close the golang connection err = wsutil.WriteClientMessage(conn, ws.OpClose, []byte(""))

the issues seems to go away, but strange nonetheless

cirospaciari commented 1 year ago

Hi, @mynlexi I'm having some problems trying to emulate this problem, this is still happening in v0.5.7 on Mac?

Go code:

package main

import (
    "context"
    "github.com/gobwas/ws"
    "github.com/gobwas/ws/wsutil"
)
func main() {
    conn, _, _, _ := ws.DefaultDialer.Dial(context.Background(), "ws://localhost:8080")
    msg := []byte("Hello")

    _ = wsutil.WriteClientMessage(conn, ws.OpText, msg) 
    _ = conn.Close()
}

Bun code:

Bun.serve({
  port:8080,
  fetch(req, server) {
    server.upgrade(req);
  },
  websocket: {
    open: ws => {
      console.log("ready to receive data ws \n");
      ws.send("ready");
    },
    close: ws => {
      console.log("\n want to close");
      console.log("closed download ws");
    },
    message(ws, message) {
      ws.send(message); // echo back the message
    },
  },
});

Output:

ready to receive data ws 

 want to close
closed download was

PS: I'm on an Linux x86 machine so maybe this is an Mac M1 related problem

mynlexi commented 1 year ago

Yeah seems like it:

% bun main.ts
[0.04ms] ".env"
Listening on 0.0.0.0:3005
status sync 201
ready to receive data ws 

.................................................
closed download ws
set DB for WorkspaceID 1
^C
lorenz in ~/code/src/github.com/mynlexi/voiceline/voiceline_spotlight on main
% bun main.ts
[0.03ms] ".env"
Listening on 0.0.0.0:3005
status sync 201
ready to receive data ws 

.................................................set DB for WorkspaceID 1
^C
lorenz in ~/code/src/github.com/mynlexi/voiceline/voiceline_spotlight on main
% bun upgrade
Congrats! You're already on the latest version of bun (which is v0.5.7)
Jarred-Sumner commented 1 week ago

We have test coverage for this

If you still don't receive a close event please let us know and ideally include a reproduction