【问题标题】:Cannot start local server with Rails via WSL无法通过 WSL 使用 Rails 启动本地服务器
【发布时间】:2020-01-06 12:27:41
【问题描述】:

我尝试更新我的 Rails 应用程序,但我认为问题出在某个地方,因为我无法启动服务器。我已经尝试切换 Ruby-Versions 并降级 Rails,但在某个地方我卡住了并不断收到相同的错误。

.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb:233:in `setsockopt': 无效参数 - setsockopt(2) (Errno::EINVAL)

我相信问题来了:日志的最后一行显示了 Puma 的问题。

this Gist you can find my Gemfile and Gemfile.lock

    rails s
    => Booting Puma
    => Rails 6.0.2.1 application starting in development
    => Run `rails server --help` for more startup options
    Puma starting in single mode...
    * Version 4.3.1 (ruby 2.6.3-p62), codename: Mysterious Traveller
    * Min threads: 5, max threads: 5
    * Environment: development
    Exiting
    Traceback (most recent call last):
            31: from bin/rails:4:in `<main>'
            30: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `require'
            29: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:291:in `load_dependency'
            28: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `block in require'
            27: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
            26: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
            25: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
            24: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
            23: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
            22: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/railties-6.0.2.1/lib/rails/commands.rb:18:in `<main>'
            21: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/railties-6.0.2.1/lib/rails/command.rb:46:in `invoke'
            20: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/railties-6.0.2.1/lib/rails/command/base.rb:69:in `perform'
            19: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
            18: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
            17: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
            16: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/railties-6.0.2.1/lib/rails/commands/server/server_command.rb:138:in `perform'
            15: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/railties-6.0.2.1/lib/rails/commands/server/server_command.rb:138:in `tap'
            14: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/railties-6.0.2.1/lib/rails/commands/server/server_command.rb:147:in `block in perform'
            13: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/railties-6.0.2.1/lib/rails/commands/server/server_command.rb:39:in `start'
            12: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/rack-2.0.8/lib/rack/server.rb:297:in `start'
            11: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/rack/handler/puma.rb:73:in `run'
            10: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/launcher.rb:172:in `run'
             9: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/single.rb:98:in `run'
             8: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/runner.rb:161:in `load_and_bind'
             7: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb:90:in `parse'
             6: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb:90:in `each'
             5: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb:106:in `block in parse'
             4: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb:222:in `add_tcp_listener'
             3: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb:222:in `each'
             2: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb:223:in `block in add_tcp_listener'
             1: from /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb:233:in `add_tcp_listener'
    /home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb:233:in `setsockopt': Invalid argument - setsockopt(2) (Errno::EINVAL)

提前感谢您的帮助!丹尼

【问题讨论】:

  • 你在运行 WSL 吗?我在 WSL 上运行 rails 时遇到同样的错误 :(
  • 没错,我当然应该提到。我正在使用 wsl。

标签: ruby-on-rails server windows-subsystem-for-linux puma


【解决方案1】:

找到了临时解决方案。 示例如下: 在文件/home/%username%/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/binder.rb 或在你的情况下/home/chocolope/.rvm/gems/ruby-2.6.3/gems/puma-4.3.1/lib/puma/binder.rb 第 233 行附近:

   #
    def add_tcp_listener(host, port, optimize_for_latency=true, backlog=1024)
      if host == "localhost"
        loopback_addresses.each do |addr|
          add_tcp_listener addr, port, optimize_for_latency, backlog
        end
        return
      end

      host = host[1..-2] if host and host[0..0] == '['
      # monkey patch starts here
      host = '127.0.0.1'
      port =  3000
      # monkey patch ends here
      s = TCPServer.new(host, port)
#      if optimize_for_latency
        s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
 #     end
  #    s.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
      s.listen backlog
      @connected_port = s.addr[1]

      @ios << s
      s
    end

现在一切正常,但我们仍然需要正确的方法来解决这个问题

这是一个常见的 WSL 问题:https://github.com/microsoft/WSL/issues/1419

【讨论】:

    【解决方案2】:

    我遇到了类似的问题

    WSL:1,Ruby:2.5.0,Rails:4.2.1,Puma:3.8.0

    我的 rails s 正在工作,当我发送 Get 请求时出现以下错误:

    Read error: #<Errno::ENOPROTOOPT: Protocol not available - getsockopt(2)>
    /home/abr/.rvm/gems/ruby-2.5.0@helpy/gems/puma-3.8.1/lib/puma/server.rb:124:in `getsockopt'
    /home/abr/.rvm/gems/ruby-2.5.0@helpy/gems/puma-3.8.1/lib/puma/server.rb:124:in `closed_socket?'
    /home/abr/.rvm/gems/ruby-2.5.0@helpy/gems/puma-3.8.1/lib/puma/server.rb:563:in `handle_request'
    /home/abr/.rvm/gems/ruby-2.5.0@helpy/gems/puma-3.8.1/lib/puma/server.rb:425:in `process_client'
    /home/abr/.rvm/gems/ruby-2.5.0@helpy/gems/puma-3.8.1/lib/puma/server.rb:289:in `block in run'
    /home/abr/.rvm/gems/ruby-2.5.0@helpy/gems/puma-3.8.1/lib/puma/thread_pool.rb:120:in `block in spawn_thread'
    

    即使我尝试了上述解决方案,我也为解决该问题付出了很多努力,但这也没有用。

    最后,我找到了一个简单的解决方案,我需要将我的 Pum 从 3.8 更新到 3.12+,它非常适合我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-03
      • 2023-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 2011-06-19
      相关资源
      最近更新 更多