WebSocket handler and fibers

80 views
Skip to first unread message

Mauro Pompilio

unread,
Feb 24, 2012, 7:35:34 AM2/24/12
to golia...@googlegroups.com
Hi there,

I'm having an issue when using em-http (with em-synchrony) inside a WebSocket handler. I actually found a workaround for it but want to verify if what I'm doing is correct, so, here's the code:
http://pastebin.com/4c2Use8A

The thing is that depending on where the request is issued, I get a "can't shield from root fiber" exception and the app crashes. If I wrap it in a fiber, it works again. I have read that normal HTTP requests in Goliath run in an independent fiber, but not sure about how the WebSockets are handled.

Aside from that, is env['stream.send'].call('some_msg') the only way to send a message over the WS? No syntax sugar for that?


Cheers!
Mauro.

--
Vi, Vi, Vi. The Editor of the Beast!
http://www.vim.org

Ilya Grigorik

unread,
Feb 24, 2012, 6:59:39 PM2/24/12
to golia...@googlegroups.com
Hey Mauro, the behavior you're seeing is "correct".. even if confusing. 

The initial handshake and on_open will happen inside of a fiber, but consequent calls are scheduled later by the reactor (whenever the message comes in). When that happens, the block is called directly, and we're already outside of the context of the original fiber. With that in mind, two things you could do:

(a) write the code inside of those handlers as async
(b) wrap the blocks inside of a fiber.. just as you are at the moment.

ig

P.S. Syntax sugar for send -- there should be something cleaner!
Reply all
Reply to author
Forward
0 new messages