Skip to content

Bug: Promise.finally() does not await returned promises (spec violation) #502

@MSNev

Description

@MSNev

Description

In lib/src/promise/base.ts (lines 147-163), the finally() implementation does not await promises returned by the onFinally callback. Per the ES2018 spec, if the callback returns a thenable, the resulting promise should be delayed until that thenable settles.

Impact

Cleanup operations that return promises (e.g., closing connections, flushing buffers) will not complete before the promise chain continues, leading to:

  • Resource leaks (connections not properly closed)
  • Race conditions (subsequent operations start before cleanup finishes)
  • Data loss (buffers not flushed)

Steps to Reproduce

javascript let cleaned = false; const p = createAsyncPromise(resolve => resolve('done')); p.finally(() => { return new Promise(r => setTimeout(() => { cleaned = true; r(); }, 100)); }).then(() => { console.log(cleaned); // Should be true, actually false });

Suggested Fix

After calling onFinally(), check if the result is thenable. If so, return a new promise that waits for it before resolving/rejecting with the original value/reason.

Severity

High — violates ES2018 spec, causes resource leaks and race conditions in cleanup code.

Metadata

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