2018-12-04 05:30:32 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-12-04 05:30:32 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import functools
|
|
|
|
import sys
|
2022-02-18 10:57:26 -05:00
|
|
|
from typing import Any, Callable, Generator, List, TypeVar, cast
|
2019-10-10 07:15:17 -04:00
|
|
|
|
2022-05-09 06:27:39 -04:00
|
|
|
from typing_extensions import ParamSpec
|
|
|
|
|
2018-12-04 05:30:32 -05:00
|
|
|
from twisted.internet import defer
|
|
|
|
from twisted.internet.defer import Deferred
|
|
|
|
from twisted.python.failure import Failure
|
|
|
|
|
2019-10-10 07:15:17 -04:00
|
|
|
# Tracks if we've already patched inlineCallbacks
|
|
|
|
_already_patched = False
|
|
|
|
|
|
|
|
|
2021-10-06 06:20:49 -04:00
|
|
|
T = TypeVar("T")
|
2022-05-09 06:27:39 -04:00
|
|
|
P = ParamSpec("P")
|
2021-10-06 06:20:49 -04:00
|
|
|
|
|
|
|
|
2021-09-10 12:03:18 -04:00
|
|
|
def do_patch() -> None:
|
2018-12-04 05:30:32 -05:00
|
|
|
"""
|
|
|
|
Patch defer.inlineCallbacks so that it checks the state of the logcontext on exit
|
|
|
|
"""
|
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
from synapse.logging.context import current_context
|
2018-12-04 05:30:32 -05:00
|
|
|
|
2019-10-10 07:15:17 -04:00
|
|
|
global _already_patched
|
|
|
|
|
2018-12-04 05:30:32 -05:00
|
|
|
orig_inline_callbacks = defer.inlineCallbacks
|
2019-10-10 07:15:17 -04:00
|
|
|
if _already_patched:
|
2019-09-27 10:58:14 -04:00
|
|
|
return
|
2018-12-04 05:30:32 -05:00
|
|
|
|
2021-10-06 06:20:49 -04:00
|
|
|
def new_inline_callbacks(
|
2022-05-09 06:27:39 -04:00
|
|
|
f: Callable[P, Generator["Deferred[object]", object, T]]
|
|
|
|
) -> Callable[P, "Deferred[T]"]:
|
2018-12-04 05:30:32 -05:00
|
|
|
@functools.wraps(f)
|
2022-05-09 06:27:39 -04:00
|
|
|
def wrapped(*args: P.args, **kwargs: P.kwargs) -> "Deferred[T]":
|
2020-03-24 10:45:33 -04:00
|
|
|
start_context = current_context()
|
2021-07-15 12:46:54 -04:00
|
|
|
changes: List[str] = []
|
2022-05-09 06:27:39 -04:00
|
|
|
orig: Callable[P, "Deferred[T]"] = orig_inline_callbacks(
|
2021-10-06 06:20:49 -04:00
|
|
|
_check_yield_points(f, changes)
|
|
|
|
)
|
2018-12-04 05:30:32 -05:00
|
|
|
|
|
|
|
try:
|
2021-10-06 06:20:49 -04:00
|
|
|
res: "Deferred[T]" = orig(*args, **kwargs)
|
2018-12-04 05:30:32 -05:00
|
|
|
except Exception:
|
2020-03-24 10:45:33 -04:00
|
|
|
if current_context() != start_context:
|
2019-09-27 10:11:14 -04:00
|
|
|
for err in changes:
|
|
|
|
print(err, file=sys.stderr)
|
|
|
|
|
2018-12-04 05:30:32 -05:00
|
|
|
err = "%s changed context from %s to %s on exception" % (
|
2019-05-10 01:12:11 -04:00
|
|
|
f,
|
|
|
|
start_context,
|
2020-03-24 10:45:33 -04:00
|
|
|
current_context(),
|
2018-12-04 05:30:32 -05:00
|
|
|
)
|
|
|
|
print(err, file=sys.stderr)
|
|
|
|
raise Exception(err)
|
|
|
|
raise
|
|
|
|
|
|
|
|
if not isinstance(res, Deferred) or res.called:
|
2020-03-24 10:45:33 -04:00
|
|
|
if current_context() != start_context:
|
2019-09-27 10:11:14 -04:00
|
|
|
for err in changes:
|
|
|
|
print(err, file=sys.stderr)
|
|
|
|
|
|
|
|
err = "Completed %s changed context from %s to %s" % (
|
2019-05-10 01:12:11 -04:00
|
|
|
f,
|
|
|
|
start_context,
|
2020-03-24 10:45:33 -04:00
|
|
|
current_context(),
|
2018-12-04 05:30:32 -05:00
|
|
|
)
|
|
|
|
# print the error to stderr because otherwise all we
|
|
|
|
# see in travis-ci is the 500 error
|
|
|
|
print(err, file=sys.stderr)
|
|
|
|
raise Exception(err)
|
|
|
|
return res
|
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
if current_context():
|
2018-12-04 05:30:32 -05:00
|
|
|
err = (
|
|
|
|
"%s returned incomplete deferred in non-sentinel context "
|
|
|
|
"%s (start was %s)"
|
2020-03-24 10:45:33 -04:00
|
|
|
) % (f, current_context(), start_context)
|
2018-12-04 05:30:32 -05:00
|
|
|
print(err, file=sys.stderr)
|
|
|
|
raise Exception(err)
|
|
|
|
|
2021-10-06 06:20:49 -04:00
|
|
|
def check_ctx(r: T) -> T:
|
2020-03-24 10:45:33 -04:00
|
|
|
if current_context() != start_context:
|
2019-09-27 10:11:14 -04:00
|
|
|
for err in changes:
|
|
|
|
print(err, file=sys.stderr)
|
2018-12-04 05:30:32 -05:00
|
|
|
err = "%s completion of %s changed context from %s to %s" % (
|
|
|
|
"Failure" if isinstance(r, Failure) else "Success",
|
2019-05-10 01:12:11 -04:00
|
|
|
f,
|
|
|
|
start_context,
|
2020-03-24 10:45:33 -04:00
|
|
|
current_context(),
|
2018-12-04 05:30:32 -05:00
|
|
|
)
|
|
|
|
print(err, file=sys.stderr)
|
|
|
|
raise Exception(err)
|
|
|
|
return r
|
|
|
|
|
|
|
|
res.addBoth(check_ctx)
|
|
|
|
return res
|
|
|
|
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
defer.inlineCallbacks = new_inline_callbacks
|
2019-10-10 07:15:17 -04:00
|
|
|
_already_patched = True
|
2019-09-27 10:11:14 -04:00
|
|
|
|
|
|
|
|
2021-10-06 06:20:49 -04:00
|
|
|
def _check_yield_points(
|
2022-05-09 06:27:39 -04:00
|
|
|
f: Callable[P, Generator["Deferred[object]", object, T]],
|
2021-10-06 06:20:49 -04:00
|
|
|
changes: List[str],
|
|
|
|
) -> Callable:
|
2019-10-10 05:53:06 -04:00
|
|
|
"""Wraps a generator that is about to be passed to defer.inlineCallbacks
|
2019-09-27 10:58:14 -04:00
|
|
|
checking that after every yield the log contexts are correct.
|
2019-10-10 06:53:57 -04:00
|
|
|
|
2019-10-10 08:17:19 -04:00
|
|
|
It's perfectly valid for log contexts to change within a function, e.g. due
|
2019-10-10 06:53:57 -04:00
|
|
|
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
|
2019-10-10 07:15:17 -04:00
|
|
|
changes: A list of strings detailing how the contexts
|
2019-10-10 06:53:57 -04:00
|
|
|
changed within a function.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
function
|
2019-09-27 10:58:14 -04:00
|
|
|
"""
|
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
from synapse.logging.context import current_context
|
2019-09-27 10:11:14 -04:00
|
|
|
|
|
|
|
@functools.wraps(f)
|
2021-10-06 06:20:49 -04:00
|
|
|
def check_yield_points_inner(
|
2022-05-09 06:27:39 -04:00
|
|
|
*args: P.args, **kwargs: P.kwargs
|
2021-10-06 06:20:49 -04:00
|
|
|
) -> Generator["Deferred[object]", object, T]:
|
2019-09-27 10:11:14 -04:00
|
|
|
gen = f(*args, **kwargs)
|
|
|
|
|
2019-10-10 05:59:07 -04:00
|
|
|
last_yield_line_no = gen.gi_frame.f_lineno
|
2021-07-15 12:46:54 -04:00
|
|
|
result: Any = None
|
2019-09-27 10:11:14 -04:00
|
|
|
while True:
|
2020-03-24 10:45:33 -04:00
|
|
|
expected_context = current_context()
|
2019-10-10 07:15:17 -04:00
|
|
|
|
2019-09-27 10:11:14 -04:00
|
|
|
try:
|
|
|
|
isFailure = isinstance(result, Failure)
|
|
|
|
if isFailure:
|
|
|
|
d = result.throwExceptionIntoGenerator(gen)
|
|
|
|
else:
|
|
|
|
d = gen.send(result)
|
|
|
|
except (StopIteration, defer._DefGen_Return) as e:
|
2020-03-24 10:45:33 -04:00
|
|
|
if current_context() != expected_context:
|
2019-09-27 10:11:14 -04:00
|
|
|
# This happens when the context is lost sometime *after* the
|
|
|
|
# final yield and returning. E.g. we forgot to yield on a
|
|
|
|
# function that returns a deferred.
|
2019-10-10 06:53:57 -04:00
|
|
|
#
|
2019-10-10 08:17:19 -04:00
|
|
|
# We don't raise here as it's perfectly valid for contexts to
|
2019-10-10 06:53:57 -04:00
|
|
|
# change in a function, as long as it sets the correct context
|
|
|
|
# on resolving (which is checked separately).
|
2019-09-27 10:11:14 -04:00
|
|
|
err = (
|
2019-09-27 10:58:14 -04:00
|
|
|
"Function %r returned and changed context from %s to %s,"
|
|
|
|
" in %s between %d and end of func"
|
2019-09-27 10:11:14 -04:00
|
|
|
% (
|
|
|
|
f.__qualname__,
|
2019-10-10 05:59:07 -04:00
|
|
|
expected_context,
|
2020-03-24 10:45:33 -04:00
|
|
|
current_context(),
|
2019-09-27 10:11:14 -04:00
|
|
|
f.__code__.co_filename,
|
|
|
|
last_yield_line_no,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
changes.append(err)
|
2022-02-18 10:57:26 -05:00
|
|
|
# The `StopIteration` or `_DefGen_Return` contains the return value from the
|
|
|
|
# generator.
|
|
|
|
return cast(T, e.value)
|
2019-09-27 10:11:14 -04:00
|
|
|
|
2019-10-10 05:58:32 -04:00
|
|
|
frame = gen.gi_frame
|
|
|
|
|
2019-10-10 05:59:07 -04:00
|
|
|
if isinstance(d, defer.Deferred) and not d.called:
|
2019-10-10 05:58:32 -04:00
|
|
|
# This happens if we yield on a deferred that doesn't follow
|
2019-10-10 08:17:19 -04:00
|
|
|
# the log context rules without wrapping in a `make_deferred_yieldable`.
|
2019-10-10 06:53:57 -04:00
|
|
|
# We raise here as this should never happen.
|
2020-03-24 10:45:33 -04:00
|
|
|
if current_context():
|
2019-10-10 05:58:32 -04:00
|
|
|
err = (
|
2019-10-10 05:59:07 -04:00
|
|
|
"%s yielded with context %s rather than sentinel,"
|
2019-10-10 05:58:32 -04:00
|
|
|
" yielded on line %d in %s"
|
|
|
|
% (
|
|
|
|
frame.f_code.co_name,
|
2020-03-24 10:45:33 -04:00
|
|
|
current_context(),
|
2019-10-10 05:58:32 -04:00
|
|
|
frame.f_lineno,
|
|
|
|
frame.f_code.co_filename,
|
|
|
|
)
|
|
|
|
)
|
2019-10-10 06:53:57 -04:00
|
|
|
raise Exception(err)
|
2019-10-10 05:58:32 -04:00
|
|
|
|
2020-05-22 05:17:36 -04:00
|
|
|
# the wrapped function yielded a Deferred: yield it back up to the parent
|
|
|
|
# inlineCallbacks().
|
2019-09-27 10:11:14 -04:00
|
|
|
try:
|
|
|
|
result = yield d
|
2020-05-22 05:17:36 -04:00
|
|
|
except Exception:
|
|
|
|
# this will fish an earlier Failure out of the stack where possible, and
|
2020-07-09 09:52:58 -04:00
|
|
|
# thus is preferable to passing in an exception to the Failure
|
2020-05-22 05:17:36 -04:00
|
|
|
# constructor, since it results in less stack-mangling.
|
|
|
|
result = Failure()
|
2019-09-27 10:11:14 -04:00
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
if current_context() != expected_context:
|
2019-09-27 10:11:14 -04:00
|
|
|
# This happens because the context is lost sometime *after* the
|
|
|
|
# previous yield and *after* the current yield. E.g. the
|
|
|
|
# deferred we waited on didn't follow the rules, or we forgot to
|
|
|
|
# yield on a function between the two yield points.
|
2019-10-10 06:53:57 -04:00
|
|
|
#
|
|
|
|
# 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).
|
2022-03-29 06:41:19 -04:00
|
|
|
err = (
|
|
|
|
"%s changed context from %s to %s, happened between lines %d and %d in %s"
|
|
|
|
% (
|
|
|
|
frame.f_code.co_name,
|
|
|
|
expected_context,
|
|
|
|
current_context(),
|
|
|
|
last_yield_line_no,
|
|
|
|
frame.f_lineno,
|
|
|
|
frame.f_code.co_filename,
|
|
|
|
)
|
2019-09-27 10:11:14 -04:00
|
|
|
)
|
|
|
|
changes.append(err)
|
|
|
|
|
|
|
|
last_yield_line_no = frame.f_lineno
|
|
|
|
|
|
|
|
return check_yield_points_inner
|