Skip to content

AbortSignal.timeout() may not abort past the timeout #1461

@nstepien

Description

@nstepien

What is the issue with the DOM Standard?

When running synchronous code, signal.aborted does not become true after the expected timeout, signal.throwIfAborted() does not throw either.

This can be an issue in, for example, testing frameworks and lead to tasks never aborting if synchronous work keeps running.

I'd like to write code like this, but it's impossible as it stands:

const signal = AbortSignal.timeout(500);

while (!signal.aborted) {
  // perform work...
  task(signal);
}

Repro code:

const signal = AbortSignal.timeout(500);
const start = Date.now();
const timeout = start + 5000;

signal.addEventListener('abort', () => {
  console.log('signal aborted');
});

while (Date.now() < timeout) {
  // this should throw after 500ms
  signal.throwIfAborted();
}

console.error('this log should be unreachable');
console.log('signal.aborted:', signal.aborted);
console.log('elapsed:', Date.now() - start);
signal.throwIfAborted();

Result:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: abortingAbortController and AbortSignal

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions