process.watchers() #1532

wants to merge 7 commits into


None yet

3 participants


As initially discussed in joyent/libuv#117, this adds a process.watchers() method to node that will return an array of objects describing all of the current event loop watchers. The objects have the following properties:

type: Can be "read", "write", "timer", "periodic", "child", "signal", "stat", or "custom".
handle: A JavaScript object attached to the watcher. It's presence and object type depends on the value of "type".
stack: A string holding up to 10 lines of a stack trace for the code that created this watcher. Only available if node was started with --debug

Example output from a simple program that calls setTimeout() and then process.watchers():

[ { type: 'timer',
     { repeat: 0,
       _idleNext: [Object],
       _idlePrev: [Object],
       callback: [Function] },
    stack: '    at insert (timers_legacy.js:61:12)\n    at (timers_legacy.js:134:7)\n    at exports.setTimeout (timers_legacy.js:186:13)\n    at (node.js:136:27)\n    at  (/Users/johnhurliman/Code/node/watchtest.js:4:1)\n    at Module._compile (module.js:420:26)\n    at Module._extensions..js (module.js:459:10)\n    at Module.load (module.js:335:31)\n    at Module._load (module.js:294:12)\n    at Module.runMain (module.js:479:10)' },
  { type: 'signal' } ]


  • v8::StackTrace::CurrentStackTrace() is called in every ObjectWrap constructor when the --debug flag is set. This may cause a noticeable performance hit. Ideally, there would be a separate flag just for capturing stack traces.
  • This relies on the ev_walk() call, part of libev which is currently being abstracted away by libuv. This code needs to be updated when uv_walk() is added to libuv.
@felixge felixge commented on the diff Aug 15, 2011
@@ -110,10 +110,38 @@ static Persistent<String> listeners_symbol;
static Persistent<String> uncaught_exception_symbol;
static Persistent<String> emit_symbol;
+struct watcher_struct {
+ int type;
+ void *w;
+const int MAX_WATCHERS = 100;
felixge Aug 15, 2011

Does this mean watchers() will stop working if this value is exceeded?

jhurliman Aug 15, 2011

When watchers() is called I allocate array space for a fixed number of watcher_structs and fill the array until ev_walk() completes or the array is full. It won't stop working, but it will only return the first 100 watchers. I'd be happy if someone wants to refactor this; I don't know an easy way of dynamically allocating space for watcher_structs without using an STL vector or another external lib.

felixge Aug 15, 2011

Makes sense. For my use case (knowing what is keeping node alive when there shouldn't be), that's no problem. But I can see how some other use cases (monitoring a busy node from the outside, detecting leaks, etc.) might not work well with this.

Anyway, I don't think that should be a problem for merging the patch as long as we mention it in the docs as a limitation for now.

felixge commented Aug 15, 2011

Not my area of expertise, but patch LGTM except for missing docs.


Sorry, we won't take this patch but a similar feature is in the works (it's a requisite for the new domains feature.)

@bnoordhuis bnoordhuis closed this Feb 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment