oven-sh / bun

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

Bun has crashed #11907

Closed renat2985 closed 1 month ago

renat2985 commented 3 months ago

How can we reproduce the crash?

This is not a server resource issue. This is a fairly powerful Amazon cloud server that runs only bun, serves about 200 WebSoscket connections...

free -h
              total        used        free      shared  buff/cache   available
Mem:           31Gi       871Mi        18Gi        22Mi        12Gi        30Gi
Swap:         511Mi          0B       511Mi

Relevant log output

Bun v1.1.13 (bd6a6051) Linux x64
Args: "bun" "socket.js"
Features: jsc http_server 
Builtins: "bun:main" "bun:sqlite" 
Elapsed: 314201443ms | User: 359302ms | Sys: 1072896ms
RSS: 1.07GB | Peak: 0.15GB | Commit: 1.07GB | Faults: 0

panic(main thread): Segmentation fault at address 0x2
oh no: Bun has crashed. This indicates a bug in Bun, not your code.

Stack Trace (bun.report)

Bun v1.1.13 (bd6a605) on linux x86_64 [AutoCommand]

Segmentation fault at address 0x00000002

Jarred-Sumner commented 3 months ago

Is there anything more you can share with us? The stacktrace says it crashed in a cork callback in the WebSocket server.

One thing that's a little interesting is that number 0x2 is how JavaScriptCore represents the value null in JS. This would potentially mean that somewhere we're assuming a value is an object without first checking that it is not null or undefined (or otherwise, not a cell).

renat2985 commented 3 months ago

These are excerpts, hope it helps you.

import { Database } from "bun:sqlite";
const db = Database.open(":memory:");
db.exec('CREATE TABLE "online" ("onlineID" INTEGER, "activity" TEXT,  *** , PRIMARY KEY("onlineID" AUTOINCREMENT))');
***
const port = process.env.PORT || 8585;
const server = Bun.serve({
  fetch(req, server) {
    const url = new URL(req.url);
    if (url.search.length == 72) {
      server.upgrade(req, {
        data: {
          name: "none",
          room: new URL(req.url).search.split("?")[1].split("&")[0].replace(/[^a-zA-Z0-9]/g, ''),
          mac: new URL(req.url).search.split("?")[1].split("&")[1].replace(/[^a-zA-Z0-9:]/g, ''),
          pid: Math.floor(Math.random() * (9999999 - 1000000 + 1)) + 1000000
        },
      })
    }
***
  port: port,
  websocket: {
    open(ws) {
      ws.subscribe(ws.data.room);
      ws.send(`Enter name:`);
    },
    message(ws, msg) {
      var dateString = viewDateTime();
***
      if (ws.data.name == "none") {
        ws.data.name = msg;
        msg = `joined`;
        ws.send(`[${dateString}] Welcome to the control ${ws.data.name}!`);
        console.log(dateString+' Connect: ' + ws.data.room + ' ' + ws.data.mac + ' ' + ws.data.pid + ' ' + ws.data.name);
***
        const result = db.query(`SELECT * FROM online WHERE room = '${ws.data.room}' AND mac = '${ws.data.mac}'`).get();
          db.query(`DELETE FROM online *** `).get();
        }
        db.query(`INSERT INTO online *** `).get();
      }
 ***
      }
      const out = `[${dateString}] ${ws.data.name} ${msg}`;
      ws.publishText(ws.data.room, out);
      ws.send(out);
    },
    close(ws, code, message) {
      var dateString = viewDateTime();
      const out = `[${dateString}] ${ws.data.name} disconnect`;
      ws.unsubscribe(ws.data.room);
      server.publish(ws.data.room, out);
***
      db.query(`DELETE FROM online  *** ');
      delete ws.data;
    },
    perMessageDeflate: false,
    idleTimeout: 60,  // 900 = 15min default: 120 (seconds)
  },
});
Jarred-Sumner commented 3 months ago

Oh, I wonder if it's delete ws.data. If you instead ws.data = null or ws.data = undefined, does that help?

renat2985 commented 3 months ago

Ok, thanks.

renat2985 commented 3 months ago

replace delete ws.data with ws.data = null unfortunately this did not help.

cirospaciari commented 2 months ago

Friday we added some fixes on server.zig, can you try bun upgrade --canary if is still happening?

Electroid commented 1 month ago

Fixed by https://github.com/oven-sh/bun/pull/13319