Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit a7d83d3

Browse files
committed
Fix 'Unhandled error in Deferred'
Fixes a CRITICAL "Unhandled error in Deferred" log message which happened when a function wrapped with `@cachedList` failed
1 parent b43c3ef commit a7d83d3

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

changelog.d/12089.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix occasional 'Unhandled error in Deferred' error message.

synapse/util/caches/descriptors.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,9 @@ def arg_to_cache_key(arg: Hashable) -> Hashable:
444444
deferred: "defer.Deferred[Any]" = defer.Deferred()
445445
deferreds_map[arg] = deferred
446446
key = arg_to_cache_key(arg)
447-
cache.set(key, deferred, callback=invalidate_callback)
447+
cached_defers.append(
448+
cache.set(key, deferred, callback=invalidate_callback)
449+
)
448450

449451
def complete_all(res: Dict[Hashable, Any]) -> None:
450452
# the wrapped function has completed. It returns a
@@ -455,28 +457,23 @@ def complete_all(res: Dict[Hashable, Any]) -> None:
455457
deferreds_map[e].callback(val)
456458
results[e] = val
457459

458-
def errback(f: Failure) -> Failure:
459-
# the wrapped function has failed. Invalidate any cache
460-
# entries we're supposed to be populating, and fail
461-
# their deferreds.
462-
for e in missing:
463-
key = arg_to_cache_key(e)
464-
cache.invalidate(key)
465-
deferreds_map[e].errback(f)
466-
467-
# return the failure, to propagate to our caller.
468-
return f
460+
def errback_all(f: Failure) -> None:
461+
# the wrapped function has failed. Propagate the failure into
462+
# the cache, which will invalidate the entry, and cause the
463+
# relevant cached_deferreds to fail, which will propagate the
464+
# failure to our caller.
465+
for d1 in deferreds_map.values():
466+
d1.errback(f)
469467

470468
args_to_call = dict(arg_dict)
471469
# copy the missing set before sending it to the callee, to guard against
472470
# modification.
473471
args_to_call[self.list_name] = tuple(missing)
474472

475-
cached_defers.append(
476-
defer.maybeDeferred(
477-
preserve_fn(self.orig), **args_to_call
478-
).addCallbacks(complete_all, errback)
479-
)
473+
# dispatch the call, and attach the two handlers
474+
defer.maybeDeferred(
475+
preserve_fn(self.orig), **args_to_call
476+
).addCallbacks(complete_all, errback_all)
480477

481478
if cached_defers:
482479
d = defer.gatherResults(cached_defers, consumeErrors=True).addCallbacks(

0 commit comments

Comments
 (0)