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

Weird behaviour on SIGINT when using cc from bun:ffi #14043

Open
sadkebab opened this issue Sep 19, 2024 · 0 comments
Open

Weird behaviour on SIGINT when using cc from bun:ffi #14043

sadkebab opened this issue Sep 19, 2024 · 0 comments
Labels
bug Something isn't working needs triage

Comments

@sadkebab
Copy link

sadkebab commented Sep 19, 2024

What version of Bun is running?

1.1.28-canary.3+866a6d918

What platform is your computer?

Darwin 22.3.0 arm64 arm

What steps can reproduce the bug?

  1. Create a c file with a function to be imported in JS in a file called test.c
#include <stdio.h>
#include <stdlib.h>

int myFn()
{
  return 1;
}
  1. Compile the file in JS with the cc function in a file called c.js
import { cc } from "bun:ffi";

const {
  symbols: { myFn },
} = cc({
  source: "./test.c",
  symbols: {
    myFn: {
      returns: "int",
      rags: [],
    },
  },
});

export { myFn };
  1. Make a index.js file that does nothing for few seconds
function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

console.log("start");
await sleep(10000);
console.log("end");
  1. Run it with bun run index.js and press ctrl+C to send a SIGINT, the process will be terminated as expected
  2. Import myFn from c.js in index.js, even without ever calling it
import { myFn } from "./c.js";

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

console.log("start");
await sleep(10000);
console.log("end");
  1. Run it with bun run index.js and press ctrl+C to send a SIGINT, the execution will stop because "end" won't get printed but the process won't be terminated and it will start eating all the CPU power
Screenshot 2024-09-19 at 15 23 29
  1. If you add a handler on SIGINT it will be handled correctly and execution will stop if process.exit is called
import { myFn } from "./c.js";

process.on("SIGINT", async () => {
  process.exit(0);
});

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

console.log("start");
await sleep(10000);
console.log("end");

What is the expected behavior?

The process should have been terminated even without a handler on SIGINT, as it does when myFn is not imported in the index file.

What do you see instead?

Importing a function created with cc will cause weird behaviours on SIGINT

Additional information

This behaviour happens only in the current canary revision, in the stable version it works as expected.

@sadkebab sadkebab added bug Something isn't working needs triage labels Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
1 participant