Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Matrix
Commits
d7275eec
Commit
d7275eec
authored
6 years ago
by
Richard van der Hoff
Browse files
Options
Downloads
Patches
Plain Diff
Add a sleep to the Limiter to fix stack overflows.
Fixes #3570
parent
7044af32
No related branches found
Branches containing commit
No related tags found
Tags containing commit
Loading
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
synapse/util/async.py
+20
-3
20 additions, 3 deletions
synapse/util/async.py
tests/util/test_limiter.py
+4
-4
4 additions, 4 deletions
tests/util/test_limiter.py
with
24 additions
and
7 deletions
synapse/util/async.py
+
20
−
3
View file @
d7275eec
...
...
@@ -248,11 +248,15 @@ class Limiter(object):
# do some work.
"""
def
__init__
(
self
,
max_count
):
def
__init__
(
self
,
max_count
,
clock
=
None
):
"""
Args:
max_count(int): The maximum number of concurrent access
"""
if
not
clock
:
from
twisted.internet
import
reactor
clock
=
Clock
(
reactor
)
self
.
_clock
=
clock
self
.
max_count
=
max_count
# key_to_defer is a map from the key to a 2 element list where
...
...
@@ -277,10 +281,23 @@ class Limiter(object):
with
PreserveLoggingContext
():
yield
new_defer
logger
.
info
(
"
Acquired limiter lock for key %r
"
,
key
)
entry
[
0
]
+=
1
# if the code holding the lock completes synchronously, then it
# will recursively run the next claimant on the list. That can
# relatively rapidly lead to stack exhaustion. This is essentially
# the same problem as http://twistedmatrix.com/trac/ticket/9304.
#
# In order to break the cycle, we add a cheeky sleep(0) here to
# ensure that we fall back to the reactor between each iteration.
#
# (This needs to happen while we hold the lock, and the context manager's exit
# code must be synchronous, so this is the only sensible place.)
yield
self
.
_clock
.
sleep
(
0
)
else
:
logger
.
info
(
"
Acquired uncontended limiter lock for key %r
"
,
key
)
entry
[
0
]
+=
1
entry
[
0
]
+=
1
@contextmanager
def
_ctx_manager
():
...
...
This diff is collapsed.
Click to expand it.
tests/util/test_limiter.py
+
4
−
4
View file @
d7275eec
...
...
@@ -48,21 +48,21 @@ class LimiterTestCase(unittest.TestCase):
self
.
assertFalse
(
d4
.
called
)
self
.
assertFalse
(
d5
.
called
)
self
.
assertTrue
(
d4
.
called
)
cm4
=
yield
d4
self
.
assertFalse
(
d5
.
called
)
with
cm3
:
self
.
assertFalse
(
d5
.
called
)
self
.
assertTrue
(
d5
.
called
)
cm5
=
yield
d5
with
cm2
:
pass
with
(
yield
d4
)
:
with
cm4
:
pass
with
(
yield
d5
)
:
with
cm5
:
pass
d6
=
limiter
.
queue
(
key
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment