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

src, loader: return promises from link #18394

Closed
wants to merge 2 commits into from

Conversation

devsnek
Copy link
Member

@devsnek devsnek commented Jan 26, 2018

Returns the promises created by link so that they can be awaited to get
rid of race conditions while resolving and loading modules.

closes #18249

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

loader, src

@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Jan 26, 2018
@Fishrock123
Copy link
Contributor

@@ -173,9 +177,11 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
}
Local<Promise> resolve_promise = resolve_return_value.As<Promise>();
obj->resolve_cache_[specifier_std].Reset(env->isolate(), resolve_promise);

USE(promises->Set(mod_context, specifier, resolve_promise));
Copy link
Member

Choose a reason for hiding this comment

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

This should crash if the Set fails (promises->Set().FromJust()). USE is only for when we don’t care about the result of execution, and should be used sparingly.

@TimothyGu
Copy link
Member

TimothyGu commented Jan 26, 2018

Why is this patch preferred over #18249 (comment)?

Edit: answered on IRC:

@devsnek | @TimothyGu in reference to 18394 this seemed simpler
@devsnek | and made me not have to pass around v8::Object

const jobs = [];
const promises = this.module.link(async (specifier) => {
const job = await this.loader.getModuleJob(specifier, url);
jobs.push(job);
Copy link
Member

Choose a reason for hiding this comment

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

If we were to keep a jobs array, we would want to store the promises from getModuleJob to ensure determinism in the order of jobs.

Returns the promises created by link so that they can be awaited to get
rid of race conditions while resolving and loading modules.
@devsnek
Copy link
Member Author

devsnek commented Jan 28, 2018

comments above addressed

@BridgeAR
Copy link
Member

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jan 31, 2018
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request Feb 1, 2018
Returns the promises created by link so that they can be awaited to get
rid of race conditions while resolving and loading modules.

PR-URL: nodejs#18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
@BridgeAR
Copy link
Member

BridgeAR commented Feb 1, 2018

Landed in edbcf7c

@TimothyGu
Copy link
Member

Unfortunately this requires a corresponding change in lib/internal/vm/Module.js, which did not exist when this PR was created...

@devsnek devsnek deleted the fix/module-link-timing branch February 1, 2018 15:14
@@ -173,9 +177,11 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
}
Local<Promise> resolve_promise = resolve_return_value.As<Promise>();
obj->resolve_cache_[specifier_std].Reset(env->isolate(), resolve_promise);

promises->Set(mod_context, specifier, resolve_promise).FromJust();

Choose a reason for hiding this comment

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

This line does not set array items (i.e. it does not set promises[0], promises[1], etc.) It sets promises[specifier] i.e. some non-array properties of promises object. So, the array returned from this function is not populated properly and the corresponding await SafePromise.all(promises) does not do what was expected. There should be i instead of specifier, i.e., promises->Set(mod_context, i, resolve_promise).FromJust();, right?

Copy link
Member Author

@devsnek devsnek Feb 1, 2018

Choose a reason for hiding this comment

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

yea i actually just caught this while testing my changes for vm.Module. i'm kinda sad this pr landed without tests, as it doesn't work. i have another pr incoming anyway, which will fix this, and includes tests that will actually ensure this behavior does what it is supposed to do.

Copy link
Member

Choose a reason for hiding this comment

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

Should've caught this while reviewing... my bad as well.

@addaleax addaleax removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Feb 4, 2018
TimothyGu pushed a commit that referenced this pull request Feb 4, 2018
This commit fixes up some issues in #18394.

* Switch vm.Module internals to use the new link method properly
* Fix bug with ModuleWrap::Link
* Add tests for ModuleWrap::Link

PR-URL: #18509
Fixes: #18249
Refs: #18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
@MylesBorins
Copy link
Contributor

This lands cleanly, but is causing v9.x to break during compilation. Should it be backported?

../src/module_wrap.cc:154:9: error: unknown type name 'Array'; did you mean 'v8::Array'?
  Local<Array> promises = Array::New(isolate,
        ^~~~~
        v8::Array
../deps/v8/include/v8.h:3553:17: note: 'v8::Array' declared here
class V8_EXPORT Array : public Object {
                ^
../src/module_wrap.cc:154:27: error: use of undeclared identifier 'Array'; did you mean 'v8::Array'?
  Local<Array> promises = Array::New(isolate,
                          ^~~~~
                          v8::Array
../deps/v8/include/v8.h:3553:17: note: 'v8::Array' declared here
class V8_EXPORT Array : public Object {
                ^
2 errors generated.
make[1]: *** [/Users/mborins/code/node/v9.x/out/Release/obj.target/node_lib/src/module_wrap.o] Error 1

@addaleax
Copy link
Member

@MylesBorins These kinds of errors can be fixed by adding using v8::Array to the top of the file. If you want, you can always do that during backporting – it’s completely harmless, the worst thing that can happen is the linter complaining if the list of using statements isn’t alphabetically sorted.

@MylesBorins
Copy link
Contributor

@addaleax thanks, that worked

MylesBorins pushed a commit that referenced this pull request Feb 21, 2018
Returns the promises created by link so that they can be awaited to get
rid of race conditions while resolving and loading modules.

PR-URL: #18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
devsnek added a commit to devsnek/node that referenced this pull request Feb 21, 2018
This commit fixes up some issues in nodejs#18394.

* Switch vm.Module internals to use the new link method properly
* Fix bug with ModuleWrap::Link
* Add tests for ModuleWrap::Link

PR-URL: nodejs#18509
Fixes: nodejs#18249
Refs: nodejs#18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
MylesBorins pushed a commit that referenced this pull request Feb 21, 2018
Returns the promises created by link so that they can be awaited to get
rid of race conditions while resolving and loading modules.

PR-URL: #18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
MylesBorins pushed a commit that referenced this pull request Feb 21, 2018
This commit fixes up some issues in #18394.

* Switch vm.Module internals to use the new link method properly
* Fix bug with ModuleWrap::Link
* Add tests for ModuleWrap::Link

PR-URL: #18509
Fixes: #18249
Refs: #18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
MylesBorins pushed a commit that referenced this pull request Feb 21, 2018
Returns the promises created by link so that they can be awaited to get
rid of race conditions while resolving and loading modules.

PR-URL: #18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
MylesBorins pushed a commit that referenced this pull request Feb 21, 2018
This commit fixes up some issues in #18394.

* Switch vm.Module internals to use the new link method properly
* Fix bug with ModuleWrap::Link
* Add tests for ModuleWrap::Link

PR-URL: #18509
Fixes: #18249
Refs: #18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
@MylesBorins MylesBorins mentioned this pull request Feb 21, 2018
MylesBorins pushed a commit that referenced this pull request Feb 21, 2018
This commit fixes up some issues in #18394.

* Switch vm.Module internals to use the new link method properly
* Fix bug with ModuleWrap::Link
* Add tests for ModuleWrap::Link

PR-URL: #18509
Fixes: #18249
Refs: #18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
@MylesBorins
Copy link
Contributor

Should this be backported to v8.x-staging? If yes please follow the guide and raise a backport PR, if not let me know or add the dont-land-on label.

MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
Returns the promises created by link so that they can be awaited to get
rid of race conditions while resolving and loading modules.

PR-URL: nodejs#18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
This commit fixes up some issues in nodejs#18394.

* Switch vm.Module internals to use the new link method properly
* Fix bug with ModuleWrap::Link
* Add tests for ModuleWrap::Link

PR-URL: nodejs#18509
Fixes: nodejs#18249
Refs: nodejs#18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fragile/buggy implementation of ES module loading
8 participants