Skip to content

Fix memory leak when exception is raised during JSON generation part 2#882

Merged
byroot merged 1 commit into
ruby:masterfrom
rhenium:ky/generate-fix-fbuffer-leak
Oct 31, 2025
Merged

Fix memory leak when exception is raised during JSON generation part 2#882
byroot merged 1 commit into
ruby:masterfrom
rhenium:ky/generate-fix-fbuffer-leak

Conversation

@rhenium

@rhenium rhenium commented Oct 31, 2025

Copy link
Copy Markdown
Member

Commit 44df509 fixed it for StandardError, but other exceptions and jumps are also possible. Use rb_ensure() to release FBuffer instead of rb_rescue().

A reproducer:

o = Object.new
def o.to_json(a) = throw :a
a = ["make heap allocation"*100, o]

10.times do
  100_000.times do
    catch(:a) { JSON(a) }
  end

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

Commit 44df509 fixed it for StandardError, but other exceptions and
jumps are also possible. Use rb_ensure() to release FBuffer instead of
rb_rescue().

A reproducer:

    o = Object.new
    def o.to_json(a) = throw :a
    a = ["make heap allocation"*100, o]

    10.times do
      100_000.times do
        catch(:a) { JSON(a) }
      end

      puts `ps -o rss= -p #{$$}`
    end
@byroot byroot merged commit e1f4224 into ruby:master Oct 31, 2025
37 checks passed
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.

2 participants