Add coments

This commit is contained in:
Erik Johnston 2019-10-10 11:53:57 +01:00
parent 9970f955ce
commit 791a8c559b

View File

@ -107,6 +107,19 @@ def do_patch():
def _check_yield_points(f, changes, start_context): def _check_yield_points(f, changes, start_context):
"""Wraps a generator that is about to be passed to defer.inlineCallbacks """Wraps a generator that is about to be passed to defer.inlineCallbacks
checking that after every yield the log contexts are correct. checking that after every yield the log contexts are correct.
Its perfectly valid for log contexts to change within a function, e.g. due
to new Measure blocks, so such changes are added to the given `changes`
list instead of triggering an exception.
Args:
f: generator function to wrap
changes (list[str]): A list of strings detailing how the contexts
changed within a function.
start_context (LoggingContext): The initial context we're expecting
Returns:
function
""" """
from synapse.logging.context import LoggingContext from synapse.logging.context import LoggingContext
@ -131,6 +144,10 @@ def _check_yield_points(f, changes, start_context):
# This happens when the context is lost sometime *after* the # This happens when the context is lost sometime *after* the
# final yield and returning. E.g. we forgot to yield on a # final yield and returning. E.g. we forgot to yield on a
# function that returns a deferred. # function that returns a deferred.
#
# We don't raise here as its perfectly valid for contexts to
# change in a function, as long as it sets the correct context
# on resolving (which is checked separately).
err = ( err = (
"Function %r returned and changed context from %s to %s," "Function %r returned and changed context from %s to %s,"
" in %s between %d and end of func" " in %s between %d and end of func"
@ -143,14 +160,14 @@ def _check_yield_points(f, changes, start_context):
) )
) )
changes.append(err) changes.append(err)
# raise Exception(err)
return getattr(e, "value", None) return getattr(e, "value", None)
frame = gen.gi_frame frame = gen.gi_frame
if isinstance(d, defer.Deferred) and not d.called: if isinstance(d, defer.Deferred) and not d.called:
# This happens if we yield on a deferred that doesn't follow # This happens if we yield on a deferred that doesn't follow
# the log context rules without wrappin in a `make_deferred_yieldable` # the log context rules without wrappin in a `make_deferred_yieldable`.
# We raise here as this should never happen.
if LoggingContext.current_context() is not LoggingContext.sentinel: if LoggingContext.current_context() is not LoggingContext.sentinel:
err = ( err = (
"%s yielded with context %s rather than sentinel," "%s yielded with context %s rather than sentinel,"
@ -162,8 +179,7 @@ def _check_yield_points(f, changes, start_context):
frame.f_code.co_filename, frame.f_code.co_filename,
) )
) )
changes.append(err) raise Exception(err)
# raise Exception(err)
try: try:
result = yield d result = yield d
@ -171,10 +187,15 @@ def _check_yield_points(f, changes, start_context):
result = Failure(e) result = Failure(e)
if LoggingContext.current_context() != expected_context: if LoggingContext.current_context() != expected_context:
# This happens because the context is lost sometime *after* the # This happens because the context is lost sometime *after* the
# previous yield and *after* the current yield. E.g. the # previous yield and *after* the current yield. E.g. the
# deferred we waited on didn't follow the rules, or we forgot to # deferred we waited on didn't follow the rules, or we forgot to
# yield on a function between the two yield points. # yield on a function between the two yield points.
#
# We don't raise here as its perfectly valid for contexts to
# change in a function, as long as it sets the correct context
# on resolving (which is checked separately).
err = ( err = (
"%s changed context from %s to %s, happened between lines %d and %d in %s" "%s changed context from %s to %s, happened between lines %d and %d in %s"
% ( % (
@ -187,7 +208,6 @@ def _check_yield_points(f, changes, start_context):
) )
) )
changes.append(err) changes.append(err)
# raise Exception(err)
expected_context = LoggingContext.current_context() expected_context = LoggingContext.current_context()