Skip to content

waitFor does not properly cancel #174

Closed
@s1hofmann

Description

Version
1.5.0

Short overview
Extracted from #173

Issue occurs on

  • Virtual machine
  • Docker container
  • Dev/Host system

Detailed error description

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:

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

Steps to reproduce error

Additional content

Please provide any (mandatory) additional data to reproduce the error (Dockerfiles etc.)

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();
    });
  });
}

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions