GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — src: fix race on modpending by migrating it to a thread local by rpetrich · Pull Request #21611 · nodejs/node

Conversation

@rpetrich

@rpetrich rpetrich commented Jun 30, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Fixes a rare race condition on modpending when two native modules are loaded simultaneously on different threads by migrating modpending to be a thread local.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows [commit guidelines]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Jun 30, 2018
addaleax
addaleax reviewed Jun 30, 2018
View reviewed changes

@addaleax addaleax left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks for the PR!

Just fyi, there are some alternative approaches that address this problem more completely – #20759 goes a bit into this direction (or at least touches a lot of this code), and I wanted to fix up addaleax/node@541ec98 to work as a “proper” solution to this problem.

In particular, Node shouldn’t load addons from Workers that don’t explicitly state that they support being loaded in such an environment – Many addons create some kind of libuv handles, and don’t install proper cleanup hooks for when the Worker shuts down that close those handles

If you want to help with that, that would be appreciated a lot 💙

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/node.cc Outdated

@addaleax addaleax Jun 30, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Almost any use of Environment::GetThreadLocalEnv() is going to be buggy, because Node.js generally seeks to support embedders who create multiple Environments per thread :(

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@rpetrich rpetrich Jun 30, 2018

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@addaleax Thanks, I was unaware of that. I've migrated to using a thread local instead, which should properly support multiple Environments per thread.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rpetrich
rpetrich force-pushed the fix-modpending-race-master branch from 18b1cde to 5c3e676 Compare June 30, 2018 19:38
@rpetrich
src: fix race on modpending by migrating it to Environment
7f2c8bf
Fixes a rare race condition on modpending when two native modules are
loaded simultaneously on different threads by migrating modpending to
be a member of Environment.
@rpetrich
rpetrich force-pushed the fix-modpending-race-master branch from 5c3e676 to 7f2c8bf Compare June 30, 2018 19:38
@rpetrich rpetrich changed the title src: fix race on modpending by migrating it to Environment src: fix race on modpending by migrating it to a thread local Jun 30, 2018
@gabrielschulhof

gabrielschulhof commented Jul 11, 2018

Copy link
Copy Markdown
Contributor

@addaleax modules which rely on mp are not multiple init anyway, because relying on mp is equivalent to relying on library constructors, which are once-per-process-per-module.

Assuming the node environment on such a thread is correctly set up to be indistinguishable from a single-threaded node environment such addons will work correctly because the one thread where they do load is the only thread where they need to work and they will not subsequently load because the library constructors will not fire again.

In contrast, when using a special symbol a module must support multiple init and module-instance-local storage, because such a symbol will be found every time the module is loaded, no matter what thread it's on and no matter that it's multiple times on a single thread.

So, I think landing this PR will help us, and will not cause any more breakage than the current inability to load a module more than once already causes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gabrielschulhof
gabrielschulhof reviewed Jul 11, 2018
View reviewed changes
Comment thread src/node.cc Outdated
static bool v8_is_profiling = false;
static bool node_is_initialized = false;
static node_module* modpending;
static uv_once_t init_once = UV_ONCE_INIT;

@gabrielschulhof gabrielschulhof Jul 11, 2018

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The name of this variable should indicate that it refers to thread_local_modpending – so, like, maybe init_modpending_once?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
gabrielschulhof
gabrielschulhof reviewed Jul 11, 2018
View reviewed changes
Comment thread src/node.cc Outdated
reinterpret_cast<napi_addon_register_func>(dlib->GetSymbolAddress(name));
}

