_compat.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import sys
  2. from typing import TYPE_CHECKING
  3. if TYPE_CHECKING:
  4. from typing import Any
  5. from typing import TypeVar
  6. T = TypeVar("T")
  7. PY37 = sys.version_info[0] == 3 and sys.version_info[1] >= 7
  8. PY38 = sys.version_info[0] == 3 and sys.version_info[1] >= 8
  9. PY310 = sys.version_info[0] == 3 and sys.version_info[1] >= 10
  10. PY311 = sys.version_info[0] == 3 and sys.version_info[1] >= 11
  11. def with_metaclass(meta, *bases):
  12. # type: (Any, *Any) -> Any
  13. class MetaClass(type):
  14. def __new__(metacls, name, this_bases, d):
  15. # type: (Any, Any, Any, Any) -> Any
  16. return meta(name, bases, d)
  17. return type.__new__(MetaClass, "temporary_class", (), {})
  18. def check_uwsgi_thread_support():
  19. # type: () -> bool
  20. # We check two things here:
  21. #
  22. # 1. uWSGI doesn't run in threaded mode by default -- issue a warning if
  23. # that's the case.
  24. #
  25. # 2. Additionally, if uWSGI is running in preforking mode (default), it needs
  26. # the --py-call-uwsgi-fork-hooks option for the SDK to work properly. This
  27. # is because any background threads spawned before the main process is
  28. # forked are NOT CLEANED UP IN THE CHILDREN BY DEFAULT even if
  29. # --enable-threads is on. One has to explicitly provide
  30. # --py-call-uwsgi-fork-hooks to force uWSGI to run regular cpython
  31. # after-fork hooks that take care of cleaning up stale thread data.
  32. try:
  33. from uwsgi import opt # type: ignore
  34. except ImportError:
  35. return True
  36. from sentry_sdk.consts import FALSE_VALUES
  37. def enabled(option):
  38. # type: (str) -> bool
  39. value = opt.get(option, False)
  40. if isinstance(value, bool):
  41. return value
  42. if isinstance(value, bytes):
  43. try:
  44. value = value.decode()
  45. except Exception:
  46. pass
  47. return value and str(value).lower() not in FALSE_VALUES
  48. # When `threads` is passed in as a uwsgi option,
  49. # `enable-threads` is implied on.
  50. threads_enabled = "threads" in opt or enabled("enable-threads")
  51. fork_hooks_on = enabled("py-call-uwsgi-fork-hooks")
  52. lazy_mode = enabled("lazy-apps") or enabled("lazy")
  53. if lazy_mode and not threads_enabled:
  54. from warnings import warn
  55. warn(
  56. Warning(
  57. "IMPORTANT: "
  58. "We detected the use of uWSGI without thread support. "
  59. "This might lead to unexpected issues. "
  60. 'Please run uWSGI with "--enable-threads" for full support.'
  61. )
  62. )
  63. return False
  64. elif not lazy_mode and (not threads_enabled or not fork_hooks_on):
  65. from warnings import warn
  66. warn(
  67. Warning(
  68. "IMPORTANT: "
  69. "We detected the use of uWSGI in preforking mode without "
  70. "thread support. This might lead to crashing workers. "
  71. 'Please run uWSGI with both "--enable-threads" and '
  72. '"--py-call-uwsgi-fork-hooks" for full support.'
  73. )
  74. )
  75. return False
  76. return True