diff --git a/src/lightning_app/CHANGELOG.md b/src/lightning_app/CHANGELOG.md index 7ce0b8bee6..c6e8c0b686 100644 --- a/src/lightning_app/CHANGELOG.md +++ b/src/lightning_app/CHANGELOG.md @@ -32,6 +32,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Fixed `AutoScaler` raising an exception when non-default cloud compute is specified ([#15991](https://github.com/Lightning-AI/lightning/pull/15991)) +- Fixed the debugger detection mechanism for lightning App in VSCode ([#16068](https://github.com/Lightning-AI/lightning/pull/16068)) + ## [1.8.4] - 2022-12-08 diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index 3b152786b6..bc3d092b28 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -515,12 +515,29 @@ def _lightning_dispatched() -> bool: return bool(int(os.getenv("LIGHTNING_DISPATCHED", 0))) +def _using_debugger() -> bool: + """This method is used to detect whether the app is run with a debugger attached.""" + if "LIGHTNING_DETECTED_DEBUGGER" in os.environ: + return True + + # Collect the information about the process. + parent_process = os.popen(f"ps -ax | grep -i {os.getpid()} | grep -v grep").read() + + # Detect whether VSCode or PyCharm debugger are used + use_debugger = "debugpy" in parent_process or "pydev" in parent_process + + # Store the result to avoid multiple popen calls. + if use_debugger: + os.environ["LIGHTNING_DETECTED_DEBUGGER"] = "1" + return use_debugger + + def _should_dispatch_app() -> bool: return ( - __debug__ - and "_pytest.doctest" not in sys.modules - and not _lightning_dispatched() + not _lightning_dispatched() and "LIGHTNING_APP_STATE_URL" not in os.environ + # Keep last to avoid running it if already dispatched + and _using_debugger() )