Skip to content

Commit eae56ee

Browse files
tchatonthomascarmocca
authored
[App] Hot fix: Resolve detection of python debugger (#16068)
Co-authored-by: thomas <[email protected]> Co-authored-by: Carlos Mocholí <[email protected]>
1 parent c4b27fb commit eae56ee

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
3232

3333
- Fixed `AutoScaler` raising an exception when non-default cloud compute is specified ([#15991](https://github.com/Lightning-AI/lightning/pull/15991))
3434

35+
- Fixed the debugger detection mechanism for lightning App in VSCode ([#16068](https://github.com/Lightning-AI/lightning/pull/16068))
36+
3537

3638
## [1.8.4] - 2022-12-08
3739

src/lightning_app/utilities/app_helpers.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,29 @@ def _lightning_dispatched() -> bool:
515515
return bool(int(os.getenv("LIGHTNING_DISPATCHED", 0)))
516516

517517

518+
def _using_debugger() -> bool:
519+
"""This method is used to detect whether the app is run with a debugger attached."""
520+
if "LIGHTNING_DETECTED_DEBUGGER" in os.environ:
521+
return True
522+
523+
# Collect the information about the process.
524+
parent_process = os.popen(f"ps -ax | grep -i {os.getpid()} | grep -v grep").read()
525+
526+
# Detect whether VSCode or PyCharm debugger are used
527+
use_debugger = "debugpy" in parent_process or "pydev" in parent_process
528+
529+
# Store the result to avoid multiple popen calls.
530+
if use_debugger:
531+
os.environ["LIGHTNING_DETECTED_DEBUGGER"] = "1"
532+
return use_debugger
533+
534+
518535
def _should_dispatch_app() -> bool:
519536
return (
520-
__debug__
521-
and "_pytest.doctest" not in sys.modules
522-
and not _lightning_dispatched()
537+
not _lightning_dispatched()
523538
and "LIGHTNING_APP_STATE_URL" not in os.environ
539+
# Keep last to avoid running it if already dispatched
540+
and _using_debugger()
524541
)
525542

526543

0 commit comments

Comments
 (0)