imanel/websocket-rack — Issue #5: Deferring "require 'rack/websocket'" anytime after the first thin request is fired off triggers the exception "undefined method `backend' for nil:NilClass"
Author: bradgessler
Created: 2011-12-09 23:52:34+00:00
State: open

Deferring "require 'rack/websocket'" anytime after the first thin request is fired off triggers the exception "undefined method `backend' for nil:NilClass"

Given the following code:

```ruby
require 'rubygems'

run Proc.new{|env|
require 'rack/websocket'

class App < Rack::WebSocket::Application
def on_open
send_data 'hi'
end
end

App.new.call(env)
}
```

Then fire off 2 curl requests:

```sh
websocket-test$ thin -R config.ru -p 9000 start &
[1] 83929
websocket-test$ >> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9000, CTRL+C to stop
curl localhost:9000
!! Unexpected error while processing request: undefined method `backend' for nil:NilClass
curl: (52) Empty reply from server
websocket-test$ curl localhost:9000
WARNING: You are using old EventMachine version. Please consider updating to EM version >= 1.0.0 or running Thin using thin-websocket.
!! Unexpected error while processing request: Connection and Upgrade headers required
curl: (52) Empty reply from server
```

The initial request triggers an error, "undefined method `backend' for nil:NilClass", but all subsequent requests are fine.

Yes, the config.ru rackup file is whacky, but its designed to represent the simplest possible use-case of this problem when the requirement of 'rack/websocket' is deferred until after initial thin requests. I'm trying to incorporate rack/websocket with a gem library at https://github.com/polleverywhere/push/tree/backends and I'm running into the problem.