oven-sh / bun

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

node readdir causes Segmentation fault (or EBADF: Bad file descriptor) #12166

Open chicken-suop opened 4 months ago

chicken-suop commented 4 months ago

How can we reproduce the crash?

Crash caused by this code running inside chokidar watching src

import { readdir, readFile, writeFile } from 'node:fs/promises';

const getMenuFilePaths = async (dir) => {
    let entries;
    try {
        entries = await readdir(dir, { withFileTypes: true });
    } catch (error) {
        logger.error(`Error reading directory ${dir}: ${error.message}`);
        return [];
    }
    const files = await Promise.all(entries.map(async entry => {
        const res = path.resolve(dir, entry.name);
        if (entry.isDirectory()) return getMenuFilePaths(res);
        if (['html', 'htm'].includes(fileName.split('.').pop().toLowerCase())) return res;
        return [];
    }));
    return files.flat();
};

getMenuFilePaths('src/foo')

When the try/catch is removed, this error is thrown many times:

EBADF: Bad file descriptor
   errno: -9
 syscall: "read"

Relevant log output

EBADF: Bad file descriptor
   errno: -9
 syscall: "read"

============================================================
Bun v1.1.17 (bb66bba1) Linux x64
Linux Kernel v6.2.0 | glibc v2.35
Args: "bun" "Build/Containers/static/builder/build.mjs" "-o" "Source" "-w"
Features: jsc Bun.stdin spawn transpiler_cache(6) tsconfig_paths tsconfig 
Builtins: "bun:main" "node:assert" "node:buffer" "node:child_process" "node:crypto" "node:events" "node:fs" "node:fs/promises" "node:os" "node:path" "node:stream" "node:string_decoder" "node:tty" "node:util" "node:util/types" "node:zlib" "node:worker_threads" 
Elapsed: 4295ms | User: 3864ms | Sys: 2093ms
RSS: 0.99GB | Peak: 0.32GB | Commit: 0.99GB | Faults: 0

panic(main thread): Segmentation fault at address 0x88E18
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.17/la1bb66bbaAqgggoH2lnhnE+xyQo1o6lD2xslgE______A2AwhniB

./watch: line 4: 891230 Illegal instruction     (core dumped) bun build.mjs -o src -w

Stack Trace (bun.report)

Bun v1.1.17 (bb66bba) on linux x86_64 [AutoCommand]

Segmentation fault at address 0x00088E18

chicken-suop commented 4 months ago

I tried installing the linux-x64-baseline build (even though my CPU supports AVX2) by modifying the install script, but this didn't fix the issue

chicken-suop commented 4 months ago

The issue was actually caused by running esbuild.build() in chokidar watch. Fixed by using esbuild's builtin watch method

Jarred-Sumner commented 4 months ago

Going to re-open this because it still seems to be a bug