Skip to content

fix uncaught error after timeout#3

Merged
Badisi merged 2 commits intoBadisi:mainfrom
sunnylqm:patch-1
Jun 29, 2025
Merged

fix uncaught error after timeout#3
Badisi merged 2 commits intoBadisi:mainfrom
sunnylqm:patch-1

Conversation

@sunnylqm
Copy link
Copy Markdown
Contributor

@sunnylqm sunnylqm commented May 7, 2025

reproduction step:

async function getLatestVersion(pkgNames: string[]) {
  return latestVersion(pkgNames, {
    requestOptions: {
      timeout: 1,
    },
  })
    .then((pkgs) => pkgs.map((pkg) => pkg.latest))
    .catch(() => []);
}

print error logs:

    const abort = (error) => {
      // request.removeAllListeners();
      request.destroy();
      reject(error);
    };
    request.once("timeout", () => {
      console.log("timeout");
      abort(`Request timed out: ${pkgUrl}`);
    });
    request.once("error", (err) => {
      console.log("error", err);
      abort(err);
    });
    request.on("close", () => {
      console.log("close");
      request.removeAllListeners();
    });

and you'll see something like:
image

because timeout event removes lisenters too early and request.destroy() may cause new errors

@Badisi Badisi self-assigned this Jun 29, 2025
@Badisi Badisi added the good first issue Good for newcomers label Jun 29, 2025
@Badisi Badisi merged commit afd57aa into Badisi:main Jun 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good first issue Good for newcomers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants