_compat.py 3.0 KB

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