From 4fdd715bc109416929806776c60b6e181f6787cd Mon Sep 17 00:00:00 2001 From: Brian May Date: Wed, 9 Dec 2015 10:22:28 +1100 Subject: [PATCH] Don't change object while iterating Closes: #40 --- sshuttle/client.py | 11 +++++++++-- sshuttle/server.py | 14 ++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/sshuttle/client.py b/sshuttle/client.py index cf7c387..2724ebf 100644 --- a/sshuttle/client.py +++ b/sshuttle/client.py @@ -277,18 +277,25 @@ udp_by_src = {} def expire_connections(now, mux): + remove = [] for chan, timeout in dnsreqs.items(): if timeout < now: debug3('expiring dnsreqs channel=%d\n' % chan) + remove.append(chan) del mux.channels[chan] - del dnsreqs[chan] + for chan in remove: + del dnsreqs[chan] debug3('Remaining DNS requests: %d\n' % len(dnsreqs)) + + remove = [] for peer, (chan, timeout) in udp_by_src.items(): if timeout < now: debug3('expiring UDP channel channel=%d peer=%r\n' % (chan, peer)) mux.send(chan, ssnet.CMD_UDP_CLOSE, '') + remove.append(peer) del mux.channels[chan] - del udp_by_src[peer] + for peer in remove: + del udp_by_src[peer] debug3('Remaining UDP channels: %d\n' % len(udp_by_src)) diff --git a/sshuttle/server.py b/sshuttle/server.py index ae9639f..85c5599 100644 --- a/sshuttle/server.py +++ b/sshuttle/server.py @@ -328,14 +328,20 @@ def main(latency_control): if dnshandlers: now = time.time() - for channel, h in list(dnshandlers.items()): + remove = [] + for channel, h in dnshandlers.items(): if h.timeout < now or not h.ok: debug3('expiring dnsreqs channel=%d\n' % channel) - del dnshandlers[channel] + remove.append(channel) h.ok = False + for channel in remove: + del dnshandlers[channel] if udphandlers: - for channel, h in list(udphandlers.items()): + remove = [] + for channel, h in udphandlers.items(): if not h.ok: debug3('expiring UDP channel=%d\n' % channel) - del udphandlers[channel] + remove.append(channel) h.ok = False + for channel in remove: + del udphandlers[channel]