Skip to content

Commit

Permalink
Raise first error that occurred during coiteration
Browse files Browse the repository at this point in the history
The reactor will by default log exceptions that occur
in the coiterator, resulting in nothing being raised.
Code using the asyncdns lib except certain errors
to occur when something is wrong so this should keep
the current asyncdns interfacing intact, at the cost of
taking longer to fail
  • Loading branch information
stveit committed May 21, 2024
1 parent cf29469 commit 9e1383d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions python/nav/asyncdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ def __init__(self):
)
self.results = defaultdict(list)
self._finished = False
self._errors = []

def resolve(self, names):
"""Resolves DNS names in parallel"""
self._finished = False
self.results = defaultdict(list)
self._finished = False
self._errors = []

def lookup_names():
for name in names:
Expand All @@ -96,7 +98,10 @@ def lookup_names():
coop = task.Cooperator()
work = lookup_names()
deferred_list = defer.DeferredList(
[coop.coiterate(work) for _ in range(BATCH_SIZE)]
[
coop.coiterate(work).addErrback(self._save_error)
for _ in range(BATCH_SIZE)
]
)
deferred_list.addCallback(self._finish)

Expand All @@ -106,6 +111,10 @@ def lookup_names():
# iteration to ensure the resolver library closes its UDP sockets
reactor.iterate()

# raise first error that occurred
for error in self._errors:
raise error

return dict(self.results)

def lookup(self, name):
Expand All @@ -128,6 +137,10 @@ def _errback(failure, host):
"""Errback"""
return host, failure.value

def _save_error(self, failure):
"""Errback"""
self._errors.append(failure.value)

def _finish(self, _):
self._finished = True

Expand Down

0 comments on commit 9e1383d

Please sign in to comment.