Skip to content

Commit 40b6616

Browse files
committed
Run valgrind on CI to check memory leaks
Many gems with C-extensions have been using valgrind to check memory leaks, such as `prism`, `lrama`, and `json`. The simplest way to run valgrind on CI is through `ruby_memcheck` gem, which is used by both `prism` and `json`. So I followed its readme to add a new job `valgrind` to `ruby.yml`.
1 parent 1209f49 commit 40b6616

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

.github/workflows/ruby.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,29 @@ jobs:
8080
- name: Run test
8181
run: |
8282
bundle exec rake ${{ matrix.job }}
83+
valgrind:
84+
runs-on: ubuntu-latest
85+
steps:
86+
- uses: actions/checkout@v2
87+
- uses: ruby/setup-ruby@v1
88+
with:
89+
ruby-version: "3.4"
90+
bundler-cache: none
91+
- name: Set working directory as safe
92+
run: git config --global --add safe.directory $(pwd)
93+
- name: Set up permission
94+
run: chmod -R o-w /opt/hostedtoolcache/Ruby
95+
- name: Install dependencies
96+
run: |
97+
sudo apt-get update
98+
sudo apt-get install -y libdb-dev curl autoconf automake m4 libtool python3 valgrind
99+
- name: Update rubygems & bundler
100+
run: |
101+
ruby -v
102+
gem update --system
103+
- name: bin/setup
104+
run: |
105+
bin/setup
106+
- run: bundle exec rake test:valgrind
107+
env:
108+
RUBY_FREE_AT_EXIT: 1

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ group :profilers do
3737
gem 'stackprof'
3838
gem 'memory_profiler'
3939
gem 'benchmark-ips'
40+
gem "ruby_memcheck", platform: :ruby
4041
end
4142

4243
# Test gems

Gemfile.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ GEM
6060
logger (1.6.6)
6161
marcel (1.0.4)
6262
memory_profiler (1.1.0)
63+
mini_portile2 (2.8.8)
6364
minitest (5.25.4)
6465
mutex_m (0.3.0)
6566
net-protocol (0.2.2)
6667
timeout
6768
net-smtp (0.5.1)
6869
net-protocol
6970
nkf (0.2.0)
71+
nokogiri (1.18.3)
72+
mini_portile2 (~> 2.8.2)
73+
racc (~> 1.4)
7074
ostruct (0.6.1)
7175
parallel (1.26.3)
7276
parser (3.3.7.1)
@@ -126,6 +130,8 @@ GEM
126130
lint_roller (~> 1.1)
127131
rubocop (~> 1.72.1)
128132
ruby-progressbar (1.13.0)
133+
ruby_memcheck (3.0.1)
134+
nokogiri
129135
securerandom (0.4.1)
130136
stackprof (0.2.27)
131137
steep (1.9.4)
@@ -193,6 +199,7 @@ DEPENDENCIES
193199
rubocop
194200
rubocop-on-rbs
195201
rubocop-rubycw
202+
ruby_memcheck
196203
stackprof
197204
steep
198205
tempfile

Rakefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ require "rake/testtask"
33
require "rbconfig"
44
require 'rake/extensiontask'
55

6+
on_windows = /mswin|mingw/ =~ RUBY_PLATFORM
7+
8+
require "ruby_memcheck" if !on_windows
9+
610
$LOAD_PATH << File.join(__dir__, "test")
711

812
ruby = ENV["RUBY"] || RbConfig.ruby
@@ -11,14 +15,22 @@ bin = File.join(__dir__, "bin")
1115

1216
Rake::ExtensionTask.new("rbs_extension")
1317

14-
Rake::TestTask.new(:test => :compile) do |t|
18+
test_config = lambda do |t|
1519
t.libs << "test"
1620
t.libs << "lib"
1721
t.test_files = FileList["test/**/*_test.rb"].reject do |path|
1822
path =~ %r{test/stdlib/}
1923
end
2024
end
2125

26+
Rake::TestTask.new(test: :compile, &test_config)
27+
28+
unless on_windows
29+
namespace :test do
30+
RubyMemcheck::TestTask.new(valgrind: :compile, &test_config)
31+
end
32+
end
33+
2234
multitask :default => [:test, :stdlib_test, :typecheck_test, :rubocop, :validate, :test_doc]
2335

2436
task :lexer do

0 commit comments

Comments
 (0)