ChatGPT解决这个技术问题 Extra ChatGPT

Ruby on Rails console is hanging when loading

For whatever reason, the Ruby on Rails console refuses to start; it just hangs. I haven't made any changes to my code, and other projects using the same version of Ruby and Ruby on Rails have no issue. When I finally Ctrl + C I get this stack trace, which points to Spring.

I can't explain why this is happening from one moment to the next, where it was working fine. I have cleared out all the gems through RVM and reinstalled all through bundle command, but still no luck. Any ideas would be appreciated.

Also the Ruby on Rails server has no issue so as far as I can tell. The issue is project related, however no code has changed, and it's only the Ruby on Rails console that has the issue.

Ruby 2.1.2 Rails 4.1.4

user_a@ubuntu:~/work/app_a$ rails console
^C/home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/run.rb:54:in `gets': Interrupt
    from /home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/run.rb:54:in `verify_server_version'
    from /home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/run.rb:25:in `call'
    from /home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call'
    from /home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/rails.rb:23:in `call'
    from /home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call'
    from /home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/client.rb:26:in `run'
    from /home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/bin/spring:48:in `<top (required)>'
    from /home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `load'
    from /home/user_a/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `<top (required)>'
    from /home/user_a/work/app_a/bin/spring:16:in `require'
    from /home/user_a/work/app_a/bin/spring:16:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

user_a@ubuntu:~/work/app_a$

P
Peter Mortensen

Restarting Spring should fix the hanging commands:

$ bin/spring stop

I experienced hanging commands (rake, bin/rails, etc.) after deleting and recreating a new Ruby on Rails application. Google wasn't that helpful. I hope this is.

Spring will start automatically when you re-run your command.


This worked for me with Rails 4.1.1. I need to read up on Spring gem to see what it's doing.
@cee-dub but what is reason behind it, why spring is not letting it running rails console?
This did not work for me. I am still experiencing issue after stopping spring.
just ran into this issue: when i ran bin/spring stop, I received the response Spring is not running. So, I ran ps aux | grep spring, saw 5 spring processes running, and manually killed them, which fixed the problem.
Amazing! Would never have guessed that.
P
Peter Mortensen

When it is suspected that Spring is the cause of weirdness, try to run this command:

spring stop && spring start

P
Peter Mortensen

It's something wrong with the Spring gem version I guess.

Go to your Gemfile and comment gem 'spring'. Then run bundle install and try again.

# gem 'spring'

And then:

bundle install

If your work depends on the gem, try update the gems by:

bundle update

This solved it for me too on Linux Mint with rails 4.1.4
be wary of bundle update. If you do it that way you have the chance to radically change your gem versions across the entire environment. This can introduce a lot of side effects you don't want. You can also do bundle update spring to update just the one gem
@jaydel You should always put your versions in your gemfile, including path version. Bundler will automatically update minor versions but not major versions, but you can roll back to the version you originally specified (which you know your app works in) at any time.
concerning putting versions in your Gemfile--agreed. I didn't know about the restriction on updating to major versions but it makes sense when I think it through :) Good information, thanks
l
littleforest

If $ bin/spring stop doesn't solve the issue, then check to make sure there isn't an orphaned Spring process still hanging around:

$ ps aux | grep -i spring

If you see something like

user  7163  0.0  0.0 110356  2165 pts/3    S+   19:40   0:00 grep --color=auto -i spring
user 16980  0.0  0.4 398826 17580 ?        Sl   Aug31   0:00 spring server | current | started 277 hours ago     

then kill the errant spring process and try to start the console again:

$ kill -9 16980 
$ rails c

O
Olivier B.

Changing the following method in the lib/application.rb file of the spring gem (path can be shown with the gem info spring command) like this does the trick:

def eager_preload
      with_pty do
        # we can't see stderr and there could be issues when it's overflown
        # see https://github.com/rails/spring/issues/396
        STDERR.reopen("/dev/null")
        preload
      end
end

see https://github.com/markiz/spring/commit/5f3ab734fc45d541aaaccb05b59cd95aa49fe8ef