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
Possible memory leak in custom keywords #1879
Comments
also for me 👍 |
why are you passing the schema in request body? Normally, you would have schema to validate against on the server, why are you receiving it from the client? Firstly, the client can pass incorrect schema. Secondly there are multiple security risks associated with untrusted schemas, as they are compiled to code... See https://ajv.js.org/security.html#untrusted-schemas In any case, the crux of memory leak problem is this issue: #1413 From v8 the schema itself is used as the key, not its serialisation. If you do need to recompile schema on every validation, which is very inefficient, you should implement your own schema caching outside of Ajv. But ideally you would compile schema once, use many times, which is not what is happening now. This article could also be helpful: https://ajv.js.org/guide/managing-schemas.html |
Sorry. That was my mistake. It was just a simple code (bad example) to show the problem. I keep my schema server side.
Thats because I have a large collection of schemas stored on my server.
Thanks for your reply. @epoberezkin |
So you could refer to them by schema ID, in which case Ajv itself can be used as a cache. As long as you don't pass the whole schema object (that's probably is loaded from somewhere, so it's a new object every time), it will be fine. And what happens now is that Ajv compiles it again (which is performance hit, so you are not getting any value from compilation), and it also consumes the memory - which is what you are observing. And the leak happens because Ajv keeps caching them using schema itself as a key. What's ironic is that the change was made for performance reasons, to avoid schema serialisations and the memory that serialized schemas take - but the net effect in case of incorrect usage is exactly the opposite... |
so your logic could be either load them all on start, or if you want to load lazily, check by ID in ajs first (with getSchema) - if it's there, use it, if not - load and add to ajv. In general it's best to never use .validate method, and instead use .getSchema / .compile (it adds to the instance by default) or .addSchema / .getSchema (the latter returning the same validation function that is returned by .compile). |
I probably should just remove .validate from readme examples and put a note in API docs to discourage its use. |
I have a server that validates every request first. When my server has a high number of requests, heap usage goes up until I face memory leak error. This problem happens when I update AJV from 6 to 8 version. Here is a small sample of my code.
What version of Ajv are you using?
8.9.0
Does the issue happen if you use the latest version?
yes
Ajv options object
Your code
package.json dependencies
myAjv.js
myServer.js
myScript.js
result
What results did you expect?
heap usage stayed more stable. I also repeat this test with ajv@6 and there was no problem(everything was normal)
Are you going to resolve the issue?
No sry!!
The text was updated successfully, but these errors were encountered: