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

Segmentation fault when running inside worker threads (Ubuntu) #64

Open
Newbie012 opened this issue May 15, 2024 · 4 comments
Open

Segmentation fault when running inside worker threads (Ubuntu) #64

Newbie012 opened this issue May 15, 2024 · 4 comments

Comments

@Newbie012
Copy link

Issue

When I run the following test:

import { it } from "vitest";
import { parseQuery } from "libpg-query";

it("should not throw segfault", async () => {
    await parseQuery("SELECT 1");
})

The test passes, and I get the parsed query, but the process exists with error code 139:

RUN  v1.6.0 /home/runner/work/test-libpg-query-seg-fault/test-libpg-query-seg-fault

 ✓ index.test.ts  (1 test) 2ms

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  06:48:56
   Duration  244ms (transform 29ms, setup 0ms, collect 22ms, tests 2ms, environment 0ms, prepare 73ms)

Segmentation fault (core dumped)
 ELIFECYCLE  Test failed. See above for more details.
Error: Process completed with exit code [13](https://github.com/Newbie012/test-libpg-query-seg-fault/actions/runs/9091229513/job/24985419636#step:8:14)9.

(see execution - https://github.com/Newbie012/test-libpg-query-seg-fault/actions/runs/9091229513/job/24985419636)

Expected

Should not throw segmentation fault

Reproduction:

  1. git clone [email protected]:Newbie012/test-libpg-query-seg-fault.git
  2. pnpm install
  3. pnpm test

Additional info

  • It works as expected on my Mac machine.
  • On Ubuntu, it fails when I use (matrix): node [18, 20] and libpg-query [14, 15, 16].
  • I haven't tested it on other Linux distributions.
@pyramation
Copy link
Collaborator

Thank you for the reproduction! Will dig into this.

@pyramation
Copy link
Collaborator

I just tested your exact example and it works if you use jest, which has your issue title points out, this doesn't work with vitest.

It seems that the issue is not necessarily related to libpg-query, as it works great in other testing environments. I'm not too familiar with vitest or how it would work with some of the dependencies we use to compile.

I wonder if this was previously working with an older version of libpg-query?

@Newbie012
Copy link
Author

Thanks for looking into it so quickly!

I opened an issue in vitest as well with a proper execution.

@Newbie012
Copy link
Author

I think it happens when parseQuerySync is invoked inside worker threads. Here's a minimal reproduction (see execution):

const { Worker, isMainThread, parentPort, workerData } = require("node:worker_threads");

if (isMainThread) {
    function run(query) {
        return new Promise((resolve, reject) => {
            const worker = new Worker(__filename, { workerData: query });
            worker.on("message", resolve);
            worker.on("error", reject);
            worker.on("exit", (code) => {
                if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));
            });
        });
    };

    run("select 1 as x").then(x => console.log(x))
} else {
    const { parseQuerySync } = require("libpg-query");
    const query = workerData;
    parentPort.postMessage(parseQuerySync(query));
}

@Newbie012 Newbie012 changed the title Segmentation fault when running inside Vitest on Ubuntu Segmentation fault when running inside worker threads (Ubuntu) May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants