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

fix(NODE-4281): ensure that the driver always uses Node.js timers #3275

Merged
merged 2 commits into from Jun 1, 2022

Conversation

addaleax
Copy link
Contributor

Description

What is changing?

Ensure that the driver always uses the Node.js timers API, rather
than the global one, in case they diverge. This affects Compass,
where the setTimeout(...).unref() usage currently results in
uncaught exceptions because setTimeout() returns a number in
browsers.

This change adds import ... from 'timers'; where necessary and
adds a linter rule to prevent regressions. If this is not
an acceptable solution, we can go back to the drawing board,
but this seems like a good solution that doesn’t add too much
overhead when writing driver code.

Is there new documentation needed for these changes?

No

What is the motivation for this change?

See ticket – setTimeout().unref() doesn’t work in Compass renderers

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: <type>(NODE-xxxx)<!>: <description>
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@durran durran added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label May 31, 2022
@durran durran self-assigned this May 31, 2022
@durran
Copy link
Member

durran commented May 31, 2022

Just for clarification, this is specifically for when the driver is used in the Electron renderer process, where the global functions are the browser functions? And we still have Node available so simply requiring 'timers' gets us the Node specific ones in that environment?

@addaleax
Copy link
Contributor Author

@durran Yes, exactly 👍

durran
durran previously approved these changes May 31, 2022
@durran durran added Team Review Needs review from team and removed Primary Review In Review with primary reviewer, not yet ready for team's eyes labels May 31, 2022
@durran durran removed their assignment May 31, 2022
@@ -0,0 +1,22 @@
'use strict';
const sinon = require('sinon');

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm okay with this approach but could another approach be only linting for global setTimeout/Immediate/Intervals in the src directory? As long as Compass isn't running our tests I think that should be okay.

I don't have a strong preference, but the benefit of only linting the src directory is one fewer custom test configurations to maintain. @addaleax

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@baileympearson I’d also be okay with applying the linting only there, I guess what would mean a new .eslintrc file inside src? That should be easy enough.

That being said, this particular file would still be necessary, because it’s about how sinon mocks the timers functions (which are then picked up by files in the src/ directory)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, right. That makes sense.

If we need this file either way, I'd rather lint everything for consistency 😃

nbbeeken
nbbeeken previously approved these changes May 31, 2022
baileympearson
baileympearson previously approved these changes Jun 1, 2022
Ensure that the driver always uses the Node.js timers API, rather
than the global one, in case they diverge. This affects Compass,
where the `setTimeout(...).unref()` usage currently results in
uncaught exceptions because `setTimeout()` returns a number in
browsers.

This change adds `import ... from 'timers';` where necessary and
adds a linter rule to prevent regressions. If this is not
an acceptable solution, we can go back to the drawing board,
but this seems like a good solution that doesn’t add too much
overhead when writing driver code.
@addaleax addaleax dismissed stale reviews from baileympearson, nbbeeken, and durran via a4dd3da June 1, 2022 14:50
@durran durran merged commit 4501a1c into mongodb:main Jun 1, 2022
@addaleax addaleax deleted the node-4281-dev branch June 1, 2022 15:49
alecgibson added a commit to alecgibson/node-mongodb-native that referenced this pull request Jul 19, 2022
This change follows on from mongodb#3275
which enforced using Node.js timer methods.

However, it didn't enforce using the Node.js _clear_ methods, which
could fall prey to similar issues.

This change adds linter rules for those clear methods, and fixes the
resulting linter errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Needs review from team
Projects
None yet
4 participants