Skip to content

[Bug] Fix singleton class lookup bug when singleton class is replaced on the instance #3088

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

Merged

Conversation

itarato
Copy link
Collaborator

@itarato itarato commented Jun 2, 2023

IO#reopen is one of those few examples (maybe the only one) where the caller object's singleton class is replaced:

https://github.com/oracle/truffleruby/blob/master/src/main/ruby/truffleruby/core/io.rb#L1901

This means, methods defined only on an IO object's singleton class won't be available after calling #reopen on it.

However, currently in TruffleRuby fetching the singleton class for an object is cached against the object - which makes an assumption that the same object must have the same singleton class during its lifetime. As the beginning states, this is not the case for IO objects currently.

An example where this occurs on production: ActiveSupport's test runner contains an IO capture mechanism: https://github.com/rails/rails/blob/0e99d0893b0e98c626a3c7d8972eea22d29c9d25/activesupport/lib/active_support/testing/stream.rb#L23-L38 This effectively erases STDOUT's singleton class. Any extension on STDOUT's singleton class, eg with this public gem could cause a lookup of the old singleton class function.

The actual break on production was a bit more nuanced:

  • the linked gem re-aliased the IO#write method with a new proxy method, both on the singleton class (to enhance the functionality of STDOUT.write).
  • a test was leveraging ActiveSupport's #capture mechanism, which deleted the singleton class
  • a follow up STDOUT.write was - due to the bug - directed to the old metaclass' .write method, which failed to call the old alias.

To sum up the bug's effect, after replacing a singleton class:

  • singleton methods and aliases can be "mixed up" causing method-not-find
  • new singleton methods/aliases not being found

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Jun 2, 2023
@itarato itarato force-pushed the feature/PA-Singleton-class-lookup-bug-fix branch from 0927461 to ec0f4f1 Compare June 2, 2023 18:34
@itarato itarato self-assigned this Jun 2, 2023
@itarato itarato added the shopify Pull requests from Shopify label Jun 2, 2023
@itarato itarato force-pushed the feature/PA-Singleton-class-lookup-bug-fix branch from ec0f4f1 to ec5aa00 Compare June 2, 2023 18:54
@itarato itarato force-pushed the feature/PA-Singleton-class-lookup-bug-fix branch from ec5aa00 to 4fd4410 Compare June 2, 2023 19:05
@oracle-contributor-agreement
Copy link

Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
The following contributors of this PR have not signed the OCA:

To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application.

When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated.

If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public.

@oracle-contributor-agreement oracle-contributor-agreement bot added OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. and removed OCA Verified All contributors have signed the Oracle Contributor Agreement. labels Jun 2, 2023
@itarato itarato force-pushed the feature/PA-Singleton-class-lookup-bug-fix branch from 4fd4410 to 1af8708 Compare June 2, 2023 20:27
@oracle-contributor-agreement oracle-contributor-agreement bot added OCA Verified All contributors have signed the Oracle Contributor Agreement. and removed OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. labels Jun 2, 2023
@itarato itarato force-pushed the feature/PA-Singleton-class-lookup-bug-fix branch 3 times, most recently from 52157b9 to 628e973 Compare June 3, 2023 17:17
@eregon eregon added the in-ci The PR is being tested in CI. Do not push new commits. label Jun 5, 2023
@eregon eregon marked this pull request as ready for review June 5, 2023 11:56
…e instance.

Co-authored-by: Kevin Menard <[email protected]>
Co-authored-by: Peter Arato <[email protected]>
Co-authored-by: Benoit Daloze <[email protected]>
@eregon eregon force-pushed the feature/PA-Singleton-class-lookup-bug-fix branch from 628e973 to 400a93a Compare June 5, 2023 12:29
@eregon eregon changed the title [WIP] [Bug] Fix singleton class lookup bug when singleton class is replaced on the instance [Bug] Fix singleton class lookup bug when singleton class is replaced on the instance Jun 5, 2023
@graalvmbot graalvmbot merged commit 0f64063 into oracle:master Jun 5, 2023
@XrXr XrXr deleted the feature/PA-Singleton-class-lookup-bug-fix branch November 7, 2024 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in-ci The PR is being tested in CI. Do not push new commits. OCA Verified All contributors have signed the Oracle Contributor Agreement. shopify Pull requests from Shopify
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants