Skip to content

fix(websockets): Initialise io_loop in multi-worker environments #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

quartzar
Copy link

Problem Addressed

This PR addresses the issue where WebSocket connections frequently fail when running a Django application with Bokeh integration using multiple Gunicorn workers. In multi-worker environments, there was only a 1/n chance (where n is the number of workers) that the WebSocket connection would succeed.

Solution

The solution implements a more robust approach to io_loop initialisation by:

  1. Explicitly setting the io_loop using IOLoop.current() if it doesn't exist
  2. Ensuring the io_loop is set before attempting session creation
  3. Adding defensive coding in the disconnect method

This change removes the worker affinity requirement, allowing any worker to successfully handle WebSocket connections regardless of which worker handled the initial HTTP request.

Changes Made

  • Modified WSConsumer.application_context to initialise the io_loop rather than raising an error
  • Ensured proper io_loop initialisation before session creation
  • Misc: Fixed a deprecation and added await to some un-awaited coroutines

Testing

Tested with Gunicorn running with 4 Uvicorn workers, confirming:

  • Before fix: ~25% success rate for Bokeh autoload app loading
  • After fix: 100% success rate for Bokeh autoload app loading

Closes #15

fixes bokeh#15)

This fixes an issue in multi-worker environments where io_loop may be initialised on a worker other than the worker that originally handled the WebSocket connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WebSocket connections fail in multi-worker environments due to missing io_loop initialisation
1 participant