Deferring "require 'rack/websocket'" anytime after the first thin request is fired off triggers the exception "undefined method `backend' for nil:NilClass"
bradgessler opened this issue · 3 comments
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:
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:
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.
Hi Brad,
The problem is that when you run require 'rack/websocket'
after first request then first connection don't set raw connection used in whole websocket-rack to send data and configure connection.
I will try to bypass that in weekend, but if you will have any suggestions then please post them here - it would help a lot :)
I was digging deep into that code nailing down this simple test and came across https://github.com/imanel/websocket-rack/blob/v0.3.1/lib/rack/websocket/extensions/thin/connection.rb#L16 not being in the right spot. I'll look into it as well at some point, but first I'm under the gun to get the push lib I'm working on running for integration tests.
I managed to work-around this issue by more aggressively loading the rack/websocket lib: https://github.com/imanel/websocket-rack/blob/v0.3.1/lib/rack/websocket/extensions/thin/connection.rb#L16
Cheers!
I'm marking it as wontfix
.
In proper code you should do something like that:
require 'rubygems'
class App < Rack::WebSocket::Application
require 'rack/websocket'
def on_open
send_data 'hi'
end
end
run Proc.new{|env|
App.new.call(env)
}
I don't see any reason to write 'require' inside of proc
- if you will write require
anything before new
function it will work.