Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't rely on return value of ::GirlFriday::WorkQueue.immediate #1027

Merged
merged 1 commit into from
Mar 29, 2021

Conversation

waltjones
Copy link
Contributor

Description of the change

Note: The bug and the fix here are related to Ruby's implicit return. A Ruby method always returns whatever its last line evaluates to.

The CI break happens on Ruby 3.0.0 builds, and as will be explained, is caused by a change in Module#alias_method.
In Ruby < 3.x, alias_method returns the class object.
https://ruby-doc.org/core-2.7.1/Module.html#method-i-alias_method

Starting in Ruby 3.x, alias_method returns its first argument.
https://ruby-doc.org/core-3.0.0/Module.html#method-i-alias_method

So in Ruby 3.x:

alias_method :orig_exit, :exit #=> :orig_exit

Our code doesn't directly use alias_method. It uses GirlFriday::WorkQueue.immediate! which does use alias_method.

def self.immediate!
  alias_method :push, :push_immediately
  alias_method :<<, :push_immediately
end

https://github.com/mperham/girl_friday/blob/01c9e725bbba4d338d8dbda90f31ca40a83d3581/lib/girl_friday/work_queue.rb#L28-L31

Because of implicit return, this returned GirlFriday::WorkQueue until Ruby 3.0 where it now returns :<<.

The fix

The original code used the return value to mock the code under test so it would use the modified class. This is unnecessary because immediate! modifies the global class object itself, not an instance of the class. The modified class will be used without needing the mock. (If the mock were needed, you would use GirlFriday::WorkQueue directly, rather than taking the return from immediate!.)

Notes

This error reproduces with some seed values for rspec and not with others. I was able to reproduce locally by using the seed value from the failing CI build. It also reproduces by isolating the girl_friday tests: bundle exec rspec spec/rollbar/delay/girl_friday_spec.rb

So we know that when the specs ran in some order the error was masked, but I don't have insight into what specifically masked the error.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Related issues

ch80862

Development

  • Lint rules pass locally
  • The code changed/added as part of this pull request has been covered with tests
  • All tests related to the changed code pass in development

Code review

  • This pull request has a descriptive title and information useful to a reviewer. There may be a screenshot or screencast attached
  • "Ready for review" label attached to the PR and reviewers mentioned in a comment
  • Changes have been reviewed by at least one other engineer
  • Issue from task tracker has a link to this pull request

@waltjones waltjones mentioned this pull request Mar 4, 2021
10 tasks
Copy link
Contributor

@bxsx bxsx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great description @waltjones 👍

@waltjones waltjones merged commit 7576f9d into master Mar 29, 2021
@waltjones waltjones deleted the wj-alias-method-fix branch June 27, 2023 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants