nodejs / nodejs/node

VM ESM with dynamic imports resolves promises before linking is complete

Abierto
#33,216 14 comentarios 5 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

esm vm
Lenguaje dominante
JavaScript
Estrellas
122k
Forks
37.3k
Merge medio
4 d 2 h
PR fusionados (30 d)
283

Descripción

  • Version: v14.1.0
  • Platform: Darwin Simens-MacBook-Pro.local 18.7.0 Darwin Kernel Version 18.7.0: Mon Feb 10 21:08:45 PST 2020; root:xnu-4903.278.28~1/RELEASE_X86_64 x86_64
  • Subsystem: VM, ESM
What steps will reproduce the bug?

See https://github.com/SimenB/node-vm-esm-promise-awaiting

Run node --experimental-vm-modules index.js to reproduce.

How often does it reproduce? Is there a required condition?

Almost every time. There seems to be some race condition in there. I've added a manual timeout in there - tweak its numbers to see different behavior. In the code the timeout is 1 second and then one of the tests fail almost every time. Increasing the timeoyt will often make both tests fail

What is the expected behavior?

All linking and execution should happen before the promises resolve.

What do you see instead?

The promises resolve, so the internal state of completed is set to true, which later throws.

Additional information

The reproduction is adapted from a bug report to Jest (https://github.com/facebook/jest/issues/9430#issuecomment-622664754), so apologies if it looks a little wonky. I've tried to emulate sorta what happens under the hood in Jest down to solely node core modules.

Note that I might very well have gotten some semantics wrong in the dynamic linking, so please tell me if I'm doing something really dumb in the reproduction (except having no module cache at all) - I've probably made the same mistake in Jest's implementation. Main difference is that the calls will linger - In jest these would all be inside test() calls or some such that we execute later. I don't think it impacts the reproduction much though - the promises resolve seconds before linking complete, so I don't think it's necessarily tied to dangling promises. I might very well be wrong though.

For some extra context if it's helpful, here's Jest's implementation: https://github.com/facebook/jest/blob/7a3c9977847cc9fafed6f6662289f3c35e44e0c6/packages/jest-runtime/src/index.ts#L323-L456

It's spread a bit around, but those are the essential bits where we use the VM APIs.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con index.js en la reproducción enlazada y ejecuta node --experimental-vm-modules index.js en el escenario de VM/ESM reportado. Comprueba cuándo se resuelven las promises en relación con el linking y la ejecución; el issue estará resuelto cuando el linking y la ejecución terminen sistemáticamente antes de que se resuelvan esas promises.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, node.js
Área
backend
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
42/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.