Skip to content
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

Possible bugs in the code. #387

Closed
iSuslov opened this issue Mar 30, 2023 · 3 comments
Closed

Possible bugs in the code. #387

iSuslov opened this issue Mar 30, 2023 · 3 comments
Labels

Comments

@iSuslov
Copy link

iSuslov commented Mar 30, 2023

While I'm rewriting the code to Typescript, I found two things that I would rather ask here since it looks like bugs.

  1. Lets take a look here:

* @param {number} [timeout] If provided and non-zero, worker termination promise will be rejected
* after timeout if worker process has not been terminated.
* @return {Promise.<WorkerHandler, Error>}
*/
WorkerHandler.prototype.terminateAndNotify = function (force, timeout) {
var resolver = Promise.defer();
if (timeout) {
resolver.promise.timeout = timeout;
}
this.terminate(force, function(err, worker) {
if (err) {
resolver.reject(err);

On the line 490 there is a assignment resolver.promise.timeout = timeout;. Doc of the method says timeout param is a number, but Promise object has a method called timeout. Seems like an attempt to override a method with a number. Task object however has a property timeout:number. Luckily it seems nowhere in the code this method WorkerHandler.prototype.terminateAndNotify gets called with the second argument.

Is it a bug in abandoned code?

  1. Second one. Lets take a look at how TERMINATE_METHOD_ID is used.

var TERMINATE_METHOD_ID = '__workerpool-terminate__';

if (this.worker.ready) {
this.worker.send(TERMINATE_METHOD_ID);
} else {
this.requestQueue.push(TERMINATE_METHOD_ID);
}

There is no problem when it gets directly sent to a worker, however when it gets added to the requestQueue there is a problem, because this is how requestQueue is being processed:

// send all queued requests to worker
function dispatchQueuedRequests()
{
for(const request of me.requestQueue.splice(0)) {
me.worker.send(request.message, request.transfer);
}
}

Looks like instead of TERMINATE_METHOD_ID undefined gets sent. Correct me if I'm wrong please.

@josdejong
Copy link
Owner

Good finds Ivan!

  1. I think this is indeed a bug. The method terminateAndNotify is called with the second timeout argument only from the public Pool.terminate method. I commented the line resolver.promise.timeout = timeout;, but still all tests pass. I expect that the terminate timeout simply doesn't work and isn't tested. The solution most likely is to change that line to resolver.promise.timeout(timeout).

  2. I think you're right, it should be something like this.requestQueue.push({ message: TERMINATE_METHOD_ID });.

I think it's best to address these bugs separately from the refactor and make sure they are properly tested.

@Michsior14
Copy link
Contributor

Both bugs have been resolved :)

@josdejong
Copy link
Owner

Both issues are fixed now in v6.4.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants