Project

General

Profile

Actions

Defect #44800

open

Memory leak when rendering code blocks

Defect #44800: Memory leak when rendering code blocks

Added by Katsuya HIDAKA about 20 hours ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
Text formatting
Target version:
-
Resolution:
Affected version:

Description

CopypreScrubber, which adds a copy button to code blocks, leaks memory. It caches one Nokogiri wrapper node and passes it to wrap for every pre. Because that node belongs to another document, each rendered HTML document stays in memory after rendering. The cached node was added in r24360 (#43643).

The attached patch creates a new wrapper node for each code block instead of caching it, as #43801 did for the copy button. This fixes the leak.

Live HTML documents after GC (Ruby 4.0.7, Nokogiri 1.19.7). CommonMark and Textile gave the same results:

Renders Before the patch After the patch
500 501 1
1,000 1,001 1

Reproduction script

Details:

  • Node#wrap duplicates the cached node and moves the copy into the document being rendered. The Ruby object of the copy stays in the node cache of the cached node's document. That document lives as long as the scrubber, and the scrubbers are constants (SCRUBBERS), so every rendered document stays in memory.
  • The HTML output does not change. I checked pre at the top level and inside li, blockquote, td, p and details.
  • Negligible performance impact. In a local benchmark with 20 code blocks per render, the median render time changed by less than 0.2 ms (about 2.5 ms per render).
  • All tests pass.

This may be related to #44766.


Files

No data to display

Actions

Also available in: PDF Atom