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

Commit

Permalink
Fix stack overflow in _PerHostRatelimiter due to synchronous requests
Browse files Browse the repository at this point in the history
When there are many synchronous requests waiting on a
`_PerHostRatelimiter`, each request will be started recursively just
after the previous request has completed. Under the right conditions,
this leads to stack exhaustion.

A common way for requests to become synchronous is when the remote
client disconnects early, because the homeserver is overloaded and slow
to respond.

Avoid stack exhaustion under these conditions by deferring subsequent
requests until the next reactor tick.

Fixes #14480.

Signed-off-by: Sean Quah <[email protected]>
  • Loading branch information
Sean Quah committed Jan 10, 2023
1 parent 19151e1 commit 5e4e918
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
26 changes: 18 additions & 8 deletions synapse/util/ratelimitutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,22 @@ def on_both(r: object) -> object:

def _on_exit(self, request_id: object) -> None:
logger.debug("Ratelimit(%s) [%s]: Processed req", self.host, id(request_id))
self.current_processing.discard(request_id)
try:
# start processing the next item on the queue.
_, deferred = self.ready_request_queue.popitem(last=False)

with PreserveLoggingContext():
deferred.callback(None)
except KeyError:
pass
# When requests complete synchronously, we will recursively start the next
# request in the queue. To avoid stack exhaustion, we defer starting the next
# request until the next reactor tick.

def start_next_request() -> None:
# We only remove the completed request from the list when we're about to
# start the next one, otherwise we can admit extra requests.
self.current_processing.discard(request_id)
try:
# start processing the next item on the queue.
_, deferred = self.ready_request_queue.popitem(last=False)

with PreserveLoggingContext():
deferred.callback(None)
except KeyError:
pass

self.reactor.callFromThread(start_next_request)
2 changes: 2 additions & 0 deletions tests/util/test_ratelimitutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_concurrent_limit(self) -> None:

# ... until we complete an earlier request
cm2.__exit__(None, None, None)
reactor.advance(0.0)
self.successResultOf(d3)

def test_sleep_limit(self) -> None:
Expand Down Expand Up @@ -116,6 +117,7 @@ async def task() -> None:
# If a stack overflow occurs, the final task will not complete.

# Wait for all the things to complete.
reactor.advance(0.0)
self.successResultOf(last_task)


Expand Down

0 comments on commit 5e4e918

Please sign in to comment.