audreyt / node-webworker-threads

Lightweight Web Worker API implementation with native threads
https://npmjs.org/package/webworker-threads
Other
2.3k stars 149 forks source link

Node 0.12 on OS X: Unexpected thread counts #48

Open akeating opened 9 years ago

akeating commented 9 years ago

Hi, I am running the example code listed but with fibo(30) instead of fibo(40) and using a valid port number:

var Worker = require('webworker-threads').Worker;
require('http').createServer(function (req,res) {
  var fibo = new Worker(function() {
    function fibo (n) {
      return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
    }
    this.onmessage = function (event) {
      postMessage(fibo(event.data));
    }
  });
  fibo.onmessage = function (event) {
    res.end('fib(30) = ' + event.data);
  };
  fibo.postMessage(30);
}).listen(8080);

Steps to reproduce:

  1. Using OSX 10.10.2 and node 0.12.0, start server using code listed above
  2. Observe node process is using 5 threads
  3. Using Chrome, navigate to localhost:8080
  4. Observe node process is using 17 threads, an increase of twelve
  5. Click refresh (at localhost:8080)
  6. Observe node process using 27 threads
  7. Continue to repeat steps 5 & 6, observing an increase of ten threads for each refresh.

Although this Worker api is not explicitly configured using a thread pool, I do expect the thread count to start off with some kind of default pool size and remain there. Both the size of the thread count increase is unexpected and as is the behavior of a never-ending increase in count.

This is just a basic example that serves to show the api; however this behavior really precludes the use of this lib in a long-lived application. It looks like there is fine-grained control over threading using some of the other apis. I am interested to see a better usage of the Worker api, that prevents this behavior using the steps listed, together with an explanation for what I am observing.

Thanks!

audreyt commented 9 years ago

Thanks for the report! There is indeed strange behavior observed in OS X with Node 0.12.0, which is why I disabled its use in EtherCalc at https://github.com/audreyt/ethercalc/blob/master/src/sc.ls#L39 — I would :heart: if someone more versed in C++ can look into this.

akeating commented 9 years ago

Hi @audreyt thanks for the update. I am also seeing the same issue using both node 0.11.14 and 0.10.35, following the steps listed above. The latter increases by 2 threads per refresh instead of 5. Interestingly, two Workers are instantiated for each request.

audreyt commented 9 years ago

That is normal (I think), as one can destroy/terminate the two workers at end of request.