patch_inline_callbacks.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.logging.context 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,
  38. start_context,
  39. LoggingContext.current_context(),
  40. )
  41. print(err, file=sys.stderr)
  42. raise Exception(err)
  43. raise
  44. if not isinstance(res, Deferred) or res.called:
  45. if LoggingContext.current_context() != start_context:
  46. err = "%s changed context from %s to %s" % (
  47. f,
  48. start_context,
  49. LoggingContext.current_context(),
  50. )
  51. # print the error to stderr because otherwise all we
  52. # see in travis-ci is the 500 error
  53. print(err, file=sys.stderr)
  54. raise Exception(err)
  55. return res
  56. if LoggingContext.current_context() != LoggingContext.sentinel:
  57. err = (
  58. "%s returned incomplete deferred in non-sentinel context "
  59. "%s (start was %s)"
  60. ) % (f, LoggingContext.current_context(), start_context)
  61. print(err, file=sys.stderr)
  62. raise Exception(err)
  63. def check_ctx(r):
  64. if LoggingContext.current_context() != start_context:
  65. err = "%s completion of %s changed context from %s to %s" % (
  66. "Failure" if isinstance(r, Failure) else "Success",
  67. f,
  68. start_context,
  69. LoggingContext.current_context(),
  70. )
  71. print(err, file=sys.stderr)
  72. raise Exception(err)
  73. return r
  74. res.addBoth(check_ctx)
  75. return res
  76. return wrapped
  77. defer.inlineCallbacks = new_inline_callbacks