oven-sh / bun

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

Crashed with http, ws, and y-websocket #12785

Open peilingjiang opened 3 months ago

peilingjiang commented 3 months ago

How can we reproduce the crash?

Please take a look at the code below.

import http from 'node:http'
import type { Duplex } from 'node:stream'
import url from 'node:url'
import number from 'lib0/number'
import debounce from 'lodash.debounce'
import WebSocket from 'ws'
// @ts-ignore
import { setupWSConnection } from 'y-websocket/bin/utils'

const host: string = process.env.HOST || 'localhost'
const port: number = number.parseInt(process.env.PORT || '1234')

const wss: WebSocket.Server = new WebSocket.Server({ noServer: true })

const server: http.Server = http.createServer(
  (_request: http.IncomingMessage, response: http.ServerResponse) => {
    response.writeHead(200, { 'Content-Type': 'text/plain' })
    response.end('okay')
  },
)

/* -------------------------------------------------------------------------- */

wss.on('connection', (ws: WebSocket, req: http.IncomingMessage) => {
  const parsedUrl = url.parse(req.url!, true)
  const id = parsedUrl.pathname?.slice(1) || ''

  setupWSConnection(ws, req, { docName: id })

  ws.on('message', (message: WebSocket.Data) => {
    try {
      const data: TLRecord = JSON.parse(message.toString())
      // ... (saving data to firestore)
    } catch (error) {
      console.error('Error parsing message ', error)
    }
  })
})

/* -------------------------------------------------------------------------- */

server.on(
  'upgrade',
  (request: http.IncomingMessage, socket: Duplex, head: Buffer) => {
    wss.handleUpgrade(request, socket, head, (ws: WebSocket) => {
      wss.emit('connection', ws, request)
    })
  },
)

server.listen(port, host, () => {
  console.log(
    `running at '${host}' on port ${port}`,
  )
})

Relevant log output

Bun v1.1.20 (ae194892) macOS Silicon
macOS v14.5
Args: "bun" "--watch" "src/index.ts"
Features: jsc transpiler_cache tsconfig_paths(2) tsconfig(2) 
Builtins: "bun:main" "node:events" "node:fs" "node:http" "node:https" "node:os" "node:path" "node:string_decoder" "node:url" "node:util/types" "ws" 
Elapsed: 80ms | User: 58ms | Sys: 20ms
RSS: 64.50MB | Peak: 64.50MB | Commit: 1.07GB | Faults: 96

Stack Trace (bun.report)

Bun v1.1.20 (ae19489) on macos aarch64 [AutoCommand]

Segmentation fault at address 0x0000001A

peilingjiang commented 3 months ago

It was due to no default export of lib0/number 🥲 Will close the issue for now.

Jarred-Sumner commented 3 months ago

Bun still shouldn't crash here. It should show a helpful error message.

peilingjiang commented 3 months ago

It does show an error message, but at the very bottom of a crazy list of many times of crashes and auto-restarts -

1 | (function (entry, fetcher)
    ^
SyntaxError: Missing 'default' export in module '/Users/.../node_modules/lib0/number.js'.
1 | (function (entry, fetcher)
    ^
SyntaxError: Missing 'default' export in module '/Users/.../node_modules/lib0/number.js'.

I didn't scroll to the bottom, so I initially missed it. I will re-open the issue just in case it would be valuable to look into it!