Skip to content

Conversation

@chadlwilson
Copy link
Contributor

@chadlwilson chadlwilson commented Oct 10, 2025

As noted in jruby/warbler#575, jruby-rack can leave ENV['GEM_PATH'] and Gem.path in an inconsistent state.

This change

  • includes a workaround for the warbler issue (no warbler release required) to avoid unintended rubygems initialization triggered by require 'stringio' in our core_ext debugging monkey patches.
  • separate to the warbler issue, documents and fixes related issues caused by use of jruby.rack.env.gem_path to update GEM_PATH
    • ensures rubygems paths are cleared if the ENV['GEM_PATH'] ever needs to be updated with a relative gem path. (Default is true) This makes things safer in the case that we end up doing early initialization for some other reason than the stringio require, in future.
    • removes the unsafe, undocumented, and unable-to-be-made-safe jruby.rack.env.gem_path=false capability, which updated path directly and didn't touch env. This one always would break warbler, and don't see any reference to people using it. Warbler has its own separate approach.
  • improves the tests and simplifies the logic

Also manually tested this with current warbler master, and validated it fixes the issues linked to/discussed in jruby/warbler#575

…nt state

As noted at jruby/warbler#575 for warbler, it is similarly possible for jruby-rack to leave ENV['GEM_PATH'] and Gem.path in an inconsistent state after it updates them. This would make any later Gem.clear_paths or Bundler usage (driven from the ENV vars) unpredictable.

This happens because jruby-rack alters `ENV['GEM_PATH']``, in some cases but does not force Rubygems to recalculate Gem.paths. Normally this is fine, however if Gem.paths had already been used and cached prior to this mangling `Gem.path` will contain a "stale" value with respect to ENV. (e.g possibly missing the `GEM_HOME`/`Gem.default_dir` value, or retaining values from an old `GEM_PATH`. Clearing the paths is the only way to ensure things are consistent for later usage.

In particular this currently can happen on JRuby 9.4+ because often `Gem.path` is already initialized by the time we boot, due to the use of default gems such as `stringio`. Rather than assume a given state, it would be better to ensure it is consistent by clearing paths whenever we touch `ENV['GEM_PATH']`.
…lse init parameter option

Changes the `false` value to behave the same way as empty, and not touch ENV['GEM_PATH'] or Gem.path at all. Previously this allowed you to alter Gem.path without touching the ENV['GEM_PATH']'. This was unsafe with Bundler 1.6+ (so for a long time), because many things may cause Gem.path to be recalculated from env, including bundler initialisations, restarts and later changes within an application's boot hooks. Furthermore the behaviour varied wildly depending on whether Rubygems was initialised prior to the Rack booter, or afterwards, even without bundler causing indeterminism and issues similar to jruby/warbler#575

I cannot find any reference to use of this value on GitHub, or discussion, and it would not work at all with modern JRuby/bundler so removing it, because it helps avoid further issues. Note that it was added in jruby@2fd826b and then the default changed back in jruby@d3ee8f0 shortly after when issues were discovered, so given it's undocumented/unsupported, it is unlikely to be relied upon by anyone.
On JRuby 9.4 this causes Rubygems initialisation which means realisation of init values. This screws with jruby-rack's own guarantees about env implied by its config values and can cause issues in boot hooks such as jruby/warbler#575 due to Rubygems being in a different state to what they expect.
@chadlwilson chadlwilson added this to the 1.2.6 milestone Oct 10, 2025
@chadlwilson chadlwilson changed the title 1.2: Ensure that Rubygems is not unnecessarily initiailized and Gem.paths are left in consistent state 1.2.x: Ensure that Rubygems is not unnecessarily initiailized and Gem.paths are left in consistent state Oct 10, 2025
@chadlwilson chadlwilson changed the title 1.2.x: Ensure that Rubygems is not unnecessarily initiailized and Gem.paths are left in consistent state 1.2.x: Ensure that Rubygems is not unnecessarily initialized and Gem.paths are left in consistent state Oct 10, 2025
@chadlwilson chadlwilson merged commit 408870e into jruby:1.2-stable Oct 11, 2025
65 checks passed
@chadlwilson chadlwilson deleted the improve-gem-path-consistency branch October 11, 2025 05:40
@chadlwilson chadlwilson changed the title 1.2.x: Ensure that Rubygems is not unnecessarily initialized and Gem.paths are left in consistent state 1.2.x: Ensure that Rubygems is not unnecessarily initialized during boot and Gem.paths are left in consistent state Oct 11, 2025
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.

1 participant