Skip to content
Permalink
Browse files

src: track no of active JS signal handlers

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 Nov 2, 2019
1 parent 6525142 commit eb8beb5f5f9492d18082cb5c34d56b1e40831188
Showing with 40 additions and 0 deletions.
  1. +2 −0 src/node_internals.h
  2. +38 −0 src/signal_wrap.cc
@@ -314,6 +314,8 @@ void EndStartedProfilers(Environment* env);
}
#endif // HAVE_INSPECTOR

bool HasSignalJSHandler(int signum);

#ifdef _WIN32
typedef SYSTEMTIME TIME_TYPE;
#else // UNIX, OSX
@@ -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,
@@ -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());
@@ -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


0 comments on commit eb8beb5

Please sign in to comment.
You can’t perform that action at this time.