-
Notifications
You must be signed in to change notification settings - Fork 357
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
[prism-cli] Memory leak while validating #1881
Comments
@radzserg @chohmann The leak might come from AJV. I've created a trimmed down version of the repro in ajv-validator/ajv#1763 As I'm FAR from being a Prism, nor AJV, nor schema expert, I'd greatly appreciate if you could help me there from the Prism use case standpoint. |
Answer from AJV author @ ajv-validator/ajv#1763 (comment)
My understanding is that Prism is actually incorrectly leveraging AJV. |
I've taken a look at the code and may have an (hypothetical) idea of a (potential) approach. Unless anyone is already working on a fix, I'll start digging my way through tomorrow. |
@radzserg Ok. I've got something that works and plugs the leak. The code is currently in an "ugly as hell" format but works without making the CI cringe. I'm now going to try to un-uglify it. Stay tuned! |
cool 👀 👍 |
How about using |
@lukasz-kuzynski-11sigma Thanks for the heads up. However, AJV already implements a caching layer and I'm not sure we should add one of ours on top of it. What I'm after is slightly different. I'm working to avoid "dynamically" re-generating the schema with every call to The moment we're invoking The idea is to generate those as part of I've got something that already works on my box and will finish fixing up the tests tomorrow. I believe I should be able to show case something tomorrow evening so that you and @radzserg have something to poke at. |
Makes sense my friend. Fingers crossed and happy to review your changes. |
@lukasz-kuzynski-11sigma did you mean WeakMap. Can you add more details? How exactly do you propose to use it? @nulltoken FYI // packages/http/src/validator/validators/utils.ts
const validationFunctionsCache: { [key: string]: ValidateFunction } = {};
function getValidationFunction(
schemaName: string,
coerce: boolean,
schema: JSONSchema,
bundle?: unknown
): ValidateFunction {
const validationSchema = {
...schema,
__bundled__: bundle,
};
const key = `${schemaName}_${coerce}_` + JSON.stringify(validationSchema);
if (!validationFunctionsCache[key]) {
validationFunctionsCache[key] = assignAjvInstance(String(schema.$schema), coerce).compile(validationSchema);
}
return validationFunctionsCache[key];
}
// and then
export const validateAgainstSchema = (
value: unknown,
schema: JSONSchema,
coerce: boolean,
prefix?: string,
bundle?: unknown
): O.Option<NonEmptyArray<IPrismDiagnostic>> => {
return pipe(
O.tryCatch(() => getValidationFunction(String(schema.$schema), coerce, schema, bundle)),
// O.tryCatch(() =>
// assignAjvInstance(String(schema.$schema), coerce).compile({
// ...schema,
// __bundled__: bundle,
// })
// ),
O.chainFirst(validateFn => O.tryCatch(() => validateFn(value))), I've updated your repro test to 10K and it looks good Looking forward for your final solution |
@nulltoken Although on bigger numbers I still see leaking with my quick fix 🤔 ... Your approach beats mine |
I've came with an idea to cache schema through WeakMap but @nulltoken is absolutely right here. We're recreating json schema object many time therefore the only kind of cache that might work is the one introduced by @radzserg. Maybe I'll come with a little bit different solution soon... |
Ok I got this guys... #1892 Let me know what you think and I'll polish the solution. |
Describe the bug
Following #1860 by @lukasz-kuzynski-11sigma we've been able to bump from Prism 4.1.2 to Prism 4.3.4.
However, some of servers started to restart due to Out of Memory issues. We've tracked down the issue to
validateInput
andvalidateOutput
functions leaking memory.To Reproduce
A complete standalone repro case is available at https://github.com/nulltoken/prism_repro
It triggers a loop of 1000 request and response validations, capturing the amount of used Heap every 10 steps, and triggering a garbage collection between every step.
Running
yarn repro
shows the following output where one can clearly see the heap growing.Expected behavior
No leak ? ;-)
/cc @chohmann
The text was updated successfully, but these errors were encountered: