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 bb20b3c
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions Library/Homebrew/os/linux/ld.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@ module Linux
module Ld
sig { returns(String) }
def self.brewed_ld_so_diagnostics
@brewed_ld_so_diagnostics ||= T.let({}, T.nilable(T::Hash[Pathname, T.nilable(String)]))

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_target = brewed_ld_so.readlink
@brewed_ld_so_diagnostics[brewed_ld_so_target] ||= begin
ld_so_output = Utils.popen_read(brewed_ld_so, "--list-diagnostics")
ld_so_output if $CHILD_STATUS.success?
end

@brewed_ld_so_diagnostics[brewed_ld_so_target].to_s
rescue TypeError
# Workaround for intermittent `Error: no implicit conversion of false into String`
unless @retried_brewed_ld_so_diagnostics&.fetch(brewed_ld_so_target, false)
@retried_brewed_ld_so_diagnostics ||= T.let({}, T.nilable(T::Hash[Pathname, T::Boolean]))
@retried_brewed_ld_so_diagnostics[brewed_ld_so_target] = true
sleep 0.5
retry

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/os/linux/ld.rb#L26-L28

Added lines #L26 - L28 were not covered by tests
end

ld_so_output
raise

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L31 was not covered by tests
end

sig { returns(String) }
Expand Down

0 comments on commit bb20b3c

Please sign in to comment.