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

Commit

Permalink
Fix incorrect juggling of logging contexts in _PerHostRatelimiter
Browse files Browse the repository at this point in the history
Introduced in #13499.

Signed-off-by: Sean Quah <seanq@matrix.org>
  • Loading branch information
Sean Quah committed Aug 18, 2022
1 parent 84169a8 commit a30c01b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.d/13554.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Instrument `FederationStateIdsServlet` (`/state_ids`) for understandable traces in Jaeger.
19 changes: 14 additions & 5 deletions synapse/util/ratelimitutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,29 @@ def on_both(r: object) -> object:
# Ensure that we've properly cleaned up.
self.sleeping_requests.discard(request_id)
self.ready_request_queue.pop(request_id, None)
wait_span_scope.__exit__(None, None, None)
wait_timer_cm.__exit__(None, None, None)
return r

ret_defer.addCallbacks(on_start, on_err)
ret_defer.addBoth(on_both)

# Tracing
wait_span_scope = start_active_span("ratelimit wait")
wait_span_scope.__enter__()

# Metrics
wait_timer_cm = queue_wait_timer.time()
wait_timer_cm.__enter__()

ret_defer.addCallbacks(on_start, on_err)
ret_defer.addBoth(on_both)
return make_deferred_yieldable(ret_defer)
# The current logcontext is now one created by `wait_span_scope`.
# We must only `__exit__` it once it has been restored by `make_deferred_yieldable`.
def on_after_logcontext_restored(r: object) -> object:
wait_timer_cm.__exit__(None, None, None)
wait_span_scope.__exit__(None, None, None)
return r

d = make_deferred_yieldable(ret_defer)
d.addBoth(on_after_logcontext_restored)
return d

def _on_exit(self, request_id: object) -> None:
logger.debug("Ratelimit(%s) [%s]: Processed req", self.host, id(request_id))
Expand Down

0 comments on commit a30c01b

Please sign in to comment.