Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timers: fix validation #54404

Merged
merged 1 commit into from
Aug 25, 2024
Merged

timers: fix validation #54404

merged 1 commit into from
Aug 25, 2024

Conversation

ShogunPanda
Copy link
Contributor

@ShogunPanda ShogunPanda commented Aug 16, 2024

This PR improves timers/promises in the following way:

  1. timers.set* now use the same validation logic. All arguments, including the delay argument (where appropriate) are now validated.
  2. timers.scheduler.wait now supports the options.ref option.
  3. Fixed the documentation for timers.scheduler.wait. It was not just "roughly" equivalent to timers.setTimeout, it was just a wrapper around it. I updated the docs.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. labels Aug 16, 2024
@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 16, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 16, 2024
@nodejs-github-bot
Copy link
Collaborator

Copy link

codecov bot commented Aug 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.33%. Comparing base (e70bd47) to head (b73e72b).
Report is 17 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #54404      +/-   ##
==========================================
- Coverage   87.34%   87.33%   -0.02%     
==========================================
  Files         649      649              
  Lines      182524   182614      +90     
  Branches    35026    35036      +10     
==========================================
+ Hits       159420   159478      +58     
- Misses      16373    16403      +30     
- Partials     6731     6733       +2     
Files Coverage Δ
lib/timers/promises.js 98.34% <100.00%> (+0.07%) ⬆️

... and 37 files with indirect coverage changes

@nodejs-github-bot
Copy link
Collaborator

@H4ad
Copy link
Member

H4ad commented Aug 16, 2024

I like the idea but maybe doing this is an argument to introduce sleep on timers, a sync version of it (which will probably block the event loop), which can be implemented with Atomic.wait.

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

I don't think we should do this (roll our third new promises timers API).

Also right below it you have scheduler.wait (4 letters) we can bind it to the scheduler which would let you do wait (4 letters) vs. sleep (5 letters) if that's a meaningful metric.

@ShogunPanda
Copy link
Contributor Author

@benjamingr That's different, IMHO. The scheduler API is experimental and thus might go away at anytime.

setTimeout is not going anywhere anytime soon.

The number of letters doesn't really matter (thanks autocompletion ;)) but I'd like to give users a simple API they can use without the risk of overriding a well known global and therefore having side effects.

@benjamingr
Copy link
Member

@ShogunPanda we currently have 4 "official" ways to set timeouts (setTimeout, promises setTimeout, scheduler.postTask and scheduler.wait) as well as lots of others for specific timeouts (e.g. AbortSignal.timeout) . That's... already too many ways to do the same thing anyway, I doubt adding another would improve things.

If there is a consensus I'll remove my block.

The scheduler API is experimental and thus might go away at anytime.

So is any new API we add like this one?

@ShogunPanda
Copy link
Contributor Author

ShogunPanda commented Aug 17, 2024

I agree: let's wait for other consensus and can solve this.

@ShogunPanda ShogunPanda added the tsc-agenda Issues and PRs to discuss during the meetings of the TSC. label Aug 21, 2024
@ShogunPanda ShogunPanda changed the title timers: add sleep alias timers: fix validation Aug 24, 2024
@ShogunPanda
Copy link
Contributor Author

@benjamingr @jasnell @legendecas

As agreed in TSC/1608 I started working on the warning for timers.setTimeout.

I then noticed that it was not the documentation that was lacking but rather the validation.
I therefore refactored and unified the validation for timers.setTimeout, timers.setInterval and timers.setImmediate, ensuring also the delay argument is properly validated.

Also, I noticed that documentation for scheduler.wait was incorrect: scheduler.wait is totally equivalent (as it internally uses it) to call timers.setTimeout. Therefore we can support the options.ref as well. I updated code and documentation to reflect that.

At the end of the day, since I just refactored internal parts (timers.set*) and add support for options.ref in scheduler.wait (which is experimental anyway) I think this PR should be semver-minor.

@ShogunPanda ShogunPanda added request-ci Add this label to start a Jenkins CI on a PR. and removed tsc-agenda Issues and PRs to discuss during the meetings of the TSC. labels Aug 24, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 24, 2024
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@benjamingr benjamingr dismissed their stale review August 24, 2024 20:46

per consensus

Copy link
Member

@ovflowd ovflowd left a comment

Choose a reason for hiding this comment

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

So is this pretty much a refactor/cleanup? And the only thing actually being fixed are the docs?

Copy link
Member

@ovflowd ovflowd left a comment

Choose a reason for hiding this comment

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

It looks clean to me -- LGTM

@ShogunPanda
Copy link
Contributor Author

Kinda, yes

@ShogunPanda ShogunPanda added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Aug 25, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 25, 2024
@nodejs-github-bot nodejs-github-bot merged commit 6d654dd into nodejs:main Aug 25, 2024
58 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 6d654dd

@ShogunPanda ShogunPanda deleted the sleep branch August 25, 2024 07:39
Comment on lines +535 to +536
Calling `timersPromises.scheduler.wait(delay, options)` is equivalent
to calling `timersPromises.setTimeout(delay, undefined, options)`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

@ShogunPanda ShogunPanda Aug 25, 2024

Choose a reason for hiding this comment

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

Because the documentation was incorrect (or not up-to-date). It was not roughly equivalent, it was strictly equivalent since it just wrapping it. I made that explicit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then I guess we should document ref as a valid option

Comment on lines +148 to +150
} catch (err) {
return PromiseReject(err);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why catch the error in the first place? Isn't returning a promise rejection strictly equivalent in an async function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I copied and pasted from setTimeout (which is not async) and missed that. Do you think we can leave like that or do we need a tiny fixing PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

It would have been more "clean" to close that PR about adding an alias for setTimeout, and open a new one for the validation changes. That way, folks watching the repo would have had more chance to notice the change, and our wait time policy would have give enough time for folks to chime in (all the changes happen during a weekend, making it even more likely fewer folks had time to have a look). All in all, I don't really care if you do the follow-up PR with my suggestion, but IMO it's the "right" thing to do.

@ovflowd
Copy link
Member

ovflowd commented Aug 25, 2024

I feel this landed prematurely. Although I gave approval I believe having at least an extra one would have been good.

Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

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

LGTM

louwers pushed a commit to louwers/node that referenced this pull request Aug 25, 2024
PR-URL: nodejs#54404
Reviewed-By: Claudio Wunder <cwunder@gnome.org>
RafaelGSS pushed a commit that referenced this pull request Aug 30, 2024
PR-URL: #54404
Reviewed-By: Claudio Wunder <cwunder@gnome.org>
RafaelGSS pushed a commit that referenced this pull request Aug 30, 2024
PR-URL: #54404
Reviewed-By: Claudio Wunder <cwunder@gnome.org>
@RafaelGSS RafaelGSS mentioned this pull request Aug 30, 2024
targos pushed a commit that referenced this pull request Sep 21, 2024
PR-URL: #54404
Reviewed-By: Claudio Wunder <cwunder@gnome.org>
targos pushed a commit that referenced this pull request Sep 26, 2024
PR-URL: #54404
Reviewed-By: Claudio Wunder <cwunder@gnome.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run. timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants