Skip to content

Commit ba0af63

Browse files
tchatonBorda
authored andcommitted
[App] Hot fix: Resolve detection of python debugger (#16068)
Co-authored-by: thomas <[email protected]> Co-authored-by: Carlos Mocholí <[email protected]> (cherry picked from commit eae56ee)
1 parent d403059 commit ba0af63

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2323
- Fixed `AutoScaler` raising an exception when non-default cloud compute is specified ([#15991](https://github.com/Lightning-AI/lightning/pull/15991))
2424
- Fixed and improvements of login flow ([#16052](https://github.com/Lightning-AI/lightning/pull/16052))
2525

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

2729
## [1.8.4] - 2022-12-08
2830

src/lightning_app/utilities/app_helpers.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,33 @@ def is_static_method(klass_or_instance, attr) -> bool:
511511
return isinstance(inspect.getattr_static(klass_or_instance, attr), staticmethod)
512512

513513

514+
def _lightning_dispatched() -> bool:
515+
return bool(int(os.getenv("LIGHTNING_DISPATCHED", 0)))
516+
517+
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+
514535
def _should_dispatch_app() -> bool:
515536
return (
516-
__debug__
517-
and "_pytest.doctest" not in sys.modules
518-
and not bool(int(os.getenv("LIGHTNING_DISPATCHED", "0")))
537+
not _lightning_dispatched()
519538
and "LIGHTNING_APP_STATE_URL" not in os.environ
539+
# Keep last to avoid running it if already dispatched
540+
and _using_debugger()
520541
)
521542

522543

0 commit comments

Comments
 (0)