Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak when ParsingError #1830

Merged
merged 1 commit into from
May 28, 2024
Merged

Conversation

ksss
Copy link
Collaborator

@ksss ksss commented May 25, 2024

I have discovered that a memory leak occurs when a ParsingError happens. This can occur in the following methods.

  • RBS::Parser.parse_type
  • RBS::Parser.parse_method_type
  • RBS::Parser.parse_signature

If a global exit occurs before free_parser is called, free_parser may not be called at all.

I have fixed this issue by using rb_ensure to ensure that free_parser is called under all circumstances.

$ cat ensure.rb
#! /usr/bin/env ruby

require 'rbs'

Thread.new {
  while true
    mem = `ps -o rss= -p #{Process.pid}`.to_i
    puts mem
    sleep 1
  end
}

while true
  begin
    RBS::Parser.parse_signature("c")
  rescue RBS::ParsingError
  end
end

# before
$ bundle exec ruby -I lib ensure.rb
37200
230096
421920
614544
806752
995488
1185664
1376944
1567616
1755488

# after
$ bundle exec ruby -I lib ensure.rb
33024
33152
33152
33184
33280
33328
33328

Ensure call free_parser() after parsing
Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@soutaro soutaro added this to the RBS 3.5 milestone May 28, 2024
@soutaro soutaro added this pull request to the merge queue May 28, 2024
Merged via the queue into ruby:master with commit f8ad373 May 28, 2024
17 checks passed
@ksss ksss deleted the fix-memory-leak branch May 28, 2024 09:36
@soutaro soutaro added the Released PRs already included in the released version label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Released PRs already included in the released version
Development

Successfully merging this pull request may close these issues.

2 participants