Skip to content

awaiting some fs/promises API (readdir, readfile) breaks later try catch block #51894

Open
@its-frank-huang

Description

@its-frank-huang

Version

v16.15.0, v20.10.0

Platform

Darwin Guang-Yu-Huang-Frank 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:59:33 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T8112 arm64

Subsystem

No response

What steps will reproduce the bug?

Steps

Init a new project

$ npm init -y

Install fs

$ npm install fs

Modify package.json

add "type": "module", in package.json

{
  "name": "cli",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fs": "^0.0.1-security"
  }
}

Copy code in one of the following cases to created file

Execute

Case 1: The promise is created before the first try-catch

Code

import { readdir } from 'fs/promises';

const main = async () => {
  const p = Promise.reject('test error');

  try {
    await readdir('.');
  } catch (error) {
    console.error('first promise rejected');
  }

  try {
    await p;
  } catch (error) {
    console.error('second promise rejected');
  }
};

main();

Result

image

Case 2: The promise is created after the first try-catch

Code

import { readdir } from 'fs/promises';

const main = async () => {
  try {
    await readdir('.');
  } catch (error) {
    console.error('first promise rejected');
  }
  
  const p = Promise.reject('test error');

  try {
    await p;
  } catch (error) {
    console.error('second promise rejected');
  }
};

main();

Result

image

Case 3: The promise is created before the first try-catch & not using readdir

Code

const main = async () => {
  const p = Promise.reject('test error');

  try {
    await Promise.resolve('test success');
  } catch (error) {
    console.error('first promise rejected');
  }

  try {
    await p;
  } catch (error) {
    console.error('second promise rejected');
  }
};

main();

Result

image

Conclusion

Somehow, the order of creation of the promise affects the behavior of the second try-catch block

How often does it reproduce? Is there a required condition?

100% on my and my co-workers' computers (all macOS)

What is the expected behavior? Why is that the expected behavior?

The second try-catch block should always catch the error, regardless of the order of creation

What do you see instead?

The second try-catch block doesn't catch the error when the promise is created before the first try-catch block

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    errorsIssues and PRs related to JavaScript errors originated in Node.js core.promisesIssues and PRs related to ECMAScript promises.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions