Repository navigation
fix: free syntax trees and rendered HTML returned by cmark - #90
Open
zachary822 wants to merge 1 commit into
Open
zachary822 wants to merge 1 commit into
zachary822 wants to merge 1 commit into
Conversation
cmark-gfm hands ownership of these allocations to the caller, but the bindings never released them, so every render leaked memory: - markdown_to_html() and render_html() copied the C string returned by cmark_markdown_to_html()/cmark_render_html() into Python and never freed it. cmark-gfm.h: "It is the caller's responsibility to free the returned buffer." - markdown_to_html_with_extensions() freed the parser but not the tree from cmark_parser_finish(), which detaches the root from the parser (parser->root = NULL), so parser_free() never releases it. This was the larger leak, several times the input size per call. In a long-running process the growth is unbounded: 2,000 renders of a 64 KB document grew RSS by ~190 MB with markdown_to_html() and ~3.4 GB with markdown_to_html_with_extensions(). Declare cmark_get_default_mem_allocator(), cmark_node_free() and cmark_render_html_with_mem() in the cffi definitions, and: - free rendered HTML after decoding it. render_html() now renders with an explicit default allocator via cmark_render_html_with_mem(), because cmark_render_html() uses the tree's allocator, which isn't reachable from Python (cmark_node_mem() is a private inline). - free the syntax tree in markdown_to_html_with_extensions(), including when parsing fails. - add node_free() so callers of parse_document() and parser_finish() can release the trees they own, and document that ownership. Add a regression test that renders repeatedly and checks peak RSS stays flat. It fails against the previous code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
cmark-gfm hands ownership of these allocations to the caller, but the
bindings never released them, so every render leaked memory:
cmark_markdown_to_html()/cmark_render_html() into Python and never
freed it. cmark-gfm.h: "It is the caller's responsibility to free the
returned buffer."
from cmark_parser_finish(), which detaches the root from the parser
(parser->root = NULL), so parser_free() never releases it. This was
the larger leak, several times the input size per call.
In a long-running process the growth is unbounded: 2,000 renders of a
64 KB document grew RSS by ~190 MB with markdown_to_html() and ~3.4 GB
with markdown_to_html_with_extensions().
Declare cmark_get_default_mem_allocator(), cmark_node_free() and
cmark_render_html_with_mem() in the cffi definitions, and:
an explicit default allocator via cmark_render_html_with_mem(),
because cmark_render_html() uses the tree's allocator, which isn't
reachable from Python (cmark_node_mem() is a private inline).
when parsing fails.
can release the trees they own, and document that ownership.
Add a regression test that renders repeatedly and checks peak RSS stays
flat. It fails against the previous code.
Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Human note :)
Fixes memory leak, feel free to close PR if not welcome. No hard feelings.