Skip to content

Commit

Permalink
os/linux/ld: harden brewed_ld_so_diagnostics against TypeError
Browse files Browse the repository at this point in the history
I think this is a bug in Ruby, but I've no idea how to track it down. I
can reproduce it intermittently in a codespace when `brew install`ing a
large number of formulae.

To work around this:
- cache the return value of `brewed_ld_so_diagnostics` so that we can
  limit the number of calls to `IO.popen`
- retry once when we see a `TypeError`

Closes #17828.
  • Loading branch information
carlocab committed Sep 16, 2024
1 parent 505d184 commit 65b76ed
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions Library/Homebrew/os/linux/ld.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,36 @@ module Linux
module Ld
sig { returns(String) }
def self.brewed_ld_so_diagnostics
@brewed_ld_so_diagnostics ||= T.let(nil, T.nilable(String))
@retried_brewed_ld_so_diagnostics ||= T.let(false, T.nilable(T::Boolean))

# Check for `#nil?` here because @brewed_ld_so_diagnostics could be a blank string.
return @brewed_ld_so_diagnostics unless @brewed_ld_so_diagnostics.nil?

brewed_ld_so = HOMEBREW_PREFIX/"lib/ld.so"
return "" unless brewed_ld_so.exist?

ld_so_output = Utils.popen_read(brewed_ld_so, "--list-diagnostics")
return "" unless $CHILD_STATUS.success?
@brewed_ld_so_diagnostics = if $CHILD_STATUS.success?
ld_so_output
elsif @retried_brewed_ld_so_diagnostics
""
else
@retried_brewed_ld_so_diagnostics = true
sleep 0.5
brewed_ld_so_diagnostics
end

@brewed_ld_so_diagnostics
rescue TypeError
# Workaround for intermittent `Error: no implicit conversion of false into String`
unless @retried_brewed_ld_so_diagnostics
@retried_brewed_ld_so_diagnostics = true
sleep 0.5
retry

Check warning on line 36 in Library/Homebrew/os/linux/ld.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/os/linux/ld.rb#L35-L36

Added lines #L35 - L36 were not covered by tests
end

ld_so_output
raise

Check warning on line 39 in Library/Homebrew/os/linux/ld.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/os/linux/ld.rb#L39

Added line #L39 was not covered by tests
end

sig { returns(String) }
Expand Down

0 comments on commit 65b76ed

Please sign in to comment.