You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently had a problem in my application where my code was nearly exactly like that in the docs for flask_socketio.disconnect:
@socketio.on('admin')defadmin(msg):
ifnotcurrent_user.admin:
disconnect()
# admin functionality below here
I found out the hard way that disconnect() does not stop execution of the current function when used as a guard clause without an else, allowing for admin functionality to be accessed even after the disconnect(). I assumed (maybe a bit harshly) that it behaved similarly to flask.abort, where an exception is raised and execution of the current request stops dead in its tracks.
Obviously, the docs do not allude to an exception being thrown, but it was not exactly clear whether "terminates the connection with the client" means that the processing of the event stops. I also believe that the example code is misleading, as it succumbs to the same issue:
@socketio.on('message')defreceive_message(msg):
ifis_banned(session['username']):
disconnect()
# ...# Doesn't matter if you're banned, because this code below is still going to run
So, I am suggesting that the documentation (especially the example) be modified to be more clear that disconnect() cannot be relied on in this way. At a minimum, the example could be modified to include an else or return statement on the line following the disconnect. Perhaps the docstring could have a quick note added to it as well.
Thank you!
The text was updated successfully, but these errors were encountered:
Yeah, you are right, that example is misleading, there should be an else after the disconnect, just like in the decorator example. Thanks, I'll fix it.
I recently had a problem in my application where my code was nearly exactly like that in the docs for
flask_socketio.disconnect
:I found out the hard way that
disconnect()
does not stop execution of the current function when used as a guard clause without an else, allowing for admin functionality to be accessed even after thedisconnect()
. I assumed (maybe a bit harshly) that it behaved similarly to flask.abort, where an exception is raised and execution of the current request stops dead in its tracks.Obviously, the docs do not allude to an exception being thrown, but it was not exactly clear whether "terminates the connection with the client" means that the processing of the event stops. I also believe that the example code is misleading, as it succumbs to the same issue:
So, I am suggesting that the documentation (especially the example) be modified to be more clear that
disconnect()
cannot be relied on in this way. At a minimum, the example could be modified to include anelse
orreturn
statement on the line following the disconnect. Perhaps the docstring could have a quick note added to it as well.Thank you!
The text was updated successfully, but these errors were encountered: