-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
src: fix race condition in debug signal on exit #3528
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef SRC_ATOMIC_POLYFILL_H_ | ||
#define SRC_ATOMIC_POLYFILL_H_ | ||
|
||
#include "util.h" | ||
|
||
namespace nonstd { | ||
|
||
template <typename T> | ||
struct atomic { | ||
atomic() = default; | ||
T exchange(T value) { return __sync_lock_test_and_set(&value_, value); } | ||
T value_ = T(); | ||
DISALLOW_COPY_AND_ASSIGN(atomic); | ||
}; | ||
|
||
} // namespace nonstd | ||
|
||
#endif // SRC_ATOMIC_POLYFILL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,14 @@ typedef int mode_t; | |
extern char **environ; | ||
#endif | ||
|
||
#ifdef __APPLE__ | ||
#include "atomic-polyfill.h" // NOLINT(build/include_order) | ||
namespace node { template <typename T> using atomic = nonstd::atomic<T>; } | ||
#else | ||
#include <atomic> | ||
namespace node { template <typename T> using atomic = std::atomic<T>; } | ||
#endif | ||
|
||
namespace node { | ||
|
||
using v8::Array; | ||
|
@@ -153,7 +161,7 @@ static double prog_start_time; | |
static bool debugger_running; | ||
static uv_async_t dispatch_debug_messages_async; | ||
|
||
static Isolate* node_isolate = nullptr; | ||
static node::atomic<Isolate*> node_isolate; | ||
static v8::Platform* default_platform; | ||
|
||
|
||
|
@@ -3410,28 +3418,46 @@ static void EnableDebug(Environment* env) { | |
} | ||
|
||
|
||
// Called from an arbitrary thread. | ||
static void TryStartDebugger() { | ||
// Call only async signal-safe functions here! Don't retry the exchange, | ||
// it will deadlock when the thread is interrupted inside a critical section. | ||
if (auto isolate = node_isolate.exchange(nullptr)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, our linter doesn't complain about this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would it? It's a common C++ idiom. |
||
v8::Debug::DebugBreak(isolate); | ||
uv_async_send(&dispatch_debug_messages_async); | ||
CHECK_EQ(nullptr, node_isolate.exchange(isolate)); | ||
} | ||
} | ||
|
||
|
||
// Called from the main thread. | ||
static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle) { | ||
// Synchronize with signal handler, see TryStartDebugger. | ||
Isolate* isolate; | ||
do { | ||
isolate = node_isolate.exchange(nullptr); | ||
} while (isolate == nullptr); | ||
|
||
if (debugger_running == false) { | ||
fprintf(stderr, "Starting debugger agent.\n"); | ||
|
||
HandleScope scope(node_isolate); | ||
Environment* env = Environment::GetCurrent(node_isolate); | ||
HandleScope scope(isolate); | ||
Environment* env = Environment::GetCurrent(isolate); | ||
Context::Scope context_scope(env->context()); | ||
|
||
StartDebug(env, false); | ||
EnableDebug(env); | ||
} | ||
Isolate::Scope isolate_scope(node_isolate); | ||
|
||
Isolate::Scope isolate_scope(isolate); | ||
v8::Debug::ProcessDebugMessages(); | ||
CHECK_EQ(nullptr, node_isolate.exchange(isolate)); | ||
} | ||
|
||
|
||
#ifdef __POSIX__ | ||
static void EnableDebugSignalHandler(int signo) { | ||
// Call only async signal-safe functions here! | ||
v8::Debug::DebugBreak(*static_cast<Isolate* volatile*>(&node_isolate)); | ||
uv_async_send(&dispatch_debug_messages_async); | ||
TryStartDebugger(); | ||
} | ||
|
||
|
||
|
@@ -3485,8 +3511,7 @@ static int RegisterDebugSignalHandler() { | |
|
||
#ifdef _WIN32 | ||
DWORD WINAPI EnableDebugThreadProc(void* arg) { | ||
v8::Debug::DebugBreak(*static_cast<Isolate* volatile*>(&node_isolate)); | ||
uv_async_send(&dispatch_debug_messages_async); | ||
TryStartDebugger(); | ||
return 0; | ||
} | ||
|
||
|
@@ -4006,7 +4031,8 @@ static void StartNodeInstance(void* arg) { | |
// Fetch a reference to the main isolate, so we have a reference to it | ||
// even when we need it to access it from another (debugger) thread. | ||
if (instance_data->is_main()) | ||
node_isolate = isolate; | ||
CHECK_EQ(nullptr, node_isolate.exchange(isolate)); | ||
|
||
{ | ||
Locker locker(isolate); | ||
Isolate::Scope isolate_scope(isolate); | ||
|
@@ -4016,7 +4042,7 @@ static void StartNodeInstance(void* arg) { | |
array_buffer_allocator->set_env(env); | ||
Context::Scope context_scope(context); | ||
|
||
node_isolate->SetAbortOnUncaughtExceptionCallback( | ||
isolate->SetAbortOnUncaughtExceptionCallback( | ||
ShouldAbortOnUncaughtException); | ||
|
||
// Start debug agent when argv has --debug | ||
|
@@ -4070,12 +4096,15 @@ static void StartNodeInstance(void* arg) { | |
env = nullptr; | ||
} | ||
|
||
if (instance_data->is_main()) { | ||
// Synchronize with signal handler, see TryStartDebugger. | ||
while (isolate != node_isolate.exchange(nullptr)); // NOLINT | ||
} | ||
|
||
CHECK_NE(isolate, nullptr); | ||
isolate->Dispose(); | ||
isolate = nullptr; | ||
delete array_buffer_allocator; | ||
if (instance_data->is_main()) | ||
node_isolate = nullptr; | ||
} | ||
|
||
int Start(int argc, char** argv) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice polyfill you have here.