ActionCable raising exception during rspec system spec

Viewed 685

I have a problem using ActionCable with system specs using Capybara. ActionCable is working perfectly on my development server, but when testing with system specs, it is unable to connect to Websockets.

The output from /log/test.log shows:

Started GET "/cable" for 127.0.0.1 at 2017-11-15 18:17:04 +0900
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-11-15 18:17:04 +0900
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: undefined method `write_nonblock' for nil:NilClass
WebSocket error occurred: undefined method `write_nonblock' for nil:NilClass
WebSocket error occurred: undefined method `write_nonblock' for nil:NilClass

I'm using:

  • ruby 2.4.1
  • rails 5.1.4
  • rspec 3.7.0
  • capybara 2.13.0
  • puma 3.10.0
  • selenium-webdriver 3.6.0

I have tried:

Capybara.server = :puma

This made no difference (probably since system specs are already using Puma).

My attempt at debugging the issue:

Looking closer at the error, the method write_nonblock is called on nil, which happens here on line 45:

https://github.com/rails/rails/blob/385825fb705446a1f11e82b65ca97a50d7e67898/actioncable/lib/action_cable/connection/stream.rb#L45

It seems like @rack_hijack_io gets set to nil, which likely happens in the hijack_rack_socket method here:

https://github.com/rails/rails/blob/385825fb705446a1f11e82b65ca97a50d7e67898/actioncable/lib/action_cable/connection/stream.rb#L102

@socket_object.env["rack.hijack_io"] returns nil, so we don't have a "rack.hijack_io" key. So the error seems to be related to the Rack Hijacking API.

1 Answers

I don't know details about your test but in my case, the issue was in config.log_level = :debug in config/environments/[env].

Because ActionCable produces a lot of debugging information about broadcasting. For instance, if you have 100 connected clients your log file will be polluted with 100 messages for each broadcast.

PS: I caught this error on NGINX + Passenger(6.0.4) + GKE with Stackdriver logging.

Related