Skip to content

Commit

Permalink
Handle callbacks for emits outside of request context (Fixes #1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Nov 17, 2019
1 parent 4fe256f commit 1bd15e0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
sys.exit(1)

import flask
from flask import _request_ctx_stack, json as flask_json
from flask import _request_ctx_stack, has_request_context, json as flask_json
from flask.sessions import SessionMixin
import socketio
from socketio.exceptions import ConnectionRefusedError
Expand Down Expand Up @@ -405,14 +405,21 @@ def ping():
callback = kwargs.pop('callback', None)
if callback:
# wrap the callback so that it sets app app and request contexts
sid = flask.request.sid
sid = None
if has_request_context:
sid = getattr(flask.request, 'sid', None)
original_callback = callback

def _callback_wrapper(*args):
return self._handle_event(original_callback, None, namespace,
sid, *args)

callback = _callback_wrapper
if sid:
# the callback wrapper above will install a request context
# before invoking the original callback
# we only use it if the emit was issued from a Socket.IO
# populated request context (i.e. request.sid is defined)
callback = _callback_wrapper
self.server.emit(event, *args, namespace=namespace, room=room,
skip_sid=skip_sid, callback=callback, **kwargs)

Expand Down

0 comments on commit 1bd15e0

Please sign in to comment.