fix: handle lazy initialization and circular dependencies#229
Merged
timfish merged 3 commits intonodejs:mainfrom Jan 20, 2026
Merged
fix: handle lazy initialization and circular dependencies#229timfish merged 3 commits intonodejs:mainfrom
timfish merged 3 commits intonodejs:mainfrom
Conversation
When finding circular dependencies or undefined exports, retry loading with a delay. That way most hooking will work as expected. It uses a backoff time and has a small finite retry number. There is no guarantee that an async initialization will be detected, but it should match pretty much all cases known to fail so far. In addition, we use the parser for Node.js 16+ now. That was needed to make the circular structure fix from Node.js 16 on. Fixes: nodejs#227 Fixes: nodejs#32
3e8b14d to
5ef6acc
Compare
jsumners-nr
reviewed
Jan 20, 2026
Comment on lines
+6
to
+15
| // Defer initialization with a timeout. This means that by the time an `import()` | ||
| // resolves, native ESM will have initialized the binding, but IITM's wrapper may | ||
| // have snapshotted it too early. | ||
| setTimeout(() => { | ||
| RunTree = class RunTree { | ||
| constructor () { | ||
| this.ok = true | ||
| } | ||
| } | ||
| }, 5) |
Contributor
There was a problem hiding this comment.
Do you think this will be fragile with respect to timing? Would a setImmediate or nextTick be more robust?
Member
Author
There was a problem hiding this comment.
I think the delay of a total of ~61 ms should suffice to not be flaky (we retry with a single millisecond, afterwards once more with 10 and afterwards with 50).
jsumners-nr
approved these changes
Jan 20, 2026
timfish
approved these changes
Jan 20, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When finding circular dependencies or undefined exports, retry loading with a delay. That way most hooking will work as expected.
It uses a backoff time and has a small finite retry number. There is no guarantee that an async initialization will be detected, but it should match pretty much all cases known to fail so far.
In addition, we use the parser for Node.js 16+ now. That was needed to make the circular structure fix from Node.js 16 on.
Fixes: #227
Fixes: #32