debug.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket 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 twisted.internet import defer, reactor
  16. from functools import wraps
  17. from synapse.util.logcontext import LoggingContext, PreserveLoggingContext
  18. def debug_deferreds():
  19. """Cause all deferreds to wait for a reactor tick before running their
  20. callbacks. This increases the chance of getting a stack trace out of
  21. a defer.inlineCallback since the code waiting on the deferred will get
  22. a chance to add an errback before the deferred runs."""
  23. # Helper method for retrieving and restoring the current logging context
  24. # around a callback.
  25. def with_logging_context(fn):
  26. context = LoggingContext.current_context()
  27. def restore_context_callback(x):
  28. with PreserveLoggingContext(context):
  29. return fn(x)
  30. return restore_context_callback
  31. # We are going to modify the __init__ method of defer.Deferred so we
  32. # need to get a copy of the old method so we can still call it.
  33. old__init__ = defer.Deferred.__init__
  34. # We need to create a deferred to bounce the callbacks through the reactor
  35. # but we don't want to add a callback when we create that deferred so we
  36. # we create a new type of deferred that uses the old __init__ method.
  37. # This is safe as long as the old __init__ method doesn't invoke an
  38. # __init__ using super.
  39. class Bouncer(defer.Deferred):
  40. __init__ = old__init__
  41. # We'll add this as a callback to all Deferreds. Twisted will wait until
  42. # the bouncer deferred resolves before calling the callbacks of the
  43. # original deferred.
  44. def bounce_callback(x):
  45. bouncer = Bouncer()
  46. reactor.callLater(0, with_logging_context(bouncer.callback), x)
  47. return bouncer
  48. # We'll add this as an errback to all Deferreds. Twisted will wait until
  49. # the bouncer deferred resolves before calling the errbacks of the
  50. # original deferred.
  51. def bounce_errback(x):
  52. bouncer = Bouncer()
  53. reactor.callLater(0, with_logging_context(bouncer.errback), x)
  54. return bouncer
  55. @wraps(old__init__)
  56. def new__init__(self, *args, **kargs):
  57. old__init__(self, *args, **kargs)
  58. self.addCallbacks(bounce_callback, bounce_errback)
  59. defer.Deferred.__init__ = new__init__