-
Notifications
You must be signed in to change notification settings - Fork 76
Massive memory leak since 8.5.5 / leaking the entire stack frame and all local variables everywhere #198
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
Comments
Hi, Are there any plans to fix this in the near future? |
I understand your frustration, but please refrain from "how can it be", when referring to free and open source software. Instead, you can offer time or money to free software projects to unblock situations like this. |
* Test for memory leaks as described in #198 * Possible fix for #198: memory leak * Optimization: avoid holding frame reference when locals == globals * Get caller frame at decoration-time Here we are more careful about which caller's locals we use to resolve forward type references. We want the callers locals at decoration-time — not at decorator-construction time. Consider: ```py frozen_dataclass = marshmallow_dataclass.dataclass(frozen=True) def f(): @custom_dataclass class A: b: "B" @custom_dataclass class B: x: int ``` The locals we want in this case are the one from where the custom_dataclass decorator is called, not from where marshmallow_dataclass.dataclass is called. * Add ability to pass explicit localns (and globalns) to class_schema When class_schema is called, it doesn't need the caller's whole stack frame. What it really wants is a `localns` to pass to `typing.get_type_hints` to be used to resolve type references. Here we add the ability to pass an explicit `localns` parameter to `class_schema`. We also add the ability to pass an explicit `globalns`, because ... might as well — it might come in useful. (Since we need these only to pass to `get_type_hints`, we might as well match `get_type_hints` API as closely as possible.) * test: check for frame leakage when decorators throw exceptions * Fix mypy by setting python to the minimum supported version, 3.8 --------- Co-authored-by: Jeff Dairiki <[email protected]>
This is (mostly) fixed by #258 (marshmallow_dataclass >= 8.6.1). When necessary, references to frames are still held until the schema is constructed (by accessing the |
Hi @dairiki - I wanted to follow up regarding:
Does this mean that if a large number of these dataclasses are constructed using the dataclass constructor directly (without unmarshalling from data), then we still have a memory leak situation in the latest version of marshmallow-dataclass? If so, what is the point of deferring schema construction, if deferring leads to serious issues? The deferring is also responsible for a terrible race condition in multi-threaded applications - see #282. Can the Also, in this context, I am assuming that calling If I have a marshmallow dataclass and schema generation defined at top level of module like below, do I still need to be concerned about memory leaks after your fix?
|
Hi @lovasoa, sorry to bother you - could you please chime in on my comment above - #198 (comment)? Many thanks in advance. |
Hi! I'm sorry, I don't use this package myself anymore and currently do not have a lot of bandwidth to maintain it. Happy to add new maintainers, though! |
Thank you for following up, @lovasoa. Has there been an attempt to get the core marshmallow project to take marshmallow-dataclass under its wing? They seem like a great pairing. |
Uh oh!
There was an error while loading. Please reload this page.
In #187 a change was implemented that included the current stack frame in an internal cache, therefore causing all local variables of the entire call stack to never be freed. Demo:
No
__del__
is called on eitherSample
orOuter
during the method invocations, only after thecache_clear
can the local variables be garbage collected. If you run the demo with 8.4.4 or just replace_internal_class_schema(clazz, base_schema, clazz_frame)
with_internal_class_schema(clazz, base_schema, None)
in marshmallow_dataclass.class_schema. Additionally this also demonstrates that the caching does no longer work as there no cache hits at all anymore.The text was updated successfully, but these errors were encountered: