oven-sh / bun

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

Segmentation fault? from WASM? #13398

Closed boehs closed 1 month ago

boehs commented 2 months ago

How can we reproduce the crash?

I'm still working on a reproduction. What I know:

  1. I am executing bun test with a setup file
  2. First, the setup file spins up @electric-sql/pglite
  3. Tests work
  4. I add code to initialize a complex elysia project that is typically deployed to cloudflare in said setup file.
  5. When this code is added, bun either runs tests fine, segfaults, is killed, or throws a nonsense syntax error.
  6. Initially, I believed that this segfault was due to complex wasm initialization for og image generation on a route:
import satori, { init } from "satori/wasm";
import { Resvg, initWasm } from "@resvg/resvg-wasm";
import resvgwasm from "../../oss/node_modules/@resvg/resvg-wasm/index_bg.wasm";
import yogawasm from "../../oss/node_modules/yoga-wasm-web/dist/yoga.wasm";
import initYoga from "yoga-wasm-web";

const initialize = async () => {
  try {
    await initWasm(resvgwasm as WebAssembly.Module);
    const yoga = await initYoga(yogawasm as WebAssembly.Module);
    init(yoga);
  } catch (error) {
    console.error(error);
  }
}; 
  1. However, I no longer believed this because when references to this codepath are removed from the router, the error still happens, and it seems like the test is flakey but in the sort of way it might work fine for 5 runs, segfault for the next 5, and start working again.
  2. Of course, that then begs the question: does elysia actually have anything to do with this? The answer is yes, maybe indirectly, I think, because
// setup.ts
const db = new Kysely({
  dialect: new PgliteDialect({
    database: new PGlite(),
  }),
});
mock.module("../db", async () => {
  return {
    db: () => db,
    dbMiddleware: new Elysia({ name: "db" }).derive(
      { as: "global" },
      async () => {
        return {
          db: db,
        };
      }
    ),
  };
});
// only when the following code is added do we segfault
const c = client.decorate({
  env: {
    JWT_SECRET: "TESTING_JWT",
  },
});
  1. So maybe the usage of db inside elysia causes this segfault? But it can't, because once I've reduced it down to the point where the elysia export is literally just this:
const app = new Elysia({
  aot: false,
}).use(helpers);
  1. we still get segfaults (helpers doesn't touch db)
  2. Ok, so its not the db because when all db references in elysia are removed the segfaults go away, and its not images because when all images references are removed it goes away...
  3. And now I can't even reproduce it despite the codebase being at square one? Holy flake?? Edit: I did reproduce it again at square one:
eeek! ``` evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: [ "", "" ] prerun(C-node) worker= false Running in main thread, faking onCustomMessage ^C evan@EvanAir pinlog % bun test bun test v1.1.24 (85a32991) oss/server/middleware/auth.spec.ts: ============================================================ Bun v1.1.24 (85a32991) macOS Silicon macOS v14.0 Args: "bun" "test" Features: jsc(2) bunfig macros(5) transpiler_cache(3) tsconfig_paths(2) tsconfig(3) Builtins: "node:fs" "node:fs/promises" "node:os" "node:path" "node:string_decoder" "node:tty" "node:util/types" Elapsed: 806ms | User: 108ms | Sys: 97ms RSS: 92.67MB | Peak: 92.67MB | Commit: 1.07GB | Faults: 1443 panic: Segmentation fault at address 0x8 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.24/Mt185a3299AiiggxD__+zt3Q214kV2463Qmg9kV2463Quv/lV+1q4Quml2QA2AQ zsh: trace trap bun test ```

I'm sorta really struggling to create a MRE here, I'm happy to do further debugging to your guidance. I wish I could point to the source repository as well but it isn't fully open source (50% of it is open source here). I would be willing to add you as a collaborator to the private repository if it is deemed necessary.

Edits

  1. Looking around in the issues, it is possibly related to our use of macros as well? the syntax error in this file /Users/evan/Code/pinlog/oss/mailer/index.ts: (see below) would also indicate this. If that is the case, the error trace would look like setup.ts > elysia > helpers > route > mailer/index.ts > {mocked db, macro}

Relevant log output

No response

Stack Trace (bun.report)

Bun v1.1.24 ([`85a3299`](<https://github.com/oven-sh/bun/tree/85a32991152871b06becd6b411eb663008369769>)) on macos aarch64 [TestCommand]

Segmentation fault at address 0x1000003DF

- *2 unknown/js code*
- `src.js_parser.NewParser_`
- `src.js_parser.NewParser_`
- `src.js_parser.NewParser_`
- `src.js_parser.NewParser_`
- `src.js_parser.NewParser_`
- `src.js_parser.NewParser_`
- `src.js_parser.Parser._parse__anon_112008`
- `src.cache.JavaScript.parse`

<!-- from bun.report: W3COj-T2xmYHM-8KEkH- -->

or

Bun v1.1.24 ([`85a3299`](<https://github.com/oven-sh/bun/tree/85a32991152871b06becd6b411eb663008369769>)) on macos aarch64 [TestCommand]

Segmentation fault at address 0xB8E8028812800008

- *2 unknown/js code*
- `src.js_printer.NewPrinter`
- `src.js_printer.NewPrinter`
- `src.js_printer.NewPrinter`
- `src.js_printer.NewPrinter`
- `src.js_printer.NewPrinter`
- `src.js_printer.NewPrinter`
- `src.js_printer.NewPrinter`
- `src.bundler.Bundler.printWithSourceMap__anon_92451`

<!-- from bun.report: ijE4bk7pPKxPwkB_jotH -->

or

zsh: illegal hardware instruction bun test

or, by far the weirdest one:

oss/server/middleware/auth.spec.ts:

# Unhandled error between tests
-------------------------------
33 |   "sign-up",
34 |   "deleted-account",
35 |   "prereg",
36 | ] as const;
37 | 
38 | let _templates = templates();
     ^
SyntaxError: Unexpected token '.'. Expected a ':' following the property name 'import'.
      at <parse> (/Users/evan/Code/pinlog/oss/mailer/index.ts:38:1)
-------------------------------

(this error is nonsensical)

or it just works

boehs commented 2 months ago

Possible duplicate of #11730

utensil commented 2 months ago

I have a similar issue:

Related code:

// bun install @myriaddreamin/typst.ts @myriaddreamin/typst-ts-web-compiler @myriaddreamin/typst-ts-renderer
// following https://myriad-dreamin.github.io/typst.ts/cookery/guide/all-in-one.html
import { $typst } from '@myriaddreamin/typst.ts/dist/esm/contrib/snippet.mjs';
// The following paths come from https://github.com/Myriad-Dreamin/typst.ts/blob/main/packages/typst.ts/examples/all-in-one-lite.html
import typst_ts_web_compiler_bg from '@myriaddreamin/typst-ts-web-compiler/pkg/typst_ts_web_compiler_bg.wasm';
import typst_ts_renderer_bg from '@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm';

$typst.setCompilerInitOptions({
    getModule: () => typst_ts_web_compiler_bg,
});
$typst.setRendererInitOptions({
    getModule: () => typst_ts_renderer_bg,
});

Stack Trace (bun.report)

Bun v1.1.25 (fe62a61) on macos aarch64 [BuildCommand]

Segmentation fault at address 0x00000004