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

Assert we drained the microtask queue since the last time we called into JavaScript #8646

Merged
merged 4 commits into from
Feb 3, 2024

Conversation

Jarred-Sumner
Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner commented Feb 2, 2024

What does this PR do?

This adds an assertion in debug builds that checks we called drainMicrotasks after calling into a JavaScript function.

This adds a helper method EventLoop.runCallback for calling into JavaScript functions from the top of the event loop. This will handle exceptions and release weakrefs and anything else we may need to do in the future.

This also fixes cases where we weren't draining microtasks in:

  • IPC
  • open() callback in Bun.listen/Bun.connect
  • open and close callbacks in Bun.serve() WebSockets
  • H2

The panic looks like this:

uh-oh: 1 JavaScript functions were called outside of the microtask queue without draining microtasks.

Last function name: open

Use EventLoop.runCallback() to run JavaScript functions outside of the microtask queue.

Failing to do this can lead to a large number of microtasks being queued and not being drained, which can lead to a large amount of memory being used and application slowdown.
bun will crash now 😭😭😭

It only tracks function names if BUN_TRACK_LAST_FN_NAME=1

How did you verify your code works?

Manually

Copy link
Contributor

github-actions bot commented Feb 2, 2024

Copy link
Contributor

github-actions bot commented Feb 2, 2024

Copy link
Contributor

github-actions bot commented Feb 2, 2024

Copy link
Contributor

github-actions bot commented Feb 2, 2024

Copy link
Contributor

github-actions bot commented Feb 2, 2024

❌🪟 @Jarred-Sumner, there are 10 test regressions on Windows x86_64

  • test\bundler\cli.test.ts
  • test\bundler\esbuild\loader.test.ts
  • test\cli\run\transpiler-cache.test.ts
  • test\js\bun\console\console-iterator.test.ts
  • test\js\bun\util\bun-file-windows.test.ts
  • test\js\node\http\node-http.test.ts
  • test\js\node\module\node-module-module.test.js
  • test\js\third_party\postgres\postgres.test.ts
  • test\regression\issue\08095.test.ts
  • test\transpiler\template-literal.test.ts

Full Test Output

this.drainMicrotasksWithGlobal(this.global);
}

this.entered_event_loop_count -= 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be an assertion that it's never negative?

@Jarred-Sumner Jarred-Sumner merged commit c75e768 into main Feb 3, 2024
26 of 31 checks passed
@Jarred-Sumner Jarred-Sumner deleted the jarred/ticker branch February 3, 2024 15:40
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

Successfully merging this pull request may close these issues.

2 participants