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

OpenApiValidator's match middleware causes memory leak in release 0.60 onward due to Ajv v7 changes #79

Open
munelear opened this issue May 8, 2022 · 0 comments

Comments

@munelear
Copy link

munelear commented May 8, 2022

The code path implicated in the memory leak is this usage of this.validate inside the middleware that is returned by match, because this.validate internally is calling ajv's compile.

this.validate(method, match.path)(req, res, next);

Per AJV's issues and documentation, a significant change was made between v6->v7 where it is expected for consumers to only call compile once. A potential solution would be to evaluate this.validate(method, match.path) as part of the initialization of the middleware, for each path, and cache the result once. Or, lazily initialize the cache on the first invocation. It sounds like they intend for this cache to be available to the application globally. This way in the middleware it's just retrieving the outcome from the cache rather than recompiling the schemas on every request, thus causing the memory leak.

Another potential solution mentioned in the issue discussion is using the ajv option addUsedSchema: false, but it's not the recommended solution, and per the discussion it sounds like there may be shortcomings of using it.
ajv-validator/ajv#1413
https://ajv.js.org/guide/managing-schemas.html

They also provided this blog post discussing the different strategies:
https://www.poberezkin.com/posts/2021-02-11-ajv-version-7-big-changes-and-improvements.html#caching-compiled-schemas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant