Skip to content

Commit 4fdd715

Browse files
committed
Don't change object while iterating
Closes: apenwarr#40
1 parent bea723c commit 4fdd715

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

sshuttle/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,25 @@ def done(self):
277277

278278

279279
def expire_connections(now, mux):
280+
remove = []
280281
for chan, timeout in dnsreqs.items():
281282
if timeout < now:
282283
debug3('expiring dnsreqs channel=%d\n' % chan)
284+
remove.append(chan)
283285
del mux.channels[chan]
284-
del dnsreqs[chan]
286+
for chan in remove:
287+
del dnsreqs[chan]
285288
debug3('Remaining DNS requests: %d\n' % len(dnsreqs))
289+
290+
remove = []
286291
for peer, (chan, timeout) in udp_by_src.items():
287292
if timeout < now:
288293
debug3('expiring UDP channel channel=%d peer=%r\n' % (chan, peer))
289294
mux.send(chan, ssnet.CMD_UDP_CLOSE, '')
295+
remove.append(peer)
290296
del mux.channels[chan]
291-
del udp_by_src[peer]
297+
for peer in remove:
298+
del udp_by_src[peer]
292299
debug3('Remaining UDP channels: %d\n' % len(udp_by_src))
293300

294301

sshuttle/server.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,20 @@ def udp_open(channel, data):
328328

329329
if dnshandlers:
330330
now = time.time()
331-
for channel, h in list(dnshandlers.items()):
331+
remove = []
332+
for channel, h in dnshandlers.items():
332333
if h.timeout < now or not h.ok:
333334
debug3('expiring dnsreqs channel=%d\n' % channel)
334-
del dnshandlers[channel]
335+
remove.append(channel)
335336
h.ok = False
337+
for channel in remove:
338+
del dnshandlers[channel]
336339
if udphandlers:
337-
for channel, h in list(udphandlers.items()):
340+
remove = []
341+
for channel, h in udphandlers.items():
338342
if not h.ok:
339343
debug3('expiring UDP channel=%d\n' % channel)
340-
del udphandlers[channel]
344+
remove.append(channel)
341345
h.ok = False
346+
for channel in remove:
347+
del udphandlers[channel]

0 commit comments

Comments
 (0)