oven-sh / bun

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

Seg faults while running our bundler script using bun #11650

Open RobertoGeuke opened 4 months ago

RobertoGeuke commented 4 months ago

How can we reproduce the crash?

We sometimes get seg faults when we run our bundle script using bun. I reproduced this a few times by running 100 invocations of our script and it happens once in ~60 invocations. Reproduced the seg fault again to generate a core dump and included backtrace from this core dump in the relevant log output.

Good to note that our bundle.ts script uses worked threads to parallelize our bundling tasks.

Relevant log output

Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
--Type <RET> for more, q to quit, c to continue without paging--
Core was generated by `/home/robertogeuke/bun1.1.12-profile scripts/bundle.ts --product=appMealkit --p'.
Program terminated with signal SIGILL, Illegal instruction.

warning: Unexpected size of section `.reg-xstate/2764516' in core file.
#0  0x0000559eefc95028 in src.crash_handler.crash ()
[Current thread is 1 (Thread 0x7f75cd17f700 (LWP 2764516))]
(gdb) bt
#0  0x0000559eefc95028 in src.crash_handler.crash ()
#1  0x0000559eefb7ca88 in src.crash_handler.crashHandler ()
#2  0x0000559eef8b39d5 in src.crash_handler.handleSegfaultPosix ()
#3  0x0000559ef107e28c in WTF::jscSignalHandler(int, siginfo_t*, void*) ()
#4  <signal handler called>
#5  0x0000559eefc9b967 in Bun__getDefaultGlobal ()
#6  0x0000559ef0bfe595 in computeErrorInfoWithoutPrepareStackTrace(JSC::VM&, Zig::GlobalObject*, WTF::Vector<JSC::StackFrame, 0ul, WTF::CrashOnOverflow, 16ul, WTF::FastMalloc>&, unsigned int&, unsigned int&, WTF::String&, JSC::JSObject*) ()
#7  0x0000559ef0bfe2b5 in computeErrorInfo(JSC::VM&, WTF::Vector<JSC::StackFrame, 0ul, WTF::CrashOnOverflow, 16ul, WTF::FastMalloc>&, unsigned int&, unsigned int&, WTF::String&, JSC::JSObject*) ()
#8  0x0000559ef0c050fa in WTF::Detail::CallableWrapper<WTF::String (*)(JSC::VM&, WTF::Vector<JSC::StackFrame, 0ul, WTF::CrashOnOverflow, 16ul, WTF::FastMalloc>&, unsigned int&, unsigned int&, WTF::String&, JSC::JSObject*), WTF::String, JSC::VM&, WTF::Vector<JSC::StackFrame, 0ul, WTF::CrashOnOverflow, 16ul, WTF::FastMalloc>&, unsigned int&, unsigned int&, WTF::String&, JSC::JSObject*>::call(JSC::VM&, WTF::Vector<JSC::StackFrame, 0ul, WTF::CrashOnOverflow, 16ul, WTF::FastMalloc>&, unsigned int&, unsigned int&, WTF::String&, JSC::JSObject*) ()
#9  0x0000559ef1d59e75 in JSC::ErrorInstance::computeErrorInfo(JSC::VM&, bool) ()
#10 0x0000559ef19534c4 in JSC::Heap::runEndPhase(JSC::GCConductor) ()
#11 0x0000559ef194e26b in JSC::Heap::runCurrentPhase(JSC::GCConductor, JSC::CurrentThreadState*) ()
#12 0x0000559ef194e03f in JSC::Heap::HeapThread::work() ()
#13 0x0000559ef1979654 in WTF::Detail::CallableWrapper<WTF::AutomaticThread::start(WTF::AbstractLocker const&)::$_0, void>::call() ()
#14 0x0000559ef107f127 in WTF::wtfThreadEntryPoint(void*) ()
#15 0x00007f78d3f7a609 in start_thread () from /lib/x86_64-linux-gnu/libpthread.so.0
#16 0x00007f78d40b4353 in clone () from /lib/x86_64-linux-gnu/libc.so.6

Stack Trace (bun.report)

Bun v1.1.12 (43f0913) on linux x86_64 [AutoCommand]

Segmentation fault at address 0x00007058

Jarred-Sumner commented 4 months ago

Duplicate of https://github.com/oven-sh/bun/issues/11568

Jarred-Sumner commented 4 months ago

Can you provide some code for us to reproduce the issue? We know there's something going wrong while it's formatting the stack trace for an Error object. Don't know any more than that though.

Jarred-Sumner commented 4 months ago

Actually, this might've been fixed today. Can you try bun upgrade --canary

RobertoGeuke commented 3 months ago

Tested it with the most recent canary version but still got the segfault after ~100 invocations of our script. Unfortunately I can't share our code, but basically our bundle.ts script receives an entry file and bundles this file together with all it's dependencies into a bundle.js file. Non-js dependencies (images, fonts, etc.) will also be chased and compiled into usable assets.

My hypothesis is that something the segfault occurs when we generate a .js script which can render static versions of our react pages. The script will be used as follows: bun bundle.js page-subscribe nl > built/page-subscribe.nl.html. The TS code of this script looks kind-off like:

    1   // Entry point when building static HTML (build time, using node).
    1 /// <reference types="node" />
    2 import {createElement} from 'react';
    3 import {renderToString} from 'react-dom/server';
    4
    5 import {FrontPage} from 'FrontPage';
    6 import {getPage} from 'index';
    7
    8 import type Country from 'Country';
    9
   10 const page = process.argv[2];
   11 const country = process.argv[3];
   12 const spinner = process.argv[4];
   13
   14 function isCountry(country: string): country is Country {
   15     return country === 'nl' || country === 'be';
   16 }
   17 if (!isCountry(country)) {
   18     throw new Error(`Invalid country: ${country}`);
   19 }
   20 const staticProps = {country, spinner: spinner || undefined};
   21 // Once we have more complex staticProps than just country, consume from stdin:
   22 // const stdin = readFileSync(0, 'utf-8')
   23 // const props = JSON.parse(stdin)
   24
   25 let html = renderToString(createElement(FrontPage, {Page: getPage(page), staticProps}));
   26 // Replace content within <style> tags with the HTML-unescaped version. a &gt; b becomes a > b.
   27 // Notice: we are intentionally checking for <style and not <style> because there's extra attributes in that tag.
   28 html = html.replace(/<style[\s\S]+<\/style>/g, (match) =>
   29     match
   30         .replace(/&gt;/g, '>')
   31         .replace(/&lt;/g, '<')
   32         .replace(/&#x27;/g, "'")
   33 );
   34 process.stdout.write('<!DOCTYPE html>');
   35 process.stdout.write(html);
   36 process.stdout.write(`<script>window.staticRender=${JSON.stringify({page, staticProps})};</script>`);