Skip to content

Commit

Permalink
[App] Hot fix: Resolve detection of python debugger (#16068)
Browse files Browse the repository at this point in the history
Co-authored-by: thomas <thomas@thomass-MacBook-Pro.local>
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
(cherry picked from commit eae56ee)
  • Loading branch information
tchaton authored and Borda committed Dec 15, 2022
1 parent 55664de commit 806d035
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/lightning_app/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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

Expand Down
23 changes: 20 additions & 3 deletions src/lightning_app/utilities/app_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)


Expand Down

0 comments on commit 806d035

Please sign in to comment.