-
Notifications
You must be signed in to change notification settings - Fork 5.1k
fix: web3-validator ajv memory leak #5574
Conversation
Muhammad-Altabba
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution.
It seems great!
However, I think it would be better to use a hash for the key instead of the full JSON. And I added a comment regarding this.
Thanks,
|
I don't think I caused tests to fail, they seem to be flaky but I cannot rerun them^^ |
Sorry @mpetrunic but it seems to be caused by the last update as the pipeline issues keep showing when I re-run it 3 times. And it is only shown after the latest update to use the hash function. And so it would be so nice of you to count for the time of the hash function and to count how many times this hash function is called (to check if there is something wrong with the code that causes unneeded calls for example). |
Do you suggest increasing the timeout on those tests or reverting the last commit? I mean converting to bytes, hashing, and converting to hex has some cost and this is quite a hot path as it gets called for every validation. |
c58b831 to
e1a233e
Compare
Many thanks @mpetrunic |
e1a233e to
68b58a4
Compare
It generates a 32bit number as the hash. I don't think that has enough collision resistance. And collision here would be a nightmare to debug. Ideally, we would use md5 or sha1 but they are not available so I switched now to blake2b which should be the fastest. |
68b58a4 to
0733bc3
Compare
|
Merged this in to 4.x even there is release branch out there as: #5580 (comment) |
Description
Please include a summary of the changes and be sure to follow our Contribution Guidelines.
Ajv from version 7 onwards uses object references to compile and cache schema. Since web3.js is passing a new object/array for every single validation, this causes every object to be compiled and attached to the schema cache which results in a massive memory leak.
This PR removes that leak by stringifying schemas and using the stringified form as the cache key. But since we now store both string, and compiled schema, memory usage is higher.
Also, we still have leak if the user starts using one-time validation schemas. Which we would need to remove or they end up permanently attached to the default validator.
resolves #5515
Type of change
Checklist for 1.x:
npm run dtslintwith success and extended the tests and types if necessary.npm run test:covand my test cases cover all the lines and branches of the added code.npm run buildwith success.dist/web3.min.jsin a browser.CHANGELOG.mdfile in the root folder.Checklist for 4.x:
yarnsuccessfullyyarn lintsuccessfullyyarn build:websuccessfullyyarn test:unitsuccessfullyyarn test:integrationsuccessfullycompile:contractssuccessfullyCHANGELOG.mdfile in the packages I have edited.