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

Cannot detect if Spring is running #634

Open
zofrex opened this issue Nov 16, 2020 · 3 comments
Open

Cannot detect if Spring is running #634

zofrex opened this issue Nov 16, 2020 · 3 comments

Comments

@zofrex
Copy link

zofrex commented Nov 16, 2020

The docs say:

From within your code, you can check whether Spring is active with if defined?(Spring).

But Spring is defined when running rails s even without Spring.

With this code inside an initializer:

if defined?(Spring)
  puts "Spring is defined (only shown on first run)"
  Spring.after_fork do
    puts "After fork"
  end
else
  puts "Spring is not defined"
end

The output from rails test is:

Spring is defined (only shown on first run)
Running via Spring preloader in process 21343
After fork

Which is as expected. Running DISABLE_SPRING=1 rails test gives:

Spring is not defined

Which is also the expected outcome.

However, running either rails s or DISABLE_SPRING=1 rails s gives the same output:

Spring is defined (only shown on first run)

In short, the documented check for whether Spring is active or not seems to return a false positive for certain Rails commands.

@ajsharp
Copy link

ajsharp commented Apr 27, 2022

I've felt this pain as well. There are a number of scenarios where you need to be able to reliably hook into whether you're in a forked spring process or not in order to properly initializer connections. For example, the ruby grpc client has a check to make sure you don't initialize a connection in a child process when you've already done so in a parent process.

I've seen scenarios where the Spring.after_fork hook doesn't get invoked reliably. Unclear if this is a bug with the hook invocation, or if Spring is present (and defined) but child processes aren't being forked.

@pboling
Copy link

pboling commented Sep 4, 2023

In my Rails 7.0.7.2 app Spring is always loaded, even with DISABLE_SPRING=true, and even with require: false in the Gemfile, and as a result there is no way to detect when commands are running against Spring, or not.

@mckeed
Copy link

mckeed commented Oct 6, 2023

In my case, another gem was loading Spring by doing require "spring/commands" even when not running in Spring. For me, it worked to check defined?(Spring::Application) instead. If you want to be really sure whether you're running in Spring, you could check defined?(Spring::Application) && caller.any? {|c| c.start_with?(Spring.const_source_location('Application')&.first) }

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

No branches or pull requests

4 participants