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

Multi-workers serve - with same port #15656

Closed
cooffeeRequired opened this issue Dec 8, 2024 · 4 comments
Closed

Multi-workers serve - with same port #15656

cooffeeRequired opened this issue Dec 8, 2024 · 4 comments
Labels
crash An issue that could cause a crash needs investigate Needs to be investigated to find the root cause runtime windows An issue that is known to occur on Windows Worker uses the web or node worker api

Comments

@cooffeeRequired
Copy link

How can we reproduce the crash?

Asynchronmic serve...

import { serve } from "bun";
import { Worker, isMainThread, parentPort } from "worker_threads";

const defaultInstances = 3;

if (isMainThread) {
    console.log("Main thread: Starting server and spawning workers...");

    // Hlavní vlákno spouští server
    const server = serve({
        port: 3000,
        fetch: async (req) => {
            const workerIndex = Math.floor(Math.random() * defaultInstances);
            return await sendToWorker(workerIndex, req);
        },
    });

    console.log("Server running on port 3000");

    // Spuštění workerů
    const workers = [];
    for (let i = 0; i < defaultInstances; i++) {
        workers.push(new Worker(new URL(import.meta.url)));
    }

    const workerChannels = new Map(
        workers.map((worker, index) => [
            index,
            {
                worker,
                port: new MessageChannel().port1,
            },
        ])
    );

    async function sendToWorker(workerIndex: number, req: Request): Promise<Response> {
        const { worker, port } = workerChannels.get(workerIndex);
        return new Promise((resolve, reject) => {
            port.postMessage({ req: req.url });
            port.on("message", (response) => resolve(new Response(response)));
            port.on("error", (err) => reject(err));
        });
    }
} else {
    // Worker logika: zpracování požadavků
    console.log(`Worker started (Thread: ${process.pid})`);

    parentPort?.on("message", ({ req }) => {
        console.log(`Worker ${process.pid} handling request: ${req}`);
        parentPort?.postMessage(`Handled by worker ${process.pid}`);
    });
}

Relevant log output

C:\projects\bun-appiest git:[master]
bun run test.ts
Main thread: Starting server and spawning workers...
Server running on port 3000
Worker started (Thread: 32520)
Worker started (Thread: 32520)
Worker started (Thread: 32520)
============================================================
Bun v1.1.34 (5e5e7c60) Windows x64
Windows v.win10_fe
CPU: sse42 avx avx2
Args: "C:\Users\nexti\.bun\bin\bun.exe" "run" "test.ts"
Features: http_server jsc tsconfig workers_spawned(3) 
Builtins: "bun:main" "node:worker_threads" 
Elapsed: 263ms | User: 62ms | Sys: 46ms
RSS: 0.19GB | Peak: 0.19GB | Commit: 0.25GB | Faults: 47845

panic(main thread): Segmentation fault at address 0x18
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.34/wr15e5e7c6EggglggCgw2muC0m44yBigv1zB40q1zB__s3tnkCA2AwB

Stack Trace (bun.report)

Bun v1.1.34 (5e5e7c6) on windows x86_64 [RunCommand]

Segmentation fault at address 0x00000018

Features: workers_spawned, http_server, jsc, tsconfig

Sentry Issue: BUN-9H3

@cooffeeRequired cooffeeRequired added the crash An issue that could cause a crash label Dec 8, 2024
Copy link
Contributor

github-actions bot commented Dec 8, 2024

@cooffeeRequired, the latest version of Bun is v1.1.38, but this crash was reported on Bun v1.1.34.

Are you able to reproduce this crash on the latest version of Bun?

bun upgrade

@github-actions github-actions bot added runtime windows An issue that is known to occur on Windows labels Dec 8, 2024
@DonIsaac DonIsaac added Worker uses the web or node worker api needs investigate Needs to be investigated to find the root cause labels Dec 8, 2024
@cooffeeRequired
Copy link
Author

Ofcourse i can, let me check it, after updates.

@cooffeeRequired
Copy link
Author

new crash with latest version

Bun v1.1.38 ([`bf2f153`](<https://github.com/oven-sh/bun/tree/bf2f153f5ca45876bc88369273743cd6fa04881c>)) on windows x86_64 [RunCommand]

Segmentation fault at address 0x00000018

- [`ScriptExecutionContext.h:64`](<https://github.com/oven-sh/bun/blob/bf2f153f5ca45876bc88369273743cd6fa04881c/src/bun.js/bindings/ScriptExecutionContext.h#L64>): `WebCore::ScriptExecutionContext::jsGlobalObject`
- [`MessagePort.cpp:209`](<https://github.com/oven-sh/bun/blob/bf2f153f5ca45876bc88369273743cd6fa04881c/src/bun.js/bindings/webcore/MessagePort.cpp#L209>): `WebCore::MessagePort::postMessage`
- [`optional:388`](<https://github.com/oven-sh/bun/blob/bf2f153f5ca45876bc88369273743cd6fa04881c/C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.39.33519/include/optional#L388>): `std::optional<WebCore::Exception>::operator bool`
- [`JSMessagePort.cpp:290`](<https://github.com/oven-sh/bun/blob/bf2f153f5ca45876bc88369273743cd6fa04881c/src/bun.js/bindings/webcore/JSMessagePort.cpp#L290>): `WebCore::jsMessagePortPrototypeFunction_postMessageOverloadDispatcher`
- *2 unknown/js code*
- `llint_entry`

Features: workers\_spawned, http\_server, jsc, tsconfig

<!-- from bun.report: 1KljbdDJrc3HU_3k8Ap8 -->

<!-- sentry_id: 86385cdc4c1c6d084ea6037266fb7e20 -->

Totaly same script, let me try it on linux

@Jarred-Sumner
Copy link
Collaborator

Tracking this in #15964

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 needs investigate Needs to be investigated to find the root cause runtime windows An issue that is known to occur on Windows Worker uses the web or node worker api
Projects
None yet
Development

No branches or pull requests

3 participants