From 1e2cd8ec7662f0cae156aa54f77af2cb659877c9 Mon Sep 17 00:00:00 2001 From: Joseph Ryan Date: Tue, 17 Jan 2017 16:20:08 -0800 Subject: [PATCH] Fix for session shenanigans with WebsocketDemultiplexer (#486) * Fix for session shenanigans with WebsocketDemultiplexer Session data was getting lost in the demux due to the session getting saved after only the first connect/disconnect consumer was run. * fix for flake8 * flake8 again flake8 again --- channels/sessions.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/channels/sessions.py b/channels/sessions.py index 1856339..cad6791 100644 --- a/channels/sessions.py +++ b/channels/sessions.py @@ -42,7 +42,13 @@ def channel_session(func): def inner(message, *args, **kwargs): # Make sure there's NOT a channel_session already if hasattr(message, "channel_session"): - return func(message, *args, **kwargs) + try: + return func(message, *args, **kwargs) + finally: + # Persist session if needed + if message.channel_session.modified: + message.channel_session.save() + # Make sure there's a reply_channel if not message.reply_channel: raise ValueError( @@ -155,7 +161,13 @@ def http_session(func): def inner(message, *args, **kwargs): # Make sure there's NOT a http_session already if hasattr(message, "http_session"): - return func(message, *args, **kwargs) + try: + return func(message, *args, **kwargs) + finally: + # Persist session if needed (won't be saved if error happens) + if message.http_session is not None and message.http_session.modified: + message.http_session.save() + try: # We want to parse the WebSocket (or similar HTTP-lite) message # to get cookies and GET, but we need to add in a few things that