From 69572e93bd9b0f1a1e4dd01488432a035213c06d Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Tue, 18 May 2021 17:34:59 -0500 Subject: [PATCH 1/4] allow for debugging in pycharm --- st2common/st2common/util/monkey_patch.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/st2common/st2common/util/monkey_patch.py b/st2common/st2common/util/monkey_patch.py index 1f0bb42cb4..8c494480fe 100644 --- a/st2common/st2common/util/monkey_patch.py +++ b/st2common/st2common/util/monkey_patch.py @@ -19,6 +19,7 @@ from __future__ import absolute_import +import os import sys __all__ = [ @@ -43,6 +44,23 @@ def monkey_patch(patch_thread=None): patched unless debugger is used. :type patch_thread: ``bool`` """ + if os.environ.get("ST2_PYCHARM_DEBUG", False): + + # pydevd_pycharm uses this to save copies of stdlib modules before + # eventlet monkey patches them, or the debugger will not work. + # see: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000333980 + os.environ["GEVENT_SUPPORT"] = "True" + + import pydevd_pycharm + + pydevd_pycharm.settrace( + os.environ.get("ST2_PYCHARM_DEBUG_HOST", "localhost"), + port=os.environ.get("ST2_PYCHARM_DEBUG_PORT", 5000), + stdoutToServer=True, + stderrToServer=True, + patch_multiprocessing=True + ) + # Eventlet when patched doesn't throw the standard ssl error on timeout, which can break # some third-party libraries including redis SSL. # See: https://github.com/eventlet/eventlet/issues/692 @@ -117,4 +135,8 @@ def is_use_debugger_flag_provided(): if arg.startswith(PARENT_ARGS_FLAG) and USE_DEBUGGER_FLAG in arg: return True + # 3. Check for ST2_PYCHARM_DEBUG env var + if os.environ.get("ST2_PYCHARM_DEBUG", False): + return True + return False From e2bb9a69a8249ef88274ed2407a67947d5c3d075 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Mon, 18 Jul 2022 11:13:42 -0500 Subject: [PATCH 2/4] add changelog entry --- CHANGELOG.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4c9693a425..83c1fab659 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -25,6 +25,19 @@ Added * Added graceful shutdown for workflow engine. #5463 Contributed by @khushboobhatia01 +* Allow debugging st2 services in PyCharm Pro's Remote Debug Server. + To enable debugging, before starting the st2 process(es), you need to: + + - install pydevd-pycharm (use the version specific to your pycharm install) in StackStorm's virtualenv. + - set the ``ST2_PYCHARM_DEBUG`` environment var to something non-empty like "true". + - (optional) set environment vars ``ST2_PYCHARM_DEBUG_HOST`` and + ``ST2_PYCHARM_DEBUG_PORT`` if pycharm is listening somewhere other than + ``localhost:5000``. + + see: https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html#remote-debug-config + + #5674 Contributed by @cognifloyd. + 3.7.0 - May 05, 2022 -------------------- From 8636000fa3a1bb519eeb1b843795caf7cc4e61c8 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Mon, 18 Jul 2022 11:24:29 -0500 Subject: [PATCH 3/4] reformat with black --- st2common/st2common/util/monkey_patch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/st2common/st2common/util/monkey_patch.py b/st2common/st2common/util/monkey_patch.py index 8c494480fe..3798382425 100644 --- a/st2common/st2common/util/monkey_patch.py +++ b/st2common/st2common/util/monkey_patch.py @@ -58,7 +58,7 @@ def monkey_patch(patch_thread=None): port=os.environ.get("ST2_PYCHARM_DEBUG_PORT", 5000), stdoutToServer=True, stderrToServer=True, - patch_multiprocessing=True + patch_multiprocessing=True, ) # Eventlet when patched doesn't throw the standard ssl error on timeout, which can break From dcf7f413c70bc59f1e3d383425869df21bf23a0b Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Mon, 18 Jul 2022 11:33:57 -0500 Subject: [PATCH 4/4] make pylint ignore pydevd_pycharm --- lint-configs/python/.pylintrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lint-configs/python/.pylintrc b/lint-configs/python/.pylintrc index d19e02e3cc..d2367a76d8 100644 --- a/lint-configs/python/.pylintrc +++ b/lint-configs/python/.pylintrc @@ -27,7 +27,7 @@ property-classes=abc.abstractproperty # Note: This modules are manipulated during the runtime so we can't detect all the properties during # static analysis # orjson has type stubs, but pylint doesn't support __init__.pyi yet: https://github.com/PyCQA/pylint/issues/2873 -ignored-modules=distutils,eventlet.green.subprocess,six,six.moves,orjson +ignored-modules=distutils,eventlet.green.subprocess,six,six.moves,orjson,pydevd_pycharm [FORMAT] max-line-length=100