From c959d6024297acbd10efc2bb2807dbe65f2e9aab Mon Sep 17 00:00:00 2001
From: Anna Henningsen <anna@addaleax.net>
Date: Sun, 3 Feb 2019 15:14:19 +0100
Subject: [PATCH] worker,etw: only enable ETW on the main thread

The Windows ETW code is not written to be compatible with multi
threading, and in particular it relies on global state like a
single static `uv_async_t`. Adding that to multiple threads
would corrupt the corresponding loops' handle queues.

This addresses the flakiness of at least
`test-worker-exit-code` and very likely other flaky tests that
relate to Worker threads on Windows as well.

Fixes: https://github.com/nodejs/node/issues/25847
Fixes: https://github.com/nodejs/node/issues/25702
Fixes: https://github.com/nodejs/node/issues/24005
Fixes: https://github.com/nodejs/node/issues/23873

PR-URL: https://github.com/nodejs/node/pull/25907
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
---
 src/node_dtrace.cc | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/node_dtrace.cc b/src/node_dtrace.cc
index dfabde0747bdc9..0f33f59b338a85 100644
--- a/src/node_dtrace.cc
+++ b/src/node_dtrace.cc
@@ -288,7 +288,11 @@ void InitDTrace(Environment* env, Local<Object> target) {
   }
 
 #ifdef HAVE_ETW
-  init_etw();
+  // ETW is neither thread-safe nor does it clean up resources on exit,
+  // so we can use it only on the main thread.
+  if (env->is_main_thread()) {
+    init_etw();
+  }
 #endif
 
 #if defined HAVE_DTRACE || defined HAVE_ETW