Skip to content

Commit

Permalink
fix(memory): Ensure RelaxNG parsing does not leak the parser context
Browse files Browse the repository at this point in the history
Fixes #2114
  • Loading branch information
flavorjones committed Nov 30, 2020
1 parent fbd65cd commit 47e2516
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ This release ends support for:
* The Node methods add_previous_sibling, previous=, before, add_next_sibling, next=, after, replace, and swap now correctly use their parent as the context node for parsing markup. These methods now also raise a RuntimeError if they are called on a node with no parent. [[nokogumbo#160](https://github.com/rubys/nokogumbo/issues/160)
* [CRuby] Fixed installation on AIX with respect to `vasprintf`. [[#1908](https://github.com/sparklemotion/nokogiri/issues/1908)]
* [CRuby] On some platforms, avoid symbol name collision with glibc's `canonicalize`. [[#2105](https://github.com/sparklemotion/nokogiri/issues/2105)]
* [JRuby] Standardize reading from IO like objects, including StringIO. [[#1888](https://github.com/sparklemotion/nokogiri/issues/1888), [#1897](https://github.com/sparklemotion/nokogiri/issues/1897)]
* [CRuby] `RelaxNG.from_document` no longer leaks memory. [[#2114](https://github.com/sparklemotion/nokogiri/issues/2114)]
* [Windows Visual C++] Fixed compiler warnings and errors. [[#2061](https://github.com/sparklemotion/nokogiri/issues/2061), [#2068](https://github.com/sparklemotion/nokogiri/issues/2068)]
* [JRuby] Standardize reading from IO like objects, including StringIO. [[#1888](https://github.com/sparklemotion/nokogiri/issues/1888), [#1897](https://github.com/sparklemotion/nokogiri/issues/1897)]
* [JRuby] Fixed document encoding regression in v1.11.0 release candidates. [[#2080](https://github.com/sparklemotion/nokogiri/issues/2080), [#2083](https://github.com/sparklemotion/nokogiri/issues/2083)] (Thanks, [@thbar](https://github.com/thbar)!)
Expand Down
1 change: 1 addition & 0 deletions ext/nokogiri/xml_relax_ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ static VALUE from_document(VALUE klass, VALUE document)
schema = xmlRelaxNGParse(ctx);

xmlSetStructuredErrorFunc(NULL, NULL);
xmlRelaxNGFreeParserCtxt(ctx);

if(NULL == schema) {
xmlErrorPtr error = xmlGetLastError();
Expand Down
17 changes: 17 additions & 0 deletions test/test_memory_leak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,23 @@ def test_leaking_dtd_nodes_after_internal_subset_removal
puts MemInfo.rss if (i % 1000 == 0)
end
end

describe "#2114 RelaxNG schema parsing has a small memory leak" do
it "no longer leaks" do
prev_rss = MemInfo.rss
100_001.times do |j|
Nokogiri::XML::RelaxNG.from_document(Nokogiri::XML::Document.parse(File.read(ADDRESS_SCHEMA_FILE)))
if (j % 10_000 == 0)
curr_rss = MemInfo.rss
diff_rss = curr_rss - prev_rss
printf("\n(iter %d) %d", j, curr_rss)
printf(" (%s%d)", diff_rss >= 0 ? "+" : "-", diff_rss) if j > 0
prev_rss = curr_rss
end
end
puts
end
end
end # if NOKOGIRI_GC

module MemInfo
Expand Down

0 comments on commit 47e2516

Please sign in to comment.