★ wanayoo — archive 1999 https://github.com/nodejs/node/pull/18394Nouvelle recherche | Portail wanayoo
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

@Fishrock123
Copy link
Member

@Fishrock123 Fishrock123 commented Jan 26, 2018

@@ -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

@TimothyGu TimothyGu Jan 26, 2018

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 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

@TimothyGu TimothyGu Jan 27, 2018

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.

@devsnek devsnek force-pushed the fix/module-link-timing branch from 0fbccf9 to 8e1fb42 Compare Jan 27, 2018
devsnek added 2 commits Jan 28, 2018
Returns the promises created by link so that they can be awaited to get
rid of race conditions while resolving and loading modules.
@devsnek devsnek force-pushed the fix/module-link-timing branch from be04b4a to f936321 Compare Jan 28, 2018
@devsnek
Copy link
Member Author

@devsnek devsnek commented Jan 28, 2018

comments above addressed

@BridgeAR
Copy link
Member

@BridgeAR BridgeAR commented Jan 31, 2018

BridgeAR added a commit to BridgeAR/node that referenced this issue 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 BridgeAR commented Feb 1, 2018

Landed in edbcf7c

@TimothyGu
Copy link
Member

@TimothyGu TimothyGu commented Feb 1, 2018

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 Feb 1, 2018
@@ -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

@TimothyGu TimothyGu Feb 1, 2018

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.

TimothyGu added a commit that referenced this issue 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
Member

@MylesBorins MylesBorins commented Feb 20, 2018

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

@addaleax addaleax commented Feb 20, 2018

@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
Member

@MylesBorins MylesBorins commented Feb 21, 2018

@addaleax thanks, that worked

MylesBorins added a commit that referenced this issue 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 issue 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 added a commit that referenced this issue 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 added a commit that referenced this issue 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 added a commit that referenced this issue 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 added a commit that referenced this issue 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 added a commit that referenced this issue 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
Member

@MylesBorins MylesBorins commented Mar 20, 2018

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 added a commit to MayaLekova/node that referenced this issue 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 added a commit to MayaLekova/node that referenced this issue 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
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

8 participants