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

Set max old space size on coverage and test #5382

Merged
merged 10 commits into from
Dec 19, 2024

Conversation

arr00
Copy link
Contributor

@arr00 arr00 commented Dec 17, 2024

Fixes #5363

Hardhat has a known issue which results in JavaScript heap out of memory error unless the max old space size is set. This PR will automatically set it on required scripts (test and coverage).

Note: this fix only sets the NODE_OPTIONS max-old-space-size flag if it was not set already. Additional options in NODE_OPTIONS are maintained. The new flag is not maintained after the call is completed.

PR Checklist

  • Tests
  • Documentation
  • Changeset entry (run npx changeset add)

Copy link

changeset-bot bot commented Dec 17, 2024

⚠️ No Changeset found

Latest commit: d44125c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ernestognw
Copy link
Member

From the docs:

--max-old-space-size=SIZE (in MiB)
Sets the max memory size of V8's old memory section. As memory consumption approaches the limit, V8 will spend more time on garbage collection in an effort to free unused memory.

On a machine with 2 GiB of memory, consider setting this to 1536 (1.5 GiB) to leave some memory for other uses and avoid swapping.

node --max-old-space-size=1536 index.js

I'm not sure what swapping refers to, but I'm worried setting it to 8192 makes the "old space size" constant and perhaps too much in low-capability devices. It doesn't say whether the default is calculated depending on the host, but I assume so based on this comment.

Copy link
Collaborator

@Amxx Amxx left a comment

Choose a reason for hiding this comment

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

  • why 4096 and not more ? In the past (before the hardhat migration to EDR 4096 was not enough)
  • why only on coverage and not on test ?

@arr00
Copy link
Contributor Author

arr00 commented Dec 17, 2024

  • why 4096 and not more ? In the past (before the hardhat migration to EDR 4096 was not enough)

    • why only on coverage and not on test ?

I'm testing on multiple machines (including CI) to see what we need. Ideally it would be below 8192 so that 8gb machines could run coverage. The test run requires significantly less memory to the point that the default is generally sufficient. We could set it to something like 4096 to just be safe though.

package.json Outdated Show resolved Hide resolved
Copy link
Collaborator

@Amxx Amxx left a comment

Choose a reason for hiding this comment

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

I'm really not a big fan of this change:

  • It fixes a value that may be to-much to to little depending on the evolution of the tests and tooling. Hardhat evolution did lower the needed value in the past. Same is true of coverage. Also, as we add more tests this could become outdated
  • Different machines (hardware, os) will have different things available.

If our estimation is too tight, there is a risk we have to update it to frequently. If the estimation is not tight enough, it will some "small" machines

IMO this value should be controlled by the host of the machine. Not by us. And an issue with what this PR proposes is that it doesn't allow the machine's host to override the value we fix without editing the package.json.

package.json Outdated
@@ -25,7 +25,7 @@
"prepack": "scripts/prepack.sh",
"generate": "scripts/generate/run.js",
"version": "scripts/release/version.sh",
"test": "hardhat test",
"test": "NODE_OPTIONS=\"--max-old-space-size=8192\" hardhat test",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we remove that from the tests?

My understanding is that it was only really needed for the coverage ... and we now have an elegant integration in coverage.sh.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What are your thoughts on the current state?

@arr00 arr00 requested a review from Amxx December 18, 2024 21:43
Comment on lines 16 to 18
env:
NODE_OPTIONS: --max_old_space_size=8192

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd keep this as the CI config. If this is ever modified, the script would not override it.

if [[ $NODE_OPTIONS != *"--max-old-space-size"* ]]; then
export NODE_OPTIONS="${NODE_OPTIONS} --max-old-space-size=8192"
fi
eval "scripts/set-max-old-space-size.sh"
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need an eval here ?

Suggested change
eval "scripts/set-max-old-space-size.sh"
scripts/set-max-old-space-size.sh

Copy link
Contributor Author

@arr00 arr00 Dec 18, 2024

Choose a reason for hiding this comment

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

This was already updated. The line is now

. ./scripts/set-max-old-space-size.sh

Copy link

socket-security bot commented Dec 19, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
pypi/[email protected] environment, eval, filesystem, network, shell 0 48.6 MB certora, shellyg
pypi/[email protected] eval, filesystem, shell, unsafe 0 1.01 MB daejunpark, justin_gerard

View full report↗︎

@Amxx Amxx merged commit e8f24d6 into OpenZeppelin:master Dec 19, 2024
16 checks passed
@arr00 arr00 deleted the chore/set-node-old_space_size branch December 19, 2024 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Coverage and test runs often result in JavaScript heap out of memory
3 participants