diff --git a/app/routes/api.py b/app/routes/api.py index 4f19264..bba2837 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -122,28 +122,31 @@ def ticket_stats(): @socketio.on('connect') def on_connect(): - if current_user.is_authenticated: + # current_user can be None when this handler is invoked without a session + # context (e.g. during a socketio.start_background_task emit from an + # APScheduler job). The None guard prevents AttributeError in that path. + if current_user and current_user.is_authenticated: join_room(f'user_{current_user.id}') logger.info(f'[SOCKET CONNECT] user_id={current_user.id}') @socketio.on('disconnect') def on_disconnect(): - if current_user.is_authenticated: + if current_user and current_user.is_authenticated: leave_room(f'user_{current_user.id}') logger.info(f'[SOCKET DISCONNECT] user_id={current_user.id}') @socketio.on('join_ticket') def on_join_ticket(data): - if current_user.is_authenticated: + if current_user and current_user.is_authenticated: ticket_id = data.get('ticket_id') join_room(f'ticket_{ticket_id}') @socketio.on('leave_ticket') def on_leave_ticket(data): - if current_user.is_authenticated: + if current_user and current_user.is_authenticated: ticket_id = data.get('ticket_id') leave_room(f'ticket_{ticket_id}')