Skip to content

Promise readline question results in unsettled promise on abortion #53497

Description

@029A-h

Version

v22.2.0

Platform

Linux LX-LAPII 6.9.3-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 31 May 2024 15:14:45 +0000 x86_64 GNU/Linux

Subsystem

node:readline/promises

What steps will reproduce the bug?

It is not possible to properly handle a user's abortion of a promise readline question with SIGINT or Ctrl+D while rl.question waits for user input:

import * as readline from 'node:readline/promises';

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

try {
  const answer = await rl.question('>>> ');
} catch {}

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

Every time when a prompt is aborted with Ctrl+C or Ctrl+D.

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

The expected behavior is to settle a promise on a readline close event, as shown in the following snippet:

import * as readline from 'node:readline';

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let rej = () => {};
rl.on('close', () => {
  console.log('closing');
  rej();
});

const answer = await new Promise((r, j) => {
  rej = j;
  rl.question('>>> ', r);
}).catch(() => console.error('interrupted'));

What do you see instead?

The promise remains unsettled, resulting in a warning that cannot be caught:

>>> Warning: Detected unsettled top-level await at file:///tmp/bug.mjs:9
  const answer = await rl.question('>>> ');

Additional information

This issue is opened based on the discussion in [stackoverflow] How to properly abort Node readline promise question?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    promisesIssues and PRs related to ECMAScript promises.readlineIssues and PRs related to the built-in readline module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions