aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-dns-terminate-during-query.js
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-02-21 21:48:58 +0100
committerAnna Henningsen <anna@addaleax.net>2019-03-01 22:18:26 +0100
commit018e95ad13f431293c9a79dd8a2da4a8a67b81c1 (patch)
treea25c96b0a95a709348dca333b5818a442d01dfbf /test/parallel/test-worker-dns-terminate-during-query.js
parentadbe3b837e8a2285238ec0fcba89c20882eb4cdb (diff)
downloadandroid-node-v8-018e95ad13f431293c9a79dd8a2da4a8a67b81c1.tar.gz
android-node-v8-018e95ad13f431293c9a79dd8a2da4a8a67b81c1.tar.bz2
android-node-v8-018e95ad13f431293c9a79dd8a2da4a8a67b81c1.zip
dns: refactor QueryWrap lifetime management
- Prefer RAII-style management over manual resource management. - Prefer `env->SetImmediate()` over a separate `uv_async_t`. - Perform `ares_destroy()` before possibly tearing down c-ares state. - Verify that the number of active queries is non-negative. - Let pending callbacks know when their underlying `QueryWrap` object has been destroyed. The last item has been a real bug, in that when Workers shut down during currently running DNS queries, they may run into use-after-free situations because: 1. Shutting the `Worker` down leads to the cleanup code deleting the `QueryWrap` objects first; then 2. deleting the `ChannelWrap` object (as it has been created before the `QueryWrap`s), whose destructor runs `ares_destroy()`, which in turn invokes all pending query callbacks with `ARES_ECANCELLED`, 3. which lead to use-after-free, as the callback tried to access the deleted `QueryWrap` object. The added test verifies that this is no longer an issue. PR-URL: https://github.com/nodejs/node/pull/26253 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-worker-dns-terminate-during-query.js')
-rw-r--r--test/parallel/test-worker-dns-terminate-during-query.js23
1 files changed, 23 insertions, 0 deletions
diff --git a/test/parallel/test-worker-dns-terminate-during-query.js b/test/parallel/test-worker-dns-terminate-during-query.js
new file mode 100644
index 0000000000..77de9f484f
--- /dev/null
+++ b/test/parallel/test-worker-dns-terminate-during-query.js
@@ -0,0 +1,23 @@
+'use strict';
+const common = require('../common');
+const { Resolver } = require('dns');
+const dgram = require('dgram');
+const { Worker, isMainThread } = require('worker_threads');
+
+// Test that Workers can terminate while DNS queries are outstanding.
+
+if (isMainThread) {
+ return new Worker(__filename);
+}
+
+const socket = dgram.createSocket('udp4');
+
+socket.bind(0, common.mustCall(() => {
+ const resolver = new Resolver();
+ resolver.setServers([`127.0.0.1:${socket.address().port}`]);
+ resolver.resolve4('example.org', common.mustNotCall());
+}));
+
+socket.on('message', common.mustCall(() => {
+ process.exit();
+}));