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

Bun has crashed #11907

Open
renat2985 opened this issue Jun 16, 2024 · 5 comments
Open

Bun has crashed #11907

renat2985 opened this issue Jun 16, 2024 · 5 comments
Labels
crash An issue that could cause a crash linux An issue that only occurs on Linux runtime

Comments

@renat2985
Copy link

renat2985 commented Jun 16, 2024

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

@renat2985 renat2985 added the crash An issue that could cause a crash label Jun 16, 2024
@github-actions github-actions bot added linux An issue that only occurs on Linux runtime labels Jun 16, 2024
@Jarred-Sumner
Copy link
Collaborator

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
Copy link
Author

renat2985 commented Jun 16, 2024

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
Copy link
Collaborator

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

@renat2985
Copy link
Author

Ok, thanks.

@renat2985
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash An issue that could cause a crash linux An issue that only occurs on Linux runtime
Projects
None yet
Development

No branches or pull requests

2 participants