1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-05-31 15:49:41 +03:00

plugins/edsm: Protect discarded_events loop against shutdown

This commit is contained in:
Athanasius 2021-08-05 16:40:11 +01:00
parent 64f97accc0
commit 7a71fc8e14
No known key found for this signature in database
GPG Key ID: AE3E527847057C7D

View File

@ -43,6 +43,8 @@ class This:
"""Holds module globals.""" """Holds module globals."""
def __init__(self): def __init__(self):
self.shutting_down = False # Plugin is shutting down.
self.session: requests.Session = requests.Session() self.session: requests.Session = requests.Session()
self.queue: Queue = Queue() # Items to be sent to EDSM by worker thread self.queue: Queue = Queue() # Items to be sent to EDSM by worker thread
self.discarded_events: Set[str] = [] # List discarded events from EDSM self.discarded_events: Set[str] = [] # List discarded events from EDSM
@ -198,7 +200,8 @@ def plugin_stop() -> None:
"""Stop this plugin.""" """Stop this plugin."""
logger.debug('Signalling queue to close...') logger.debug('Signalling queue to close...')
# Signal thread to close and wait for it # Signal thread to close and wait for it
this.queue.put(None) this.shutting_down = True
this.queue.put(None) # Still necessary to get `this.queue.get()` to unblock
this.thread.join() # type: ignore this.thread.join() # type: ignore
this.thread = None this.thread = None
this.session.close() this.session.close()
@ -610,6 +613,10 @@ def worker() -> None: # noqa: CCR001 C901 # Cant be broken up currently
entry: Mapping[str, Any] = {} entry: Mapping[str, Any] = {}
while not this.discarded_events: while not this.discarded_events:
if this.shutting_down:
logger.debug(f'returning from discarded_events loop due to {this.shutting_down=}')
return
get_discarded_events_list() get_discarded_events_list()
if this.discarded_events: if this.discarded_events:
break break
@ -618,16 +625,20 @@ def worker() -> None: # noqa: CCR001 C901 # Cant be broken up currently
logger.debug('Got "events to discard" list, commencing queue consumption...') logger.debug('Got "events to discard" list, commencing queue consumption...')
while True: while True:
if this.shutting_down:
logger.debug(f'{this.shutting_down=}, so setting closing = True')
closing = True
item: Optional[Tuple[str, Mapping[str, Any]]] = this.queue.get() item: Optional[Tuple[str, Mapping[str, Any]]] = this.queue.get()
if item: if item:
(cmdr, entry) = item (cmdr, entry) = item
if 'edsm-cmdr-events' in trace_on:
logger.trace(f'De-queued ({cmdr=}, {entry["event"]=})')
else: else:
logger.debug('Empty queue message, setting closing = True') logger.debug('Empty queue message, setting closing = True')
closing = True # Try to send any unsent events before we close closing = True # Try to send any unsent events before we close
entry = {'event': 'ShutDown'} # Dummy to allow for `entry['event']` below
if 'edsm-cmdr-events' in trace_on:
logger.trace(f'De-queued ({cmdr=}, {entry["event"]=})')
retrying = 0 retrying = 0
while retrying < 3: while retrying < 3: