Not Handling Connections to Multiple Worker Processes Correctly
See original GitHub issueif you just change the example qnotify.py
script (or indeed any other) to run on two worker processes as follows:
jp.justpy(notify_test, start_server=False)
app = jp.app
if __name__ == '__main__':
import uvicorn
uvicorn.run('qnotify:app',host='127.0.0.1',port=80,workers=2,use_colors=False)
keep refreshing the page and hitting one of the arrow buttons. Eventually the buttons will stop working and you will get the error message on every button click:
WARNING justpy: No page to load
After a Ctrl+C in the stack trace you will see:
File "c:\users\dima\anaconda3\lib\site-packages\justpy\justpy.py", line 279, in on_disconnect
await WebPage.instances[pid].on_disconnect(websocket) # Run the specific page disconnect function
KeyError: 0
I believe this stack trace actually points to the issue where the page stops working, even though you don’t normally get to see it when it occurs. I’ve only come to this conclusion after running the script via pm2
, which opens separate command line windows for each forked worker process.
Whenever the workers load balance a page load, so that the page http request goes to one worker and the websocket connection to the other (easy to see in separate shell windows), that’s when the exception occurs and the page stops working.
Maybe it’s an implementation issue with Uvicorn, but it is justpy that’s throwing the exception.
A refresh will usually still get the page back.
Btw, the KeyError continues to increment if the same issue re-occurs.
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (5 by maintainers)
Top GitHub Comments
This was very helpful, much appreciated!
Only tested it with two identical app instances running on different ports, and works like a charm. Both sections you mentioned were indeed required for websockets to work.
My config below (on nginx-1.21.6). Note this is running on an internal network, not facing the www.
No problems. Below is a minimal example of the config you’d need for nginx. Then you’d run JustPy instances on ports 10001 and 10002 over https. You can give it the same key and crt files as for nginx.
The bulk of the SSL config probably isn’t “necessary”, but you might as well follow what according to my research is close to best practice. I’ve omitted a lot of other things I mentioned above, eg nginx serving static files - there’s about a million config settings for nginx and it would just confuse the issue to post a full config here.
Feel free to play around with this. My memory is a bit hazy on what some of the settings do exactly.