patch_inline_callbacks.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import print_function
  16. import functools
  17. import sys
  18. from twisted.internet import defer
  19. from twisted.internet.defer import Deferred
  20. from twisted.python.failure import Failure
  21. def do_patch():
  22. """
  23. Patch defer.inlineCallbacks so that it checks the state of the logcontext on exit
  24. """
  25. from synapse.util.logcontext import LoggingContext
  26. orig_inline_callbacks = defer.inlineCallbacks
  27. def new_inline_callbacks(f):
  28. orig = orig_inline_callbacks(f)
  29. @functools.wraps(f)
  30. def wrapped(*args, **kwargs):
  31. start_context = LoggingContext.current_context()
  32. try:
  33. res = orig(*args, **kwargs)
  34. except Exception:
  35. if LoggingContext.current_context() != start_context:
  36. err = "%s changed context from %s to %s on exception" % (
  37. f, start_context, LoggingContext.current_context()
  38. )
  39. print(err, file=sys.stderr)
  40. raise Exception(err)
  41. raise
  42. if not isinstance(res, Deferred) or res.called:
  43. if LoggingContext.current_context() != start_context:
  44. err = "%s changed context from %s to %s" % (
  45. f, start_context, LoggingContext.current_context()
  46. )
  47. # print the error to stderr because otherwise all we
  48. # see in travis-ci is the 500 error
  49. print(err, file=sys.stderr)
  50. raise Exception(err)
  51. return res
  52. if LoggingContext.current_context() != LoggingContext.sentinel:
  53. err = (
  54. "%s returned incomplete deferred in non-sentinel context "
  55. "%s (start was %s)"
  56. ) % (
  57. f, LoggingContext.current_context(), start_context,
  58. )
  59. print(err, file=sys.stderr)
  60. raise Exception(err)
  61. def check_ctx(r):
  62. if LoggingContext.current_context() != start_context:
  63. err = "%s completion of %s changed context from %s to %s" % (
  64. "Failure" if isinstance(r, Failure) else "Success",
  65. f, start_context, LoggingContext.current_context(),
  66. )
  67. print(err, file=sys.stderr)
  68. raise Exception(err)
  69. return r
  70. res.addBoth(check_ctx)
  71. return res
  72. return wrapped
  73. defer.inlineCallbacks = new_inline_callbacks