void InitDLOpenOnce() {

@gabrielschulhof gabrielschulhof Jul 11, 2018

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This should be InitModpendingOnce, not InitDLOpenOnce.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@devsnek devsnek mentioned this pull request Jul 12, 2018
worker threads throws exception while loading npm lib #21783
Closed
@gabrielschulhof

gabrielschulhof commented Jul 19, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I was able to cause node --experimental-worker to abort with the following message:

node[25666]: ../src/node.cc:1783:void node::DLOpen(const v8::FunctionCallbackInfo<v8::Value>&): Assertion `(modpending) == nullptr' failed.
 1: 0x89a0c0 node::Abort() [node]
 2: 0x89a1a5  [node]
 3: 0x89b38a  [node]
 4: 0xb14459  [node]
 5: 0xb14fc9 v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) [node]
 6: 0x685b99041bd 

by running the following code:

/* index.js */
const assert = require('assert');
const {
  Worker, isMainThread, parentPort
} = require('worker_threads');

if (isMainThread) {
  let robinHood;
  (new Worker(__filename)).on('message', (data) => {
    if (data === 'ready') {
      robinHood = JSON.stringify(require('./build/Release/robin_hood'));
    } else {
      console.log(data + '\nvs.\n' + robinHood);
      assert.notStrictEqual(data, robinHood);
    }
  });
} else {
  parentPort.postMessage('ready');
  parentPort.postMessage(JSON.stringify(require('./build/Release/friar_tuck')));
}

where

/* robin_hood.c */
#include <assert.h>
#include <node_api.h>

static napi_value Init(napi_env env, napi_value exports) {
  napi_status status;
  napi_value result;

  status = napi_create_uint32(env, 42, &result);
  assert(status == napi_ok);

  status = napi_set_named_property(env, exports, "answer", result);
  assert(status == napi_ok);

  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

and

/* friar_tuck.c */
#include <assert.h>
#include <node_api.h>

static napi_value Init(napi_env env, napi_value exports) {
  napi_status status;
  napi_value result;

  status = napi_create_string_utf8(env, "good", NAPI_AUTO_LENGTH, &result);
  assert(status == napi_ok);

  status = napi_set_named_property(env, exports, "question", result);
  assert(status == napi_ok);

  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

in a shell loop with

while node --experimental-worker ./index.js; do echo -n ''; done

It's not deterministic though so it doesn't serve as a good test.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Jul 19, 2018

Copy link
Copy Markdown
Contributor

@addaleax I believe that using NODE_MODULE_INIT() or NAPI_MODULE_INIT() instead of NODE_MODULE() and NAPI_MODULE() constitutes opt-in, especially given the NODE_MODULE_INIT() documentation we recently added.

In conclusion, using NODE_MODULE() or NAPI_MODULE() will result in a single-load module, unless the symbol given as the nm_register_func is the well-known symbol (which is most often not the case in the wild), and using NODE_MODULE_INIT() or NAPI_MODULE_INIT() will result in a contex-aware, multiple-load module. With this change, both scenarios will work correctly AFAICT.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhdawson

mhdawson commented Jul 19, 2018

Copy link
Copy Markdown
Member

@gabrielf that all makes sense to me. I guess we just need to get this PR updated based on your comments and the it looks good to me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Jul 19, 2018

Copy link
Copy Markdown
Contributor

@rpetrich do you think you might be able to find the time to address my comments so we can move the PR forward?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Gabriel Schulhof
src: squash! fix naming
da4632a
@gabrielschulhof

gabrielschulhof commented Jul 27, 2018

Copy link
Copy Markdown
Contributor

@mhdawson, @addaleax I fixed the naming. Could you please have another look?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rpetrich

rpetrich commented Jul 27, 2018

Copy link
Copy Markdown
Contributor Author

@gabrielschulhof Thanks for pushing to the PR. I'd missed the earlier comments about naming.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Jul 28, 2018

Copy link
Copy Markdown
Contributor

@rpetrich happy to help!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Aug 8, 2018

Copy link
Copy Markdown
Contributor

@mhdawson could you PTAL?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gabrielschulhof
gabrielschulhof approved these changes Aug 11, 2018
View reviewed changes
@gabrielschulhof

gabrielschulhof commented Aug 11, 2018

Copy link
Copy Markdown
Contributor

@nodejs/addon-api could someone else please also take a look?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Aug 13, 2018

Copy link
Copy Markdown
Contributor

@nodejs/collaborators could someone please have a look?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BridgeAR
BridgeAR requested review from TimothyGu, addaleax, bnoordhuis, devsnek, jasnell and mhdawson August 13, 2018 13:54
@devsnek
devsnek removed their request for review August 13, 2018 15:25
@gabrielschulhof gabrielschulhof mentioned this pull request Aug 14, 2018
doc: require two approvals to land changes #22255
Closed
3 tasks done
@Trott

Trott commented Aug 14, 2018

Copy link
Copy Markdown
Member

CI: https://ci.nodejs.org/job/node-test-pull-request/16448/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@joyeecheung joyeecheung mentioned this pull request Aug 15, 2018
CI failures: 20180815 nodejs/reliability#13
Closed
@TimothyGu
TimothyGu removed their request for review August 16, 2018 00:39
apapirovski
apapirovski approved these changes Aug 16, 2018
View reviewed changes
@gabrielschulhof

gabrielschulhof commented Aug 16, 2018

Copy link
Copy Markdown
Contributor

CI resumed as https://ci.nodejs.org/job/node-test-pull-request/16483/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Aug 16, 2018

Copy link
Copy Markdown
Contributor

New CI: https://ci.nodejs.org/job/node-test-pull-request/16484/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Aug 16, 2018

Copy link
Copy Markdown
Contributor

FreeBSD rebuild: https://ci.nodejs.org/job/node-test-commit-freebsd/19726/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Trott

Trott commented Aug 16, 2018

Copy link
Copy Markdown
Member

I think FreeBSD is back to green. Let's try a Resume Build: https://ci.nodejs.org/job/node-test-pull-request/16501/

gabrielschulhof reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Aug 17, 2018

Copy link
Copy Markdown
Contributor

Landed in 3814534.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof gabrielschulhof closed this Aug 17, 2018
gabrielschulhof pushed a commit that referenced this pull request Aug 17, 2018
@rpetrich Gabriel Schulhof
src: fix race on modpending
3814534
Fixes a rare race condition on modpending when two native modules are
loaded simultaneously on different threads by storing it thread-
locally.

PR-URL: #21611
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
@joyeecheung joyeecheung mentioned this pull request Aug 17, 2018
CI Failures: 20180817 nodejs/reliability#14
Closed
targos pushed a commit that referenced this pull request Aug 19, 2018
@rpetrich @targos
src: fix race on modpending
eba8da1
Fixes a rare race condition on modpending when two native modules are
loaded simultaneously on different threads by storing it thread-
locally.

PR-URL: #21611
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
targos pushed a commit that referenced this pull request Sep 3, 2018
@rpetrich @targos
src: fix race on modpending
866965e
Fixes a rare race condition on modpending when two native modules are
loaded simultaneously on different threads by storing it thread-
locally.

PR-URL: #21611
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
@targos targos mentioned this pull request Sep 5, 2018
Release proposal: v10.10.0 #22716
Merged
This was referenced Sep 6, 2018
2018-09-06 Version 10.10.0 (Current) @targos beevelop/docker-android-nodejs#236
Closed
2018-09-06 Version 10.10.0 (Current) @targos beevelop/docker-nodejs#221
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gabrielschulhof gabrielschulhof gabrielschulhof approved these changes
@bnoordhuis bnoordhuis Awaiting requested review from bnoordhuis
@jasnell jasnell Awaiting requested review from jasnell
@addaleax addaleax Awaiting requested review from addaleax
@mhdawson mhdawson Awaiting requested review from mhdawson
+1 more reviewer
@apapirovski apapirovski apapirovski approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

c++ Issues and PRs that require attention from people who are familiar with C++.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@rpetrich @gabrielschulhof @mhdawson @Trott @apapirovski @addaleax @nodejs-github-bot
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.