Skip to content

Commit 3482be9

Browse files
fanatidmariuspod
andauthoredDec 30, 2022
Fix segfaults when used with worker_threads (#195)
* fix: don't use static constructor field with global scope which results in a race condition between worker threads. * feat: bump node-addon-api to ^3.2.1 which adds instance data to env. * update ci * bump node version Co-authored-by: mariuspod <14898268+mariuspod@users.noreply.github.com>
1 parent 4afabb4 commit 3482be9

File tree

7 files changed

+25
-26
lines changed

7 files changed

+25
-26
lines changed
 

‎.github/workflows/ci.yaml

+14-14
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,17 @@ jobs:
1515
os:
1616
- macos-latest
1717
- ubuntu-latest
18-
- windows-latest
18+
- windows-2019
1919
steps:
2020
- name: Fetch code
2121
uses: actions/checkout@v1
2222
with:
2323
submodules: true
2424

25-
- name: Install dependencies
26-
run: yarn install --ignore-scripts
27-
28-
- name: Build addon
29-
if: runner.os != 'Linux'
30-
run: make build-addon
31-
32-
- name: Build addon
33-
if: runner.os == 'Linux'
34-
run: make build-addon-linux
35-
3625
- name: Get minimal Node.js version from package.json (Linux & macOS)
3726
id: node-version-nix
3827
if: runner.os != 'Windows'
39-
run: echo "::set-output name=version::$(node -p 'require("./package.json").engines.node.match(/(\d.*)$/)[0]')"
28+
run: echo "::set-output name=version::$(node -p 'require("./package.json").engines.node.match(/(\d+)\..*$/)[1]')"
4029

4130
- name: Use Node.js ${{ steps.node-version-nix.outputs.version }} (Linux & macOS)
4231
if: runner.os != 'Windows'
@@ -47,14 +36,25 @@ jobs:
4736
- name: Get minimal Node.js version from package.json (Windows)
4837
id: node-version-win
4938
if: runner.os == 'Windows'
50-
run: echo "::set-output name=version::$(node -p 'require(\"./package.json\").engines.node.match(/(\d.*)$/)[0]')"
39+
run: echo "::set-output name=version::$(node -p 'require(\"./package.json\").engines.node.match(/(\d+)\..*$/)[1]')"
5140

5241
- name: Use Node.js ${{ steps.node-version-win.outputs.version }} (Windows)
5342
if: runner.os == 'Windows'
5443
uses: actions/setup-node@v1
5544
with:
5645
node-version: ${{ steps.node-version-win.outputs.version }}
5746

47+
- name: Install dependencies
48+
run: yarn install --ignore-scripts
49+
50+
- name: Build addon
51+
if: runner.os != 'Linux'
52+
run: make build-addon
53+
54+
- name: Build addon
55+
if: runner.os == 'Linux'
56+
run: make build-addon-linux
57+
5858
- name: Run tests for addon
5959
run: make test-tap
6060

‎Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ prebuildify-cross = ./node_modules/.bin/prebuildify-cross
99
# hack, otherwise GitHub Actions for Windows:
1010
# '.' is not recognized as an internal or external command, operable program or batch file.
1111
build-addon:
12-
$(prebuildify) --target node@10.0.0 --napi --strip && node -p "process.platform"
12+
$(prebuildify) --target node@14.0.0 --napi --strip && node -p "process.platform"
1313

1414
build-addon-linux:
15-
$(prebuildify-cross) -i centos7-devtoolset7 -i alpine --target node@10.0.0 --napi --strip
15+
$(prebuildify-cross) -i centos7-devtoolset7 -i alpine --target node@14.0.0 --napi --strip
1616

1717

1818
nyc = ./node_modules/.bin/nyc

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This module provides native bindings to [bitcoin-core/secp256k1](https://github.com/bitcoin-core/secp256k1). In browser [elliptic](https://github.com/indutny/elliptic) will be used as fallback.
44

5-
Works on node version 10.0.0 or greater, because use [N-API](https://nodejs.org/api/n-api.html).
5+
Works on node version 14.0.0 or greater, because use [N-API](https://nodejs.org/api/n-api.html).
66

77
## Installation
88

‎binding.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
'-fno-exceptions',
8484
],
8585
'defines': [
86-
'NAPI_VERSION=3',
86+
'NAPI_VERSION=6',
8787
],
8888
'xcode_settings': {
8989
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"dependencies": {
3535
"elliptic": "^6.5.4",
36-
"node-addon-api": "^2.0.0",
36+
"node-addon-api": "^5.0.0",
3737
"node-gyp-build": "^4.2.0"
3838
},
3939
"devDependencies": {
@@ -48,7 +48,7 @@
4848
"yargs": "^15.0.2"
4949
},
5050
"engines": {
51-
"node": ">=10.0.0"
51+
"node": ">=14.0.0"
5252
},
5353
"gypfile": true
5454
}

‎src/secp256k1.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
} while (0)
2727

2828
// Secp256k1
29-
Napi::FunctionReference Secp256k1Addon::constructor;
30-
unsigned int Secp256k1Addon::secp256k1_context_flags =
29+
const unsigned int Secp256k1Addon::secp256k1_context_flags =
3130
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY;
3231

3332
Napi::Value Secp256k1Addon::Init(Napi::Env env) {
@@ -66,8 +65,9 @@ Napi::Value Secp256k1Addon::Init(Napi::Env env) {
6665
InstanceMethod("ecdh", &Secp256k1Addon::ECDH),
6766
});
6867

69-
constructor = Napi::Persistent(func);
70-
constructor.SuppressDestruct();
68+
Napi::FunctionReference* constructor = new Napi::FunctionReference();
69+
*constructor = Napi::Persistent(func);
70+
env.SetInstanceData<Napi::FunctionReference>(constructor);
7171

7272
return func;
7373
}

‎src/secp256k1.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class Secp256k1Addon : public Napi::ObjectWrap<Secp256k1Addon> {
2828
};
2929

3030
private:
31-
static Napi::FunctionReference constructor;
32-
static unsigned int secp256k1_context_flags;
31+
static const unsigned int secp256k1_context_flags;
3332
const secp256k1_context* ctx_;
3433
ECDSASignData ecdsa_sign_data;
3534
ECDHData ecdh_data;

0 commit comments

Comments
 (0)
Please sign in to comment.