From 4256aa8a2e842eb9fec99c4bf4cdf704a685c733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20=C3=89vrard?= Date: Wed, 14 May 2025 12:12:54 +0200 Subject: [PATCH] Remove notification events from the queue after the wait times out Closes #14022 (grafted from 23bbeb1772ec7d5e27876e638337d9f2d81bbf19) --HG-- branch : 7.0 --- trytond/trytond/bus.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/trytond/trytond/bus.py b/trytond/trytond/bus.py index 28fde61c3b..faf6e0cb1a 100644 --- a/trytond/trytond/bus.py +++ b/trytond/trytond/bus.py @@ -100,28 +100,34 @@ class LongPollingBus: return cls.create_response(channel, content) event = threading.Event() - for channel in channels: - if channel in cls._queues[pid, database]['events']: - event_channel = cls._queues[pid, database]['events'][channel] - else: + try: + for channel in channels: with cls._queues_lock[pid]: event_channel = cls._queues[pid, database][ 'events'][channel] - event_channel.append(event) + event_channel.append(event) - triggered = event.wait(_long_polling_timeout) - if not triggered: - response = cls.create_response(None, None) - else: - response = cls.create_response( - *cls._messages[database].get_next(channels, last_message)) - - with cls._queues_lock[pid]: - for channel in channels: - events = cls._queues[pid, database]['events'][channel] - for e in events[:]: - if e.is_set(): - events.remove(e) + triggered = event.wait(_long_polling_timeout) + if not triggered: + response = cls.create_response(None, None) + else: + response = cls.create_response( + *cls._messages[database].get_next(channels, last_message)) + finally: + with cls._queues_lock[pid]: + queue_events = cls._queues[pid, database]['events'] + for channel in channels: + if channel not in queue_events: + continue + events = queue_events[channel] + try: + events.remove(event) + except ValueError: + pass + if not events: + # A user could query a lot of channels filling the + # events dictionnary with empty lists + del queue_events[channel] return response