oven-sh / bun

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

Crash with reconnecting websockets (latest bun version) #12010

Open AlgorithmAiden opened 1 week ago

AlgorithmAiden commented 1 week ago

How can we reproduce the crash?

Download this git repo https://github.com/AlgorithmAiden/bun_crash_code, then remove the .git folder from the outer folder.

First change all occurrences of process.chdir('C:\\Users\\jack\\Home\\Programming\\blog projects\\blog generator') so that the path is the location of the folder.

Run bun livePreview.js, after a small delay you should get an output like this: [⟍] LIVE PREVIEW [⟋] Hosting site at http://localhost:3000/ There are 0 connected websockets for live previewing Watching 1 post: Making a custom blog

Open the site at the url, the third line should now read There is 1 connected websocket for live previewing. CTRL+c to stop the program, then run it again after > ~250ms, it should produce the crash, sometimes this process needs to be run more than once to get the crash.

Relevant log output

============================================================
Bun v1.1.15 (b23ba1fe) Windows x64
Args: "C:\Users\jack\AppData\Roaming\npm\node_modules\bun\bin\bun.exe" "C:\Users\jack\Home\Programming\blog projects\blog generator\livePreview.js"
Features: jsc Bun.stdin(2) Bun.stdout abort_signal http_server(2) spawn(2) 
Builtins: "bun:main" "node:events" "node:fs" "node:os" "node:path" "node:readline" "node:stream" "node:string_decoder" "node:util" "node:util/types" "ws" 
Elapsed: 766ms | User: 31ms | Sys: 31ms
RSS: 0.17GB | Peak: 0.17GB | Commit: 0.26GB | Faults: 42285

panic(main thread): Internal assertion failure
oh no: Bun has crashed. This indicates a bug in Bun, not your code.

To send a redacted crash report to Bun's team,
please file a GitHub issue using the link below:

 https://bun.report/1.1.15/wa1b23ba1fA6hghIgll3M++3iJwvrmG+2kCq16kd8y5uao+o+bwmg+fu7j/fg7g/fA0eNrzzCtJLcpLzFFILC5OLSrJzM9TSEvMzCktSgUAiSkKPg

Stack Trace (bun.report)

Bun v1.1.15 (b23ba1f) on windows x86_64 [AutoCommand]

panic: Internal assertion failure

AlgorithmAiden commented 1 week ago

Smaller crash replication

Steps to reproduce:

  1. Create a folder and copy all the files below to it.
  2. Run bun install ws in the folder.
  3. Run bun crash.js.
  4. Open chrome to http://localhost:3000/.
  5. Enjoy! (should crash in < ~30 seconds)

crash.js

import { spawn } from 'bun'

function update() {
    process.stdout.write(`*`)
    spawn({
        cmd: ['bun', 'server.js']
    }).exited.then(() =>
        setTimeout(update, Math.random() * 1000)
    )
}
update()

server.js

import { serve, file } from 'bun'
import { WebSocketServer } from 'ws'

const wss = new WebSocketServer({ port: 3001 })

setTimeout(() => wss.clients.forEach(client => {
    if (client.readyState === 1) client.send('reload')
}), 100)

serve({
    async fetch() {
        return new Response(file('index.html'), {
            headers: { 'Content-Type': 'text/html' }
        })
    }
})

setTimeout(process.exit, Math.random() * 1000)

index.html

<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <script>
        function setupWebSocket() {
            new WebSocket('ws://localhost:3001').onmessage = (event) => {
                if (event.data === 'reload') location.reload()
            }
            setTimeout(setupWebSocket, 100)
        }
        setupWebSocket()
    </script>
</body>

</html>