Skip to content

Add support for PyCharm Pro's Remote Debug Server #5674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

cognifloyd
Copy link
Member

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

@cognifloyd cognifloyd added this to the 3.8.0 milestone Jul 18, 2022
@cognifloyd cognifloyd self-assigned this Jul 18, 2022
@cognifloyd cognifloyd requested a review from a team July 18, 2022 16:21
@pull-request-size pull-request-size bot added the size/M PR that changes 30-99 lines. Good size to review. label Jul 18, 2022
@guzzijones
Copy link
Contributor

guzzijones commented Jul 18, 2022 via email

@cognifloyd
Copy link
Member Author

cognifloyd commented Jul 18, 2022

@guzzijones JetBrains is based in the Czech Republic which became a state in 1993. It's predecessor, Czechoslovakia, escaped Soviet control in 1989.

Also, JetBrains has suspended operations in Russia due to the invasion of Ukraine: https://blog.jetbrains.com/blog/2022/03/11/jetbrains-statement-on-ukraine/

Copy link
Member

@arm4b arm4b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to integrate additional logic related to any IDE into the st2 core.

ST2_PYCHARM_DEBUG, ST2_PYCHARM_DEBUG_HOST, ST2_PYCHARM_DEBUG_PORT or anything else shouldn't be present in the st2common.

Copy link
Member Author

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see how including something IDE-specific is distasteful, but I believe it is warranted as yet-another-cost of our reliance on eventlet.

It is impossible afaik to hook the debugger into eventlet-based code without changing the sources (adding a call before eventlet.monkey_patch()). For normal python processes, there are several other ways to attach a debugger to the process, but this is the best way I've found with st2 code so far.

I've isolated these codepaths so that it is as minimal as possible, but if there's another way to enable using a debugger, I'm open to that.

Comment on lines +138 to +140
# 3. Check for ST2_PYCHARM_DEBUG env var
if os.environ.get("ST2_PYCHARM_DEBUG", False):
return True
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is not pycharm specific - it allows enabling debug mode via env var instead of changing the cli args.

Suggested change
# 3. Check for ST2_PYCHARM_DEBUG env var
if os.environ.get("ST2_PYCHARM_DEBUG", False):
return True
# 3. Check for ST2_DEBUG env var
if os.environ.get("ST2_DEBUG", False):
return True

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I split this change into a separate PR: #5675

Comment on lines +47 to +56
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(
Copy link
Member Author

@cognifloyd cognifloyd Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@armab Do you have a recommendation on how we can make this extensible so that people can inject whatever debugger before monkey_patching occurs?

Copy link
Member Author

@cognifloyd cognifloyd Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about something like:

def monkey_patch(patch_thread=None):
    """
    ...
    """
    use_debugger = is_use_debugger_flag_provided()
    if use_debugger:
        try:
            from st2_3rd_party_debugger import pre_monkey_patch
        except ImportError:
            pass
        else:
            pre_monkey_patch()

    ...
    import eventlet
    ...
    if patch_thread is None:
        patch_thread = not use_debugger

    eventlet.monkey_patch(...)
    ...

Then, I would only need to install a package in the st2 virtualenv that provides a pycharm-specific version of the st2_3rd_party_debugger module.

@armab what do you think of this solution for enabling me to inject the debugger without adding anything IDE-specific, and without patching the st2 code.

@guzzijones
Copy link
Contributor

guzzijones commented Jul 18, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement pantsbuild size/M PR that changes 30-99 lines. Good size to review. status:under discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants