From a71d9c33d763d6c9f12a7185885a58c0732e59b8 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 12 Aug 2021 22:15:18 -0700 Subject: [PATCH] fix stupid and dangerous bugs with sleeps not working --- respodns/dns.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/respodns/dns.py b/respodns/dns.py index b83cc6a..60566d6 100644 --- a/respodns/dns.py +++ b/respodns/dns.py @@ -193,17 +193,17 @@ async def try_all_ips(db, try_me, checks, opts: Options, callback=None): from asyncio import create_task, sleep, BoundedSemaphore seen, total = 0, None + sem = BoundedSemaphore(opts.ip_simul) - async def process(ip): + async def _process(ip): nonlocal seen - first = seen == 0 seen += 1 if opts.progress: lament(f"#{seen}: {ip}" if total is None else f"#{seen}/{total}: {ip}") stderr.flush() - if not first: - await sleep(opts.ip_wait) + first_failure = await try_ip(db, ip, checks, opts, callback) + if first_failure is None: print(ip) # all tests for this server passed; pass it along to stdout elif opts.dry: # don't save the error anywhere; pass it along to stdout @@ -214,14 +214,23 @@ async def try_all_ips(db, try_me, checks, opts: Options, callback=None): else: print(ip, ff.reason, ff.kind, ff.domain, sep="\t") - sem = BoundedSemaphore(opts.ip_simul) + async def process(ip): + try: + await _process(ip) + finally: + sem.release() + tasks = [] while (res := await try_me.get()) is not None: - ip, total = res #lament("TRYING", ip) - async with sem: - task = create_task(process(ip)) - tasks.append(task) + if len(tasks) > 0: + await sleep(opts.ip_wait) + ip, total = res + # acquire now instead of within the task so + # a ton of tasks aren't created all at once. + await sem.acquire() + task = create_task(process(ip)) + tasks.append(task) for task in tasks: await task