Skip to content

Fix limit memory usage of XML::Error.errors#14966

Closed
straight-shoota wants to merge 5 commits intocrystal-lang:masterfrom
straight-shoota:fix/xml-errors-limit
Closed

Fix limit memory usage of XML::Error.errors#14966
straight-shoota wants to merge 5 commits intocrystal-lang:masterfrom
straight-shoota:fix/xml-errors-limit

Conversation

@straight-shoota
Copy link
Member

This patch limits the size of XML::Error.errors to contain only the last 10 error messages.
A new class property XML::Error.max_error_capacity allows configuration of the error capacity. This property is already marked as deprecated because it'll vanish together with XML::Error.errors.

Resolves #14934
Closes #14936

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:serialization labels Sep 4, 2024
@straight-shoota straight-shoota self-assigned this Sep 4, 2024
Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming this time the spec is right 😅

@straight-shoota straight-shoota added this to the 1.14.0 milestone Sep 4, 2024
@straight-shoota straight-shoota added the breaking-change May have breaking effect in some edge cases. Mostly negligible, but still noteworthy. label Sep 4, 2024

@[Deprecated("This class property is deprecated. XML errors are accessible directly in the respective context via `XML::Reader#errors` and `XML::Node#errors`.")]
def self.max_error_capacity=(@@max_error_capacity)
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: if I want to disable the behavior, which is the way forward, I have to set it this value to 0, but then I get a deprecation notice 😕

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I don't think you should actually need to disable it explicitly.
You should of course be able to do that if you want. And in that case this method should not raise a deprecation message. But we want to get rid of this method as well, so it should still be deprecated.
Maybe we should mark it as Experimental instead? 🤔
Or don't annotate it at all but document it that it should always be guarded by compare_version(Crystal::VERSION, "1.13.2") > 0) && compare_version(Crystal::VERSION, "2.0.0") < 0).

@@max_error_capacity
end

@@errors = Deque(self).new(max_error_capacity)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: this initializer will run before user code, won't @max_error_capacity always be the default 5, whatever I set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, yeah it will 🤦

@ysbaddaden
Copy link
Collaborator

This is limiting the leak (it won't keep growing) but it' still allocating pointless strings that will have to be collected... for how long are we gonna keep this?

I'm leaning more to have XML::Error.errors always return nil from now on, with the documentation stating that it used to return errors but doesn't since v1.14, or just plain drop the method.

@straight-shoota
Copy link
Member Author

Yeah, I'm all in for disabling the feature entirely. This patch is just adding more complexity to something we need to get rid of anyway.
#14936 is ready as an alternative.

@bararchy
Copy link
Contributor

Should this be closed in favor of #14936 ?

@straight-shoota straight-shoota deleted the fix/xml-errors-limit branch October 20, 2024 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change May have breaking effect in some edge cases. Mostly negligible, but still noteworthy. kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:serialization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XML::Error.errors leaks memory

4 participants