@@ -51,6 +51,9 @@ using v8_inspector::V8InspectorClient;
5151
5252static uv_sem_t start_io_thread_semaphore;
5353static uv_async_t start_io_thread_async;
54+ // This is just an additional check to make sure start_io_thread_async
55+ // is not accidentally re-used or used when uninitialized.
56+ static std::atomic_bool start_io_thread_async_initialized { false };
5457
5558class StartIoTask : public Task {
5659 public:
@@ -88,6 +91,7 @@ static void StartIoThreadWakeup(int signo) {
8891inline void * StartIoThreadMain (void * unused) {
8992 for (;;) {
9093 uv_sem_wait (&start_io_thread_semaphore);
94+ CHECK (start_io_thread_async_initialized);
9195 Agent* agent = static_cast <Agent*>(start_io_thread_async.data );
9296 if (agent != nullptr )
9397 agent->RequestIoThreadStart ();
@@ -141,6 +145,7 @@ static int StartDebugSignalHandler() {
141145
142146#ifdef _WIN32
143147DWORD WINAPI StartIoThreadProc (void * arg) {
148+ CHECK (start_io_thread_async_initialized);
144149 Agent* agent = static_cast <Agent*>(start_io_thread_async.data );
145150 if (agent != nullptr )
146151 agent->RequestIoThreadStart ();
@@ -664,6 +669,7 @@ Agent::Agent(Environment* env)
664669
665670Agent::~Agent () {
666671 if (start_io_thread_async.data == this ) {
672+ CHECK (start_io_thread_async_initialized.exchange (false ));
667673 start_io_thread_async.data = nullptr ;
668674 // This is global, will never get freed
669675 uv_close (reinterpret_cast <uv_handle_t *>(&start_io_thread_async), nullptr );
@@ -681,6 +687,7 @@ bool Agent::Start(const std::string& path,
681687
682688 client_ = std::make_shared<NodeInspectorClient>(parent_env_, is_main);
683689 if (parent_env_->is_main_thread ()) {
690+ CHECK_EQ (start_io_thread_async_initialized.exchange (true ), false );
684691 CHECK_EQ (0 , uv_async_init (parent_env_->event_loop (),
685692 &start_io_thread_async,
686693 StartIoThreadAsyncCallback));
@@ -847,13 +854,15 @@ void Agent::RequestIoThreadStart() {
847854 // We need to attempt to interrupt V8 flow (in case Node is running
848855 // continuous JS code) and to wake up libuv thread (in case Node is waiting
849856 // for IO events)
857+ CHECK (start_io_thread_async_initialized);
850858 uv_async_send (&start_io_thread_async);
851859 Isolate* isolate = parent_env_->isolate ();
852860 v8::Platform* platform = parent_env_->isolate_data ()->platform ();
853861 std::shared_ptr<TaskRunner> taskrunner =
854862 platform->GetForegroundTaskRunner (isolate);
855863 taskrunner->PostTask (std::make_unique<StartIoTask>(this ));
856864 isolate->RequestInterrupt (StartIoInterrupt, this );
865+ CHECK (start_io_thread_async_initialized);
857866 uv_async_send (&start_io_thread_async);
858867}
859868
0 commit comments