From 5bd127e5abf214954559dfda6315e4e4533abdcd Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Wed, 18 Mar 2026 10:30:02 +0900 Subject: [PATCH 1/2] Fix `spec:valgrind` task by adopting `ruby_memcheck` The existing `spec:valgrind` task fails with the following error on modern environments: `NameError: uninitialized constant RUBY` Instead of just fixing the outdated constant, this commit replaces the old custom Valgrind command builder with the `ruby_memcheck` gem. This modernizes the memory check process and automatically handles Valgrind suppressions for Ruby's internal memory allocations, making it much easier to detect actual memory leaks in the C extension. --- Rakefile | 23 +++++------------------ gems.rb | 4 ++++ 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/Rakefile b/Rakefile index 33c7df3..7ebe8f4 100644 --- a/Rakefile +++ b/Rakefile @@ -48,24 +48,11 @@ namespace :build do end end -# adapted from http://flavoriffic.blogspot.com/2009/06/easily-valgrind-gdb-your-ruby-c.html -def specs_command - require "find" - files = [] - Find.find("spec") do |f| - files << f if File.basename(f) =~ /.*spec.*\.rb$/ - end - cmdline = "#{RUBY} -I.:lib:ext:spec \ - -e '%w[#{files.join(' ')}].each { |f| require f }'" -end - -namespace :spec do - desc "run specs with valgrind" - task :valgrind => :compile do - system "valgrind --num-callers=15 \ - --partial-loads-ok=yes --undef-value-errors=no \ - --tool=memcheck --leak-check=yes --track-fds=yes \ - --show-reachable=yes #{specs_command}" +if RUBY_PLATFORM.include?('linux') + require 'ruby_memcheck' + require 'ruby_memcheck/rspec/rake_task' + namespace :spec do + RubyMemcheck::RSpec::RakeTask.new(valgrind: :compile) end end diff --git a/gems.rb b/gems.rb index 48a8cd5..0c1e7ed 100644 --- a/gems.rb +++ b/gems.rb @@ -7,3 +7,7 @@ gem "bake-gem" gem "bake-modernize" end + +group :development, :test do + gem 'ruby_memcheck', '~> 3.0' if RUBY_PLATFORM.include?('linux') +end From 778b43d860140c514dca2d818be26d7aa9d10814 Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Wed, 18 Mar 2026 10:47:00 +0900 Subject: [PATCH 2/2] Add memory check in CI --- .github/workflows/valgrind.yaml | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 .github/workflows/valgrind.yaml diff --git a/.github/workflows/valgrind.yaml b/.github/workflows/valgrind.yaml new file mode 100644 index 0000000..249a3c8 --- /dev/null +++ b/.github/workflows/valgrind.yaml @@ -0,0 +1,42 @@ +name: Memcheck + +on: [push, pull_request] + +permissions: + contents: read + +jobs: + test: + name: ${{matrix.ruby}} on ${{matrix.os}} + runs-on: ${{matrix.os}}-latest + continue-on-error: ${{matrix.experimental}} + + strategy: + matrix: + os: + - ubuntu + + ruby: + - "3.2" + - "3.3" + - "3.4" + - "4.0" + + experimental: [false] + + include: + - os: ubuntu + ruby: head + experimental: true + + steps: + - uses: actions/checkout@v6 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{matrix.ruby}} + bundler-cache: true + - name: Install Valgrind + run: sudo apt-get install -y valgrind + - name: Run tests + timeout-minutes: 10 + run: bundle exec rake spec:valgrind