Skip to content

fix: free syntax trees and rendered HTML returned by cmark - #90

Open
zachary822 wants to merge 1 commit into
theacodes:masterfrom
zachary822:fix-memory-leak
Open

zachary822 wants to merge 1 commit into
theacodes:masterfrom
zachary822:fix-memory-leak

Conversation

@zachary822

Copy link
Copy Markdown

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

Human note :)
Fixes memory leak, feel free to close PR if not welcome. No hard feelings.

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>
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

Successfully merging this pull request may close these issues.

1 participant