Skip to content

process is still alive when finishing the automation #173

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

Closed
1 of 3 tasks
bjrmatos opened this issue Oct 11, 2020 · 6 comments
Closed
1 of 3 tasks

process is still alive when finishing the automation #173

bjrmatos opened this issue Oct 11, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@bjrmatos
Copy link

bjrmatos commented Oct 11, 2020

Short summary

Seems like there is some active handle that prevents the node.js process to exit, probably related when execution the logic when trying to find a match for an image

Desired execution environment / tested on

  • Virtual machine (Windows 10, with node 14.12.0)
  • Docker container
  • Dev/Host system

Detailed question

I was working on my app and doing a lot of debugging and noticed that the process was not ended even when reaching the end of the execution, so i've used the why-is-node-running module to check what active resources/handles that prevent the process to exit.

here is the output of why-is-node-running:

# nan:AsyncWorker
Z:\accounting-processes-bot\node_modules\opencv4nodejs-prebuilt\lib\promisify.js:18                                      - fn.apply(this, args);
Z:\accounting-processes-bot\node_modules\opencv4nodejs-prebuilt\lib\promisify.js:9                                       - return new Promise((resolve, reject) => {
Z:\accounting-processes-bot\node_modules\@nut-tree\nut-js\dist\lib\provider\opencv\match-image.function.js:14            - const match = await haystack.matchTemplateAsync(needle, cv.TM_SQDIFF_NORMED);
Z:\accounting-processes-bot\node_modules\@nut-tree\nut-js\dist\lib\provider\opencv\template-matching-finder.class.js:92  - const matchResult = await match_image_function_1.matchImages(haystack, scaledNeedle);
Z:\accounting-processes-bot\node_modules\@nut-tree\nut-js\dist\lib\provider\opencv\template-matching-finder.class.js:118 - const matches = await this.findMatches(matchRequest, debug);

any clue how to prevent this? maybe there is some method that am i missing to call that closes something related to the opencv?

thanks.

@svettwer
Copy link
Contributor

Hi 👋

Thanks for reaching out. Would it be possible to provide some code of your automation on where the error occurs? Strange behavior most likely appears due to missing await statements or something like this. In addition, it would be good to know which nut.js version you are currently using.

@bjrmatos
Copy link
Author

Strange behavior most likely appears due to missing await statements or something like this. In addition, it would be good to know which nut.js version you are currently using

I'm using version 1.5.0, I've already double-checked if I have some missing await for the promises generated in the app, but all looks good.

Would it be possible to provide some code of your automation on where the error occurs?

unfortunately, there is a lot of abstraction in the codebase of my app, it will be hard just to provide single nut.js api calls that reproduce this. but basically, I'm just searching images a lot with waitFor, sometimes also doing parallel searches and waiting with Promise.all() for all of them to finish, not sure if that kind of usage can generate some leaked resource/handle. anyway according to the message reported by why-is-node-running this looks related to some calls done inside the OpenCV handling but did not found something wrong in those files.

I was trying to debug this case to find the cause, I did not find it but found another related bug with screen.waitFor, the waitFor uses this utility, which does not clear the interval correctly if the max timeout is reached before getting the result of the action().

this is the flow:

here is basically the code needed to fix this case in util/poll-action.function.ts, not sure if you want me to send a PR

export function timeout<R>(updateIntervalMs: number, maxDurationMs: number, action: (...params: any) => Promise<R>): Promise<R> {
  return new Promise<R>((resolve, reject) => {
    let interval: NodeJS.Timeout;
    let isDone = false;
    const maxTimeout = setTimeout(
      () => {
        isDone = true;
        clearTimeout(maxTimeout);
        if (interval) {
          clearTimeout(interval);
        }
        reject(`Action timed out after ${maxDurationMs} ms`);
      },
      maxDurationMs
    );
    const startInterval = () => {
      interval = setTimeout(function intervalFunc() {
        action().then((result) => {
          if (isDone) {
            return;
          }

          if (!result) {
            interval = setTimeout(intervalFunc, updateIntervalMs);
          } else {
            isDone = true;
            clearTimeout(maxTimeout);
            clearTimeout(interval);
            resolve(result);
          }
        }).catch(() => {
          if (isDone) {
            return;
          }

          interval = setTimeout(intervalFunc, updateIntervalMs);
        });
      }, updateIntervalMs);
    };

    action().then((result) => {
      if (isDone) {
        return;
      }

      if (!result) {
        startInterval();
      } else {
        isDone = true;
        clearTimeout(maxTimeout);
        resolve(result);
      }
    }).catch(() => {
      if (isDone) {
        return;
      }

      startInterval();
    });
  });
}

maybe something similar is happening in the files related to the OpenCV

@s1hofmann
Copy link
Member

Hi @bjrmatos 👋

Contributions are always welcome. Would be best to extract your waitFor finding to a separate issue to work on.

Thanks for reaching out!

@s1hofmann
Copy link
Member

Hi @bjrmatos,

now that #177 has been merged, could you check on this issue again?

Best regards

Simon

@s1hofmann s1hofmann added the bug Something isn't working label Nov 13, 2020
@bjrmatos
Copy link
Author

will do! i will test this during the next days, will be back here with information if this happens again.

sorry i was not able to submit a PR but i got burnout with some extra work :)

@bjrmatos
Copy link
Author

bjrmatos commented Dec 8, 2020

just to follow up on this, we did not see any other issue through all this time, so the fix was ok 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants