crossbario / crossbar

Crossbar.io - WAMP application router
https://crossbar.io/
Other
2.05k stars 274 forks source link

Concurrent calls are handled incorrectly #1145

Closed koxu1996 closed 7 years ago

koxu1996 commented 7 years ago

Description

I wanted to see how Crossbar deal with concurrent calls, so I added sleep(1) to my function and called it few times. I expected to get all responses after 1 second or one response every second in case of sequential execution. But actual behaviour astonished me: I get first response after 1s but other responses comes together after several seconds: issue_php I thought it can be Thruway issue, but using AutobahnPy I get same result: issue_py

Version:

Crossbar.io        : 17.6.1-3 (Crossbar.io COMMUNITY)                                                
Autobahn           : 17.6.2 (with JSON, MessagePack, CBOR, UBJSON)                                   
Twisted            : 17.5.0-EPollReactor          
LMDB               : 0.92/lmdb-0.9.18             
Python             : 2.7.9/CPython                
OS                 : Linux-4.4.0-83-generic-x86_64-with-debian-8.8                                   
Machine            : x86_64                       
Release key        : RWTAY0Umy44F3jAVWG+tEYunifD02Kmn4Pxc2cHUMEd2sckt/llYhRXp

Steps to reproduce the issue

  1. Download issue.zip and unpack
  2. Run composer install and crossbar start
  3. Open index_php.html or index_py.html
oberstet commented 7 years ago

Use autobahn.twisted.util.sleep instead of the Python standard library time.sleep - the latter is blocking the reactor, the former not.

koxu1996 commented 7 years ago

@oberstet So non-async code won't be executed concurrently, because workers are using only single threads?

oberstet commented 7 years ago

To run code concurrently in the sense of utilizing multiple cores (parallel), you must use threads or processes.

To run blocking code, that code must be run on a background thread, because you otherwise this blocks the event loop.

You can run multiple threads with an event loop on each - which you then must not block.

These statements are true in general (not specific to Crossbar.io, Autobahn, Twisted or even Python).

koxu1996 commented 7 years ago

@oberstet Ok, I understand. But in case of blocking code, could you explain weird behaviour presented on gif? Why reactor return value of first call, then execute rest calls and finally return values instead of returning after each execution?

koxu1996 commented 7 years ago

Sometimes there are 4 response after 4 sec. and last after 1 sec. more: ezgif-4-774105f0db

meejah commented 7 years ago

While you're sleep-ing, the reactor can't do anything (including receive or send data that's ready). I'm not sure exactly what happens after that, but blocking the reactor in this way will definitely lead to bad/weird behavior, as you've discovered ;)