Skip to content

Commit

Permalink
Add a diagnostic for a bad re2 gem
Browse files Browse the repository at this point in the history
When the system re2 library gets updated, it's often difficult and
time-consuming to determine that a `gem pristine re2` is needed. This
`gdk doctor` diagnostic attempts to reproduce the seg fault that occurs
as a result of mudge/re2#43.
  • Loading branch information
stanhu authored and ashmckenzie committed May 4, 2020
1 parent e32d090 commit ba32ef4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/gdk/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative 'diagnostic/geo'
require_relative 'diagnostic/git'
require_relative 'diagnostic/ruby_gems'
require_relative 'diagnostic/re2'

module GDK
module Diagnostic
Expand All @@ -23,6 +24,7 @@ def self.all
PendingMigrations
Geo
Status
Re2
]

klasses.map do |const|
Expand Down
36 changes: 36 additions & 0 deletions lib/gdk/diagnostic/re2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'open3'

module GDK
module Diagnostic
class Re2 < Base
TITLE = 're2'
SCRIPT = "require 're2'; regexp = RE2::Regexp.new('{', log_errors: false); regexp.error unless regexp.ok?"

def diagnose
# When re2 and libre2 are out of sync, a seg fault can occur due
# to some memory corruption (https://github.com/mudge/re2/issues/43).
# This test doesn't always fail the first time, so repeat the test
# several times to be sure.
5.times do
@stdout, @stderr, @status = Open3.capture3('ruby', '-e', SCRIPT)
break unless @status.success?
end
end

def success?
@status.success? && @stdout.empty? && @stderr.empty?
end

def detail
<<~MESSAGE
It looks like your system re2 library may have been upgraded, and
the re2 gem needs to be rebuilt as a result.
Please run `gem pristine re2`.
MESSAGE
end
end
end
end
3 changes: 2 additions & 1 deletion spec/lib/gdk/diagnostic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
GDK::Diagnostic::Dependencies,
GDK::Diagnostic::PendingMigrations,
GDK::Diagnostic::Geo,
GDK::Diagnostic::Status
GDK::Diagnostic::Status,
GDK::Diagnostic::Re2
]

expect(described_class.all.map(&:class)).to eq(diagnostic_classes)
Expand Down

0 comments on commit ba32ef4

Please sign in to comment.