Project

General

Profile

Actions

Bug #20653

closed

Memory leak in String#start_with? when regexp times out

Added by peterzhu2118 (Peter Zhu) 4 months ago. Updated 3 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:118686]

Description

GitHub PR: https://github.com/ruby/ruby/pull/11247

This commit refactors how Onigmo handles timeout. Instead of raising a timeout error, onig_search will return a ONIGERR_TIMEOUT which the caller can free memory, and then raise a timeout error.

This fixes a memory leak in String#start_with when the regexp times out. For example:

regex = Regexp.new("^#{"(a*)" * 10_000}x$", timeout: 0.000001)
str = "a" * 1000000 + "x"

10.times do
  100.times do
    str.start_with?(regex)
  rescue
  end

  puts `ps -o rss= -p #{$$}`
end

Before:

33216
51936
71152
81728
97152
103248
120384
133392
133520
133616

After:

14912
15376
15824
15824
16128
16128
16144
16144
16160
16160
Actions #1

Updated by peterzhu2118 (Peter Zhu) 4 months ago

  • Status changed from Open to Closed

Applied in changeset git|7464514ca5e825ada31522b63a33e02d62a0a248.


Fix memory leak in String#start_with? when regexp times out

[Bug #20653]

This commit refactors how Onigmo handles timeout. Instead of raising a
timeout error, onig_search will return a ONIGERR_TIMEOUT which the
caller can free memory, and then raise a timeout error.

This fixes a memory leak in String#start_with when the regexp times out.
For example:

regex = Regexp.new("^#{"(a*)" * 10_000}x$", timeout: 0.000001)
str = "a" * 1000000 + "x"

10.times do
  100.times do
    str.start_with?(regex)
  rescue
  end

  puts `ps -o rss= -p #{$$}`
end

Before:

33216
51936
71152
81728
97152
103248
120384
133392
133520
133616

After:

14912
15376
15824
15824
16128
16128
16144
16144
16160
16160

Updated by k0kubun (Takashi Kokubun) 3 months ago

  • Backport changed from 3.1: UNKNOWN, 3.2: WONTFIX, 3.3: REQUIRED to 3.1: UNKNOWN, 3.2: WONTFIX, 3.3: DONE
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0