Skip to content

node:vm microtaskMode: 'afterEvaluate' doesn't seem to be respected #20145

Closed
oven-sh/WebKit
#101

Description

@d2201

What version of Bun is running?

1.2.15+df017990a

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

Run the following code:

import vm from 'vm';

const context = vm.createContext({
  console,
}, {
  microtaskMode: 'afterEvaluate',
});

const code = `
  Promise.resolve().then(() => {
    console.log('Microtask inside VM');
  });
  console.log('End of VM code');
`;

console.log('Before vm.runInContext');

Promise.resolve().then(() => {
  console.log('Microtask outside VM');
});

vm.runInContext(code, context);

console.log('After vm.runInContext');

What is the expected behavior?

In node environment:

$ npx tsx index.ts
Before vm.runInContext
End of VM code
Microtask inside VM
After vm.runInContext
Microtask outside VM

What do you see instead?

$ bun run index.ts
Before vm.runInContext
End of VM code
After vm.runInContext
Microtask outside VM
Microtask inside VM

Additional information

Currently it does seem like Bun doesn't respect the option. When I remove the option, then the output is the same for both environments

Activity

  1. added
    node.jsCompatibility with Node.js APIs
    and removed on Jun 3, 2025
  2. nhattran998 commented on Jun 4, 2025

    @nhattran998

    +1 for the same issue

  3. Jarred-Sumner commented on Jun 4, 2025

    @Jarred-Sumner
    Collaborator

    can you explain the usecase more?

  4. d2201 commented on Jun 4, 2025

    @d2201
    Author

    @Jarred-Sumner the tool I'm using (temporal.io) depends on VM to deliver it's core functionality.

    At a high level, the workflow looks like this:

    • Poll for work
    • Execute work inside the VM
    • Notify the control server once the work is completed

    This works flawlessly in Node.js. However, in Bun, it fails -- the information sent to the control server ends up being empty.

    This is the long-standing issue we're trying to resolve: temporalio/sdk-typescript#1334

  5. x04 commented on Jul 30, 2025

    @x04
    Contributor

    I am not sure if this can even be fixed without changes to JSC because JSC does not have context specific micro task queues, you only have access to vm.drainMicrotasks() which is obviously a global queue which means the Microtask outside VM promise will be ahead in the queue relative to the Microtask inside VM that is created later.

  6. kunalsinghal commented on Aug 13, 2025

    @kunalsinghal

    +1 to the issue; we're sadly forced to temporarily move to node because of this issue

  7. Tanner-Scadden commented on Sep 8, 2025

    @Tanner-Scadden

    +1 to this issue, we have to deploy to node until this is fixed, and use vitest for the test suite, vs being able to do it all through bun.

  8. matvejs16 commented on Oct 11, 2025

    @matvejs16

    Hello! I've seen the recent improvements for node:vm in the Bun 1.3 changelog. Could you please clarify if this issue regarding microtaskMode: 'afterEvaluate' was addressed as part of those changes? I haven't had a chance to test it myself yet. Thanks!

  9. yordis commented on Jan 22, 2026

    @yordis

    temporalio/sdk-typescript#1334 is blocking us as well

    @matvejs16 I think I did-did tested in latest bun and it was still an issue, but I will confirm in 2 weeks once I am back home.

  10. robobun commented on Jul 24, 2026

    @robobun
    Collaborator

    Fixed by #32018.

    Verified on main at df84f8d: microtaskMode: 'afterEvaluate' now drains the VM context's microtasks before vm.runInContext returns, matching Node.js ordering.

    $ bun repro.js
    Before vm.runInContext
    End of VM code
    Microtask inside VM
    After vm.runInContext
    Microtask outside VM
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingnode.jsCompatibility with Node.js APIs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions