From ffbe7382f813e979f5b6efae2766fdd9f469145e Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Sat, 19 Nov 2016 20:16:15 +0100 Subject: [PATCH] Async: Fixes problem when callbacks add other callbacks. For context, see http://irclogs.nim-lang.org/19-11-2016.html#19:08:51 --- lib/pure/asyncdispatch.nim | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim index 58028847e5..b93390221d 100644 --- a/lib/pure/asyncdispatch.nim +++ b/lib/pure/asyncdispatch.nim @@ -1061,28 +1061,34 @@ else: # make a copy to iterate over. let currentCBs = data.readCBs data.readCBs = @[] + # Using another sequence because callbacks themselves can add + # other callbacks. + var newCBs: seq[Callback] = @[] for cb in currentCBs: - if data.readCBs.len > 0: + if newCBs.len > 0: # A callback has already returned with EAGAIN, don't call any # others until next `poll`. - data.readCBs.add(cb) + newCBs.add(cb) else: if not cb(data.fd): # Callback wants to be called again. - data.readCBs.add(cb) + newCBs.add(cb) + data.readCBs = newCBs & data.readCBs if EvWrite in info.events or info.events == {EvError}: let currentCBs = data.writeCBs data.writeCBs = @[] + var newCBs: seq[Callback] = @[] for cb in currentCBs: - if data.writeCBs.len > 0: + if newCBs.len > 0: # A callback has already returned with EAGAIN, don't call any # others until next `poll`. - data.writeCBs.add(cb) + newCBs.add(cb) else: if not cb(data.fd): # Callback wants to be called again. - data.writeCBs.add(cb) + newCBs.add(cb) + data.writeCBs = newCBs & data.writeCBs if info.key in p.selector: var newEvents: set[Event]