Repair grievous error that led to lost/duplicated messages. Python's approach to closing-over-mutable-variables sucks.

This commit is contained in:
Tony Garnock-Jones 2022-02-09 23:47:01 +01:00
parent b8ba97742d
commit efa56da614
1 changed files with 5 additions and 1 deletions

View File

@ -109,7 +109,10 @@ def gather_events_from_socket(facet, callback, ip, loop):
facet.log.debug('waiting for event...')
events = ip.get()
facet.log.debug(f'... got {len(events)} events')
turn.external(facet, lambda: callback(events), loop=loop)
# AAARGH python's horrible closure rules wrt mutability bite AGAIN!!!!
def handler_for_specific_events(events):
return lambda: callback(events)
turn.external(facet, handler_for_specific_events(events), loop=loop)
except Exception as e:
facet.log.debug(e)
finally:
@ -172,6 +175,7 @@ def main(args):
facet = turn.active_facet()
loop = find_loop()
facet.log.info('Starting background netlink thread')
threading.Thread(
name='background-netlink-socket-read-thread',
target=lambda: gather_events_from_socket(facet, handle_events, ip, loop)).start()