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: emit process warning if delay is negative or NaN #46678

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

jakecastelli
Copy link
Contributor

@jakecastelli jakecastelli commented Feb 16, 2023

Partially addressed #46596 to keep the consistency of the warning message for TIMEOUT_MAX number as the negative number, NaN will be set to 1 as well.

@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 Feb 16, 2023
lib/internal/timers.js Outdated Show resolved Hide resolved
@jakecastelli

This comment was marked as outdated.

@jakecastelli jakecastelli changed the title timer: improve warning message for negative number timers: improve warning message for negative number Feb 16, 2023
@jakecastelli
Copy link
Contributor Author

I personally would prefer to add TimeoutInvalidDelayWarning for both negative number or NaN but keen to hear others thought on this.

doc/api/timers.md Outdated Show resolved Hide resolved
Copy link
Member

@debadree25 debadree25 left a comment

Choose a reason for hiding this comment

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

Hi I think this would require a test too that the warning is emitted as per the conditions

@jakecastelli
Copy link
Contributor Author

Hi I think this would require a test too that the warning is emitted as per the conditions

Hi Debadree, I was wondering if we should add a new warning name(s) to address negative number and NaN or just leave it empty, for sure, I will add tests.

@debadree25
Copy link
Member

we should add a new warning name(s)

Didn't get you here 😅 new name for the warning?

@jakecastelli
Copy link
Contributor Author

we should add a new warning name(s)

Didn't get you here 😅 new name for the warning?

Sorry, I was trying to refer to here, when the delay is greater than 2**31 - 1 the process would emit TimeoutOverflowWarning, you could also find it in the process.md here, I was wondering if we should add warning for the negative number and NaN

@debadree25
Copy link
Member

Ah, I think having a name would be a good thing, would follow the pattern and make it easier to test maybe something like TimeoutNaNWarning or TimeoutNegativeWarning should be good

@jakecastelli jakecastelli requested review from debadree25 and removed request for mscdex February 19, 2023 10:42
@jakecastelli
Copy link
Contributor Author

Sorry, I didn't know that when you request a new review would remove the existing review request 😅

@jakecastelli jakecastelli changed the title timers: improve warning message for negative number timers: improve warning message for setTimeout delay Feb 19, 2023
@jakecastelli jakecastelli changed the title timers: improve warning message for setTimeout delay timers: improve warning message for timer delay Feb 19, 2023
@jakecastelli
Copy link
Contributor Author

I forgot to rebase @debadree25 sorry, would you mind running the workflows again 🙏

Copy link
Member

@debadree25 debadree25 left a comment

Choose a reason for hiding this comment

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

The code looks good! will wait for a few more reviews!

lib/internal/timers.js Outdated Show resolved Hide resolved
doc/api/timers.md Outdated Show resolved Hide resolved
@debadree25 debadree25 added the review wanted PRs that need reviews. label Feb 19, 2023
@jakecastelli

This comment was marked as off-topic.

@panva
Copy link
Member

panva commented Feb 27, 2023

Yup.

@jakecastelli
Copy link
Contributor Author

Sorry guys, its been a while. Just wanted to get a bit of directions, are you guys happy with the current state of the code or want me to make more changes (e.g. emit error per process for negative number and NaN)

@debadree25
Copy link
Member

I think lets make it once per process and land it!

Emit process warning when delay is a negative number or not a number,
this will keep the consistency of the warning message for `TIMEOUT_MAX`
number As the negative number will be set to 1 by default.
fix: add warning names

fix: use NumberIsNaN from primordials

tests: add tests for warnings
@jakecastelli
Copy link
Contributor Author

There is a bit of inconsistency in NodeJS docs here it says

setTimeout(callback[, delay[, ...args]])

delay is an option argument however down below

delay The number of milliseconds to wait before calling the callback. Default: 1.

It didn't say optional, while MDN specifically says delay is optional here

Anyway...

This PR will emit warning on code like this:

setTimeout(() =>{ /* do something */ })

Should we do this to the after see here?

after === undefined ? 1 : after *= 1; // Coalesce to number or NaN

ping @jasnell @debadree25 👀

@debadree25
Copy link
Member

I think we should not emit a warning in the case of setTimeout(() =>{ /* do something */ }) only explicitly passing an invalid value should we generate a warning

@jakecastelli
Copy link
Contributor Author

Agreed @debadree25, patched a fix so it will only emit when the argument is invalid.

@debadree25 debadree25 added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 5, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 5, 2024
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

This should follow the usual deprecation cycle – i.e. first a doc-only deprecation should land, then this PR can land as semver-major turning it into a runtime deprecation.

@jakecastelli
Copy link
Contributor Author

This should follow the usual deprecation cycle – i.e. first a doc-only deprecation should land, then this PR can land as semver-major turning it into a runtime deprecation.

Hi @aduh95 thanks for the feedback! Can I quickly ask about the deprecation part, as I didn't see if there is any deprecation in my PR, just wondering if I am missing out something.

@jakecastelli
Copy link
Contributor Author

ping @aduh95 👀

@jakecastelli
Copy link
Contributor Author

Would be great if I can get a bit more direction on this PR.

@aduh95
Copy link
Contributor

aduh95 commented May 15, 2024

Sorry for missing your previous pings!

What's being deprecated is passing NaN or a negative number to setTimeout/setInterval.

Before this PR:

$ node -e 'setInterval(console.log,-1).close();setInterval(console.log,NaN).close();setTimeout(console.log,-1);setTimeout(console.log,NaN)'

With this PR:

$ node -e 'setInterval(console.log,-1).close();setInterval(console.log,NaN).close();setTimeout(console.log,-1);setTimeout(console.log,NaN)'
(node:43520) TimeoutNegativeWarning: -1 is a negative number.
Timeout duration was set to 1.
(Use `node --trace-warnings ...` to show where the warning was created)
(node:43520) TimeoutNaNWarning: NaN is not a number.
Timeout duration was set to 1.

As you can see, code that was working before is now emitting a warning.

Existing stable public APIs that change in a backward-incompatible way must
undergo deprecation. The exceptions to this rule are:
* Adding or removing errors thrown or reported by a public API.
* Changing error messages for errors without error code.
* Altering the timing and non-internal side effects of the public API.
* Changes to errors thrown by dependencies of Node.js, such as V8.
* One-time exceptions granted by the TSC.

All deprecations receive a unique and immutable identifier. Documentation,
warnings, and errors use the identifier when referring to the deprecation. The
documentation for the deprecation identifier must always remain in the API
documentation. This is true even if the deprecation is no longer in use (for
example, due to removal of an End-of-Life deprecated API).
<a id="deprecation-cycle"></a>
A _deprecation cycle_ is a major release during which an API has been in one of
the three Deprecation levels. Documentation-Only Deprecations can land in a
minor release. They can not change to a Runtime Deprecation until the next major
release.
No API can change to End-of-Life without going through a Runtime Deprecation
cycle. There is no rule that deprecated code must progress to End-of-Life.
Documentation-Only and Runtime Deprecations can remain in place for an unlimited
duration.
Communicate pending deprecations and associated mitigations with the ecosystem
as soon as possible. If possible, do it before the pull request adding the
deprecation lands on the `main` branch.
Use the `notable-change` label on pull requests that add or change the
deprecation level of an API.

@jakecastelli
Copy link
Contributor Author

Much thanks! Now I understood.

jakecastelli added a commit to jakecastelli/node that referenced this pull request May 15, 2024
jakecastelli added a commit to jakecastelli/node that referenced this pull request May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. review wanted PRs that need reviews. 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.

None yet

8 participants