Skip to content

Commit

Permalink
src: track no of active JS signal handlers
Browse files Browse the repository at this point in the history
This makes it possible to tell whether a signal is being tracked in JS.

PR-URL: #30229
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Shelley Vohr <codebytere@gmail.com>
  • Loading branch information
addaleax authored and MylesBorins committed Dec 17, 2019
1 parent 6525142 commit eb8beb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ void EndStartedProfilers(Environment* env);
}
#endif // HAVE_INSPECTOR

bool HasSignalJSHandler(int signum);

#ifdef _WIN32
typedef SYSTEMTIME TIME_TYPE;
#else // UNIX, OSX
Expand Down
38 changes: 38 additions & 0 deletions src/signal_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ using v8::Object;
using v8::String;
using v8::Value;

void DecreaseSignalHandlerCount(int signum);

namespace {

static Mutex handled_signals_mutex;
static std::map<int, int64_t> handled_signals; // Signal -> number of handlers

class SignalWrap : public HandleWrap {
public:
static void Initialize(Local<Object> target,
Expand Down Expand Up @@ -85,6 +90,11 @@ class SignalWrap : public HandleWrap {
CHECK_EQ(r, 0);
}

void Close(v8::Local<v8::Value> close_callback) override {
if (active_) DecreaseSignalHandlerCount(handle_.signum);
HandleWrap::Close(close_callback);
}

static void Start(const FunctionCallbackInfo<Value>& args) {
SignalWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
Expand Down Expand Up @@ -112,21 +122,49 @@ class SignalWrap : public HandleWrap {
wrap->MakeCallback(env->onsignal_string(), 1, &arg);
},
signum);

if (err == 0) {
CHECK(!wrap->active_);
wrap->active_ = true;
Mutex::ScopedLock lock(handled_signals_mutex);
handled_signals[signum]++;
}

args.GetReturnValue().Set(err);
}

static void Stop(const FunctionCallbackInfo<Value>& args) {
SignalWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());

if (wrap->active_) {
wrap->active_ = false;
DecreaseSignalHandlerCount(wrap->handle_.signum);
}

int err = uv_signal_stop(&wrap->handle_);
args.GetReturnValue().Set(err);
}

uv_signal_t handle_;
bool active_ = false;
};


} // anonymous namespace

void DecreaseSignalHandlerCount(int signum) {
Mutex::ScopedLock lock(handled_signals_mutex);
int new_handler_count = --handled_signals[signum];
CHECK_GE(new_handler_count, 0);
if (new_handler_count == 0)
handled_signals.erase(signum);
}

bool HasSignalJSHandler(int signum) {
Mutex::ScopedLock lock(handled_signals_mutex);
return handled_signals.find(signum) != handled_signals.end();
}
} // namespace node


Expand Down

0 comments on commit eb8beb5

Please sign in to comment.