Skip to content

refactor: use Promise.withResolvers() (Node 22+)#503

Merged
marcoscaceres merged 6 commits into
mainfrom
chore/promise-withresolvers
Apr 28, 2026
Merged

refactor: use Promise.withResolvers() (Node 22+)#503
marcoscaceres merged 6 commits into
mainfrom
chore/promise-withresolvers

Conversation

@marcoscaceres

Copy link
Copy Markdown
Collaborator

Summary

Replace new Promise((resolve, reject) => { ... }) with Promise.withResolvers() in 3 call sites:

  • utils/background-task-queue.ts — worker message listener and module registration
  • utils/sh.ts — shell exec wrapper

Cleaner code, same behavior. Node 22+ native API.

@marcoscaceres
marcoscaceres requested a review from Copilot April 25, 2026 12:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Refactors internal async flows to use Promise.withResolvers() instead of new Promise((resolve, reject) => ...), aiming for cleaner promise wiring while keeping behavior consistent on Node 22+.

Changes:

  • Refactor sh() exec wrapper to use Promise.withResolvers() for completion signaling.
  • Refactor BackgroundTaskQueue worker message listeners and module registration to use Promise.withResolvers().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
utils/sh.ts Replaces manual Promise constructor with Promise.withResolvers() around child process lifecycle.
utils/background-task-queue.ts Replaces Promise constructor usages in worker response handling and init registration with Promise.withResolvers().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread utils/sh.ts
Comment thread utils/sh.ts Outdated
Comment thread utils/sh.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread utils/background-task-queue.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread utils/background-task-queue.ts Outdated
Comment thread utils/sh.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread utils/sh.ts
Replace new Promise((resolve, reject) => { ... }) with the cleaner
Promise.withResolvers() pattern in background-task-queue.ts (2 sites)
and sh.ts (1 site).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread utils/sh.ts
Comment thread utils/background-task-queue.ts
Comment thread utils/background-task-queue.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread utils/sh.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@marcoscaceres

Copy link
Copy Markdown
Collaborator Author

@copilot Can you fix and resolve unaddressed comments and get this happy again?

Copilot AI added 2 commits April 27, 2026 08:16
…e.ts

- sh.ts: Remove broken duplicate Promise.withResolvers + new Promise code; use
  Promise.withResolvers() properly; switch exit->close for complete stdio flush;
  add error event handler for spawn failures; make rejection shapes consistent
  (both reject with Error instances with command/stdout/stderr/code fields)
- background-task-queue: Store split2 stream refs so off() targets the right
  stream and unpipe on cleanup; add worker error/exit handling in run() to
  prevent deadlocks on unexpected worker failures; add worker error/exit
  handling in activate() to prevent indefinite hangs during initialization

Agent-Logs-Url: https://github.com/speced/respec-web-services/sessions/4640ebb0-d6ac-4269-b7bf-e563038f39e1

Copilot AI commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

@copilot Can you fix and resolve unaddressed comments and get this happy again?

Fixed in eef7301. Here's what was addressed:

utils/sh.ts (commit 220d437):

  • Removed the broken duplicate Promise.withResolvers + new Promise code
  • Switched from exit to close event so stdio streams are fully flushed before settling
  • Added error event handler for spawn failures (promise no longer hangs indefinitely)
  • Made rejection shapes consistent — both error and non-zero exit paths now reject with an Error instance carrying { command, stdout, stderr, code } fields

utils/background-task-queue.ts (commits 220d437, eef7301):

  • Stored split2 stream references so off() and unpipe() target the correct transform stream (not worker.stdout/stderr directly)
  • Added worker error/exit listeners in run() — if the worker dies mid-job, the lock is released and the promise rejects with the job ID in the error message instead of deadlocking the queue
  • Added worker error/exit listeners in activate() — if the worker crashes during module initialization, the promise rejects rather than hanging indefinitely

@marcoscaceres

Copy link
Copy Markdown
Collaborator Author

@copilot can you review this and fix any issues that come up?

@marcoscaceres

Copy link
Copy Markdown
Collaborator Author

@copilot can you review this again and fix any issues

@marcoscaceres

Copy link
Copy Markdown
Collaborator Author

@copilot Can you confirm that all your comments have been addressed? Please resolve them if you’re happy. Can you fix anything outstanding from your feedback that hasn’t been addressed?

Copilot AI commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

@copilot Can you confirm that all your comments have been addressed? Please resolve them if you’re happy. Can you fix anything outstanding from your feedback that hasn’t been addressed?

All reviewer comments have been addressed. Here's the final status:

utils/sh.ts: close event (complete stdio flush), error handler (no more indefinite hangs on spawn failure), consistent Error-instance rejection shape with { command, stdout, stderr, code } on all paths.

utils/background-task-queue.ts: Split2 streams stored in variables so off()/unpipe() target the correct transform; run() has cross-removing error/exit handlers that release the lock and reject on worker death; activate() has the same pattern to prevent init hangs.

CI tests are passing on the latest commit (a3bd437).

@marcoscaceres
marcoscaceres marked this pull request as ready for review April 27, 2026 20:08
@marcoscaceres
marcoscaceres merged commit befebf4 into main Apr 28, 2026
4 checks passed
@marcoscaceres
marcoscaceres deleted the chore/promise-withresolvers branch April 28, 2026 12:12
marcoscaceres added a commit that referenced this pull request May 4, 2026
* refactor: use Promise.withResolvers() (Node 22+)

Replace new Promise((resolve, reject) => { ... }) with the cleaner
Promise.withResolvers() pattern in background-task-queue.ts (2 sites)
and sh.ts (1 site).

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix: address all PR review feedback on sh.ts and background-task-queue.ts

- sh.ts: Remove broken duplicate Promise.withResolvers + new Promise code; use
  Promise.withResolvers() properly; switch exit->close for complete stdio flush;
  add error event handler for spawn failures; make rejection shapes consistent
  (both reject with Error instances with command/stdout/stderr/code fields)
- background-task-queue: Store split2 stream refs so off() targets the right
  stream and unpipe on cleanup; add worker error/exit handling in run() to
  prevent deadlocks on unexpected worker failures; add worker error/exit
  handling in activate() to prevent indefinite hangs during initialization

Agent-Logs-Url: https://github.com/speced/respec-web-services/sessions/4640ebb0-d6ac-4269-b7bf-e563038f39e1

* fix: add job ID to worker unexpected-exit error message for easier debugging

Agent-Logs-Url: https://github.com/speced/respec-web-services/sessions/4640ebb0-d6ac-4269-b7bf-e563038f39e1

* fix: cross-remove error/exit listeners in run() to prevent double lock release

Agent-Logs-Url: https://github.com/speced/respec-web-services/sessions/5a7c6559-a65f-44e5-865b-b8a2f0b8c955

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants