|
42 | 42 | )
|
43 | 43 | from synapse.logging.context import (
|
44 | 44 | PreserveLoggingContext,
|
45 |
| - current_context, |
46 | 45 | make_deferred_yieldable,
|
47 | 46 | preserve_fn,
|
48 | 47 | run_in_background,
|
@@ -233,8 +232,6 @@ async def _start_key_lookups(self, verify_requests):
|
233 | 232 | """
|
234 | 233 |
|
235 | 234 | try:
|
236 |
| - ctx = current_context() |
237 |
| - |
238 | 235 | # map from server name to a set of outstanding request ids
|
239 | 236 | server_to_request_ids = {}
|
240 | 237 |
|
@@ -265,12 +262,8 @@ def lookup_done(res, verify_request):
|
265 | 262 |
|
266 | 263 | # if there are no more requests for this server, we can drop the lock.
|
267 | 264 | if not server_requests:
|
268 |
| - with PreserveLoggingContext(ctx): |
269 |
| - logger.debug("Releasing key lookup lock on %s", server_name) |
270 |
| - |
271 |
| - # ... but not immediately, as that can cause stack explosions if |
272 |
| - # we get a long queue of lookups. |
273 |
| - self.clock.call_later(0, drop_server_lock, server_name) |
| 265 | + logger.debug("Releasing key lookup lock on %s", server_name) |
| 266 | + drop_server_lock(server_name) |
274 | 267 |
|
275 | 268 | return res
|
276 | 269 |
|
@@ -335,20 +328,32 @@ async def do_iterations():
|
335 | 328 | )
|
336 | 329 |
|
337 | 330 | # look for any requests which weren't satisfied
|
338 |
| - with PreserveLoggingContext(): |
339 |
| - for verify_request in remaining_requests: |
340 |
| - verify_request.key_ready.errback( |
341 |
| - SynapseError( |
342 |
| - 401, |
343 |
| - "No key for %s with ids in %s (min_validity %i)" |
344 |
| - % ( |
345 |
| - verify_request.server_name, |
346 |
| - verify_request.key_ids, |
347 |
| - verify_request.minimum_valid_until_ts, |
348 |
| - ), |
349 |
| - Codes.UNAUTHORIZED, |
350 |
| - ) |
| 331 | + while remaining_requests: |
| 332 | + verify_request = remaining_requests.pop() |
| 333 | + rq_str = ( |
| 334 | + "VerifyJsonRequest(server=%s, key_ids=%s, min_valid=%i)" |
| 335 | + % ( |
| 336 | + verify_request.server_name, |
| 337 | + verify_request.key_ids, |
| 338 | + verify_request.minimum_valid_until_ts, |
351 | 339 | )
|
| 340 | + ) |
| 341 | + |
| 342 | + # If we run the errback immediately, it may cancel our |
| 343 | + # loggingcontext while we are still in it, so instead we |
| 344 | + # schedule it for the next time round the reactor. |
| 345 | + # |
| 346 | + # (this also ensures that we don't get a stack overflow if we |
| 347 | + # has a massive queue of lookups waiting for this server). |
| 348 | + self.clock.call_later( |
| 349 | + 0, |
| 350 | + verify_request.key_ready.errback, |
| 351 | + SynapseError( |
| 352 | + 401, |
| 353 | + "Failed to find any key to satisfy %s" % (rq_str,), |
| 354 | + Codes.UNAUTHORIZED, |
| 355 | + ), |
| 356 | + ) |
352 | 357 | except Exception as err:
|
353 | 358 | # we don't really expect to get here, because any errors should already
|
354 | 359 | # have been caught and logged. But if we do, let's log the error and make
|
@@ -410,10 +415,23 @@ async def _attempt_key_fetches_with_fetcher(self, fetcher, remaining_requests):
|
410 | 415 | # key was not valid at this point
|
411 | 416 | continue
|
412 | 417 |
|
413 |
| - with PreserveLoggingContext(): |
414 |
| - verify_request.key_ready.callback( |
415 |
| - (server_name, key_id, fetch_key_result.verify_key) |
416 |
| - ) |
| 418 | + # we have a valid key for this request. If we run the callback |
| 419 | + # immediately, it may cancel our loggingcontext while we are still in |
| 420 | + # it, so instead we schedule it for the next time round the reactor. |
| 421 | + # |
| 422 | + # (this also ensures that we don't get a stack overflow if we had |
| 423 | + # a massive queue of lookups waiting for this server). |
| 424 | + logger.debug( |
| 425 | + "Found key %s:%s for %s", |
| 426 | + server_name, |
| 427 | + key_id, |
| 428 | + verify_request.request_name, |
| 429 | + ) |
| 430 | + self.clock.call_later( |
| 431 | + 0, |
| 432 | + verify_request.key_ready.callback, |
| 433 | + (server_name, key_id, fetch_key_result.verify_key), |
| 434 | + ) |
417 | 435 | completed.append(verify_request)
|
418 | 436 | break
|
419 | 437 |
|
|
0 commit comments