GeezerVison is showing github.com — n-api: guard against cond null dereference · nodejs/node@45816c5
File treeExpand file treeCollapse file tree Open diff view settings test/addons-napi/test_threadsafe_function
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_api.cc +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3782,7 +3782,7 @@ class TsFn: public node::AsyncResource { | 3782 | 3782 | if (thread_count == 0 || mode == napi_tsfn_abort) { | 3783 | 3783 | if (!is_closing) { | 3784 | 3784 | is_closing = (mode == napi_tsfn_abort); | 3785 | | - if (is_closing) { | | 3785 | + if (is_closing && max_queue_size > 0) { | 3786 | 3786 | cond->Signal(lock); | 3787 | 3787 | } | 3788 | 3788 | if (uv_async_send(&async) != 0) { | @@ -3872,7 +3872,9 @@ class TsFn: public node::AsyncResource { | 3872 | 3872 | if (size == 0) { | 3873 | 3873 | if (thread_count == 0) { | 3874 | 3874 | is_closing = true; | 3875 | | - cond->Signal(lock); | | 3875 | + if (max_queue_size > 0) { | | 3876 | + cond->Signal(lock); | | 3877 | + } | 3876 | 3878 | CloseHandlesAndMaybeDelete(); | 3877 | 3879 | } else { | 3878 | 3880 | if (uv_idle_stop(&idle) != 0) { | @@ -3939,7 +3941,9 @@ class TsFn: public node::AsyncResource { | 3939 | 3941 | if (set_closing) { | 3940 | 3942 | node::Mutex::ScopedLock lock(this->mutex); | 3941 | 3943 | is_closing = true; | 3942 | | - cond->Signal(lock); | | 3944 | + if (max_queue_size > 0) { | | 3945 | + cond->Signal(lock); | | 3946 | + } | 3943 | 3947 | } | 3944 | 3948 | if (handles_closing) { | 3945 | 3949 | return; | |
Collapse file Copy file name to clipboardExpand all lines: test/addons-napi/test_threadsafe_function/binding.c +34-5Lines changed: 34 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | #include "../common.h" | 10 | 10 |
| 11 | 11 | #define ARRAY_LENGTH 10 | | 12 | +#define MAX_QUEUE_SIZE 2 | 12 | 13 |
| 13 | 14 | static uv_thread_t uv_threads[2]; | 14 | 15 | static napi_threadsafe_function ts_fn; | @@ -18,6 +19,7 @@ typedef struct { | 18 | 19 | napi_threadsafe_function_release_mode abort; | 19 | 20 | bool start_secondary; | 20 | 21 | napi_ref js_finalize_cb; | | 22 | + uint32_t max_queue_size; | 21 | 23 | } ts_fn_hint; | 22 | 24 |
| 23 | 25 | static ts_fn_hint ts_info; | @@ -71,6 +73,12 @@ static void data_source_thread(void* data) { | 71 | 73 | for (index = ARRAY_LENGTH - 1; index > -1 && !queue_was_closing; index--) { | 72 | 74 | status = napi_call_threadsafe_function(ts_fn, &ints[index], | 73 | 75 | ts_fn_info->block_on_full); | | 76 | + if (ts_fn_info->max_queue_size == 0) { | | 77 | + // Let's make this thread really busy for 200 ms to give the main thread a | | 78 | + // chance to abort. | | 79 | + uint64_t start = uv_hrtime(); | | 80 | + for (; uv_hrtime() - start < 200000000;); | | 81 | + } | 74 | 82 | switch (status) { | 75 | 83 | case napi_queue_full: | 76 | 84 | queue_was_full = true; | @@ -167,8 +175,8 @@ static napi_value StartThreadInternal(napi_env env, | 167 | 175 | napi_callback_info info, | 168 | 176 | napi_threadsafe_function_call_js cb, | 169 | 177 | bool block_on_full) { | 170 | | - size_t argc = 3; | 171 | | - napi_value argv[3]; | | 178 | + size_t argc = 4; | | 179 | + napi_value argv[4]; | 172 | 180 |
| 173 | 181 | ts_info.block_on_full = | 174 | 182 | (block_on_full ? napi_tsfn_blocking : napi_tsfn_nonblocking); | @@ -178,8 +186,18 @@ static napi_value StartThreadInternal(napi_env env, | 178 | 186 | napi_value async_name; | 179 | 187 | NAPI_CALL(env, napi_create_string_utf8(env, "N-API Thread-safe Function Test", | 180 | 188 | NAPI_AUTO_LENGTH, &async_name)); | 181 | | - NAPI_CALL(env, napi_create_threadsafe_function(env, argv[0], NULL, async_name, | 182 | | - 2, 2, uv_threads, join_the_threads, &ts_info, cb, &ts_fn)); | | 189 | + NAPI_CALL(env, napi_get_value_uint32(env, argv[3], &ts_info.max_queue_size)); | | 190 | + NAPI_CALL(env, napi_create_threadsafe_function(env, | | 191 | + argv[0], | | 192 | + NULL, | | 193 | + async_name, | | 194 | + ts_info.max_queue_size, | | 195 | + 2, | | 196 | + uv_threads, | | 197 | + join_the_threads, | | 198 | + &ts_info, | | 199 | + cb, | | 200 | + &ts_fn)); | 183 | 201 | bool abort; | 184 | 202 | NAPI_CALL(env, napi_get_value_bool(env, argv[1], &abort)); | 185 | 203 | ts_info.abort = abort ? napi_tsfn_abort : napi_tsfn_release; | @@ -224,8 +242,9 @@ static napi_value Init(napi_env env, napi_value exports) { | 224 | 242 | for (index = 0; index < ARRAY_LENGTH; index++) { | 225 | 243 | ints[index] = index; | 226 | 244 | } | 227 | | - napi_value js_array_length; | | 245 | + napi_value js_array_length, js_max_queue_size; | 228 | 246 | napi_create_uint32(env, ARRAY_LENGTH, &js_array_length); | | 247 | + napi_create_uint32(env, MAX_QUEUE_SIZE, &js_max_queue_size); | 229 | 248 |
| 230 | 249 | napi_property_descriptor properties[] = { | 231 | 250 | { | @@ -238,6 +257,16 @@ static napi_value Init(napi_env env, napi_value exports) { | 238 | 257 | napi_enumerable, | 239 | 258 | NULL | 240 | 259 | }, | | 260 | + { | | 261 | + "MAX_QUEUE_SIZE", | | 262 | + NULL, | | 263 | + NULL, | | 264 | + NULL, | | 265 | + NULL, | | 266 | + js_max_queue_size, | | 267 | + napi_enumerable, | | 268 | + NULL | | 269 | + }, | 241 | 270 | DECLARE_NAPI_PROPERTY("StartThread", StartThread), | 242 | 271 | DECLARE_NAPI_PROPERTY("StartThreadNoNative", StartThreadNoNative), | 243 | 272 | DECLARE_NAPI_PROPERTY("StartThreadNonblocking", StartThreadNonblocking), | |
Collapse file Copy file name to clipboardExpand all lines: test/addons-napi/test_threadsafe_function/test.js +63-21Lines changed: 63 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,7 @@ if (process.argv[2] === 'child') { | 23 | 23 | if (callCount === 2) { | 24 | 24 | binding.Unref(); | 25 | 25 | } | 26 | | - }, false /* abort */, true /* launchSecondary */); | | 26 | + }, false /* abort */, true /* launchSecondary */, +process.argv[3]); | 27 | 27 |
| 28 | 28 | // Release the thread-safe function from the main thread so that it may be | 29 | 29 | // torn down via the environment cleanup handler. | @@ -35,6 +35,7 @@ function testWithJSMarshaller({ | 35 | 35 | threadStarter, | 36 | 36 | quitAfter, | 37 | 37 | abort, | | 38 | + maxQueueSize, | 38 | 39 | launchSecondary }) { | 39 | 40 | return new Promise((resolve) => { | 40 | 41 | const array = []; | @@ -47,7 +48,7 @@ function testWithJSMarshaller({ | 47 | 48 | }), !!abort); | 48 | 49 | }); | 49 | 50 | } | 50 | | - }, !!abort, !!launchSecondary); | | 51 | + }, !!abort, !!launchSecondary, maxQueueSize); | 51 | 52 | if (threadStarter === 'StartThreadNonblocking') { | 52 | 53 | // Let's make this thread really busy for a short while to ensure that | 53 | 54 | // the queue fills and the thread receives a napi_queue_full. | @@ -57,6 +58,24 @@ function testWithJSMarshaller({ | 57 | 58 | }); | 58 | 59 | } | 59 | 60 |
| | 61 | +function testUnref(queueSize) { | | 62 | + return new Promise((resolve, reject) => { | | 63 | + let output = ''; | | 64 | + const child = fork(__filename, ['child', queueSize], { | | 65 | + stdio: [process.stdin, 'pipe', process.stderr, 'ipc'] | | 66 | + }); | | 67 | + child.on('close', (code) => { | | 68 | + if (code === 0) { | | 69 | + resolve(output.match(/\S+/g)); | | 70 | + } else { | | 71 | + reject(new Error('Child process died with code ' + code)); | | 72 | + } | | 73 | + }); | | 74 | + child.stdout.on('data', (data) => (output += data.toString())); | | 75 | + }) | | 76 | + .then((result) => assert.strictEqual(result.indexOf(0), -1)); | | 77 | +} | | 78 | + | 60 | 79 | new Promise(function testWithoutJSMarshaller(resolve) { | 61 | 80 | let callCount = 0; | 62 | 81 | binding.StartThreadNoNative(function testCallback() { | @@ -71,13 +90,23 @@ new Promise(function testWithoutJSMarshaller(resolve) { | 71 | 90 | }), false); | 72 | 91 | }); | 73 | 92 | } | 74 | | - }, false /* abort */, false /* launchSecondary */); | | 93 | + }, false /* abort */, false /* launchSecondary */, binding.MAX_QUEUE_SIZE); | 75 | 94 | }) | 76 | 95 |
| 77 | 96 | // Start the thread in blocking mode, and assert that all values are passed. | 78 | 97 | // Quit after it's done. | 79 | 98 | .then(() => testWithJSMarshaller({ | 80 | 99 | threadStarter: 'StartThread', | | 100 | + maxQueueSize: binding.MAX_QUEUE_SIZE, | | 101 | + quitAfter: binding.ARRAY_LENGTH | | 102 | +})) | | 103 | +.then((result) => assert.deepStrictEqual(result, expectedArray)) | | 104 | + | | 105 | +// Start the thread in blocking mode with an infinite queue, and assert that all | | 106 | +// values are passed. Quit after it's done. | | 107 | +.then(() => testWithJSMarshaller({ | | 108 | + threadStarter: 'StartThread', | | 109 | + maxQueueSize: 0, | 81 | 110 | quitAfter: binding.ARRAY_LENGTH | 82 | 111 | })) | 83 | 112 | .then((result) => assert.deepStrictEqual(result, expectedArray)) | @@ -86,6 +115,7 @@ new Promise(function testWithoutJSMarshaller(resolve) { | 86 | 115 | // Quit after it's done. | 87 | 116 | .then(() => testWithJSMarshaller({ | 88 | 117 | threadStarter: 'StartThreadNonblocking', | | 118 | + maxQueueSize: binding.MAX_QUEUE_SIZE, | 89 | 119 | quitAfter: binding.ARRAY_LENGTH | 90 | 120 | })) | 91 | 121 | .then((result) => assert.deepStrictEqual(result, expectedArray)) | @@ -94,6 +124,16 @@ new Promise(function testWithoutJSMarshaller(resolve) { | 94 | 124 | // Quit early, but let the thread finish. | 95 | 125 | .then(() => testWithJSMarshaller({ | 96 | 126 | threadStarter: 'StartThread', | | 127 | + maxQueueSize: binding.MAX_QUEUE_SIZE, | | 128 | + quitAfter: 1 | | 129 | +})) | | 130 | +.then((result) => assert.deepStrictEqual(result, expectedArray)) | | 131 | + | | 132 | +// Start the thread in blocking mode with an infinite queue, and assert that all | | 133 | +// values are passed. Quit early, but let the thread finish. | | 134 | +.then(() => testWithJSMarshaller({ | | 135 | + threadStarter: 'StartThread', | | 136 | + maxQueueSize: 0, | 97 | 137 | quitAfter: 1 | 98 | 138 | })) | 99 | 139 | .then((result) => assert.deepStrictEqual(result, expectedArray)) | @@ -102,6 +142,7 @@ new Promise(function testWithoutJSMarshaller(resolve) { | 102 | 142 | // Quit early, but let the thread finish. | 103 | 143 | .then(() => testWithJSMarshaller({ | 104 | 144 | threadStarter: 'StartThreadNonblocking', | | 145 | + maxQueueSize: binding.MAX_QUEUE_SIZE, | 105 | 146 | quitAfter: 1 | 106 | 147 | })) | 107 | 148 | .then((result) => assert.deepStrictEqual(result, expectedArray)) | @@ -112,6 +153,7 @@ new Promise(function testWithoutJSMarshaller(resolve) { | 112 | 153 | .then(() => testWithJSMarshaller({ | 113 | 154 | threadStarter: 'StartThread', | 114 | 155 | quitAfter: 1, | | 156 | + maxQueueSize: binding.MAX_QUEUE_SIZE, | 115 | 157 | launchSecondary: true | 116 | 158 | })) | 117 | 159 | .then((result) => assert.deepStrictEqual(result, expectedArray)) | @@ -122,15 +164,27 @@ new Promise(function testWithoutJSMarshaller(resolve) { | 122 | 164 | .then(() => testWithJSMarshaller({ | 123 | 165 | threadStarter: 'StartThreadNonblocking', | 124 | 166 | quitAfter: 1, | | 167 | + maxQueueSize: binding.MAX_QUEUE_SIZE, | 125 | 168 | launchSecondary: true | 126 | 169 | })) | 127 | 170 | .then((result) => assert.deepStrictEqual(result, expectedArray)) | 128 | 171 |
| 129 | 172 | // Start the thread in blocking mode, and assert that it could not finish. | 130 | | -// Quit early and aborting. | | 173 | +// Quit early by aborting. | | 174 | +.then(() => testWithJSMarshaller({ | | 175 | + threadStarter: 'StartThread', | | 176 | + quitAfter: 1, | | 177 | + maxQueueSize: binding.MAX_QUEUE_SIZE, | | 178 | + abort: true | | 179 | +})) | | 180 | +.then((result) => assert.strictEqual(result.indexOf(0), -1)) | | 181 | + | | 182 | +// Start the thread in blocking mode with an infinite queue, and assert that it | | 183 | +// could not finish. Quit early by aborting. | 131 | 184 | .then(() => testWithJSMarshaller({ | 132 | 185 | threadStarter: 'StartThread', | 133 | 186 | quitAfter: 1, | | 187 | + maxQueueSize: 0, | 134 | 188 | abort: true | 135 | 189 | })) | 136 | 190 | .then((result) => assert.strictEqual(result.indexOf(0), -1)) | @@ -140,25 +194,13 @@ new Promise(function testWithoutJSMarshaller(resolve) { | 140 | 194 | .then(() => testWithJSMarshaller({ | 141 | 195 | threadStarter: 'StartThreadNonblocking', | 142 | 196 | quitAfter: 1, | | 197 | + maxQueueSize: binding.MAX_QUEUE_SIZE, | 143 | 198 | abort: true | 144 | 199 | })) | 145 | 200 | .then((result) => assert.strictEqual(result.indexOf(0), -1)) | 146 | 201 |
| 147 | 202 | // Start a child process to test rapid teardown | 148 | | -.then(() => { | 149 | | - return new Promise((resolve, reject) => { | 150 | | - let output = ''; | 151 | | - const child = fork(__filename, ['child'], { | 152 | | - stdio: [process.stdin, 'pipe', process.stderr, 'ipc'] | 153 | | - }); | 154 | | - child.on('close', (code) => { | 155 | | - if (code === 0) { | 156 | | - resolve(output.match(/\S+/g)); | 157 | | - } else { | 158 | | - reject(new Error('Child process died with code ' + code)); | 159 | | - } | 160 | | - }); | 161 | | - child.stdout.on('data', (data) => (output += data.toString())); | 162 | | - }); | 163 | | -}) | 164 | | -.then((result) => assert.strictEqual(result.indexOf(0), -1)); | | 203 | +.then(() => testUnref(binding.MAX_QUEUE_SIZE)) | | 204 | + | | 205 | +// Start a child process with an infinite queue to test rapid teardown | | 206 | +.then(() => testUnref(0)); |
|