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

Commit

Permalink
Log looping call exceptions
Browse files Browse the repository at this point in the history
If a looping call function errors, then it kills the loop entirely.
Currently it throws away the exception logs, so we should make it
actually log them.

Fixes #3929
  • Loading branch information
erikjohnston committed Oct 5, 2018
1 parent c6dbd21 commit f7199e8
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion synapse/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
import logging
from itertools import islice

Expand Down Expand Up @@ -66,7 +67,7 @@ def looping_call(self, f, msec):
f(function): The function to call repeatedly.
msec(float): How long to wait between calls in milliseconds.
"""
call = task.LoopingCall(f)
call = task.LoopingCall(_log_exception_wrapper(f))
call.clock = self._reactor
call.start(msec / 1000.0, now=False)
return call
Expand Down Expand Up @@ -109,3 +110,19 @@ def batch_iter(iterable, size):
sourceiter = iter(iterable)
# call islice until it returns an empty tuple
return iter(lambda: tuple(islice(sourceiter, size)), ())


def _log_exception_wrapper(f):
"""Used to wrap looping calls to log loudly if they get killed
"""

@functools.wraps(f)
def wrap(*args, **kwargs):
try:
logger.info("Running looping call")
return f(*args, **kwargs)
except: # noqa: E722, as we reraise the exception this is fine.
logger.exception("Looping called died")
raise

return wrap

0 comments on commit f7199e8

Please sign in to comment.