From 7e665c3035a4b57ec51abf6b4bcc03d5404d5dcf Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 12:07:56 +0100 Subject: [PATCH 01/14] update --- src/lightning_app/utilities/app_helpers.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index 3b152786b682a..f4ad35d9a59bd 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -515,9 +515,20 @@ def _lightning_dispatched() -> bool: return bool(int(os.getenv("LIGHTNING_DISPATCHED", 0))) +def _use_debugger() -> bool: + """This method is used to detect whether the app is runned with a debugger attached.""" + if "LIGHTNING_DETECTED_DEBUGGER" in os.environ: + return True + parent_process = os.popen(f"ps -ej | grep -i {os.getpid()}").read() + use_debugger = "debugpy" in parent_process + if use_debugger: + os.environ["LIGHTNING_DETECTED_DEBUGGER"] = "1" + return use_debugger + + def _should_dispatch_app() -> bool: return ( - __debug__ + _use_debugger() and "_pytest.doctest" not in sys.modules and not _lightning_dispatched() and "LIGHTNING_APP_STATE_URL" not in os.environ From fe5b0a0c1bb34c2f4bc2083fc64d1bd1ec702a09 Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 12:09:56 +0100 Subject: [PATCH 02/14] update --- src/lightning_app/utilities/app_helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index f4ad35d9a59bd..d824133f1cb0f 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -515,7 +515,7 @@ def _lightning_dispatched() -> bool: return bool(int(os.getenv("LIGHTNING_DISPATCHED", 0))) -def _use_debugger() -> bool: +def _using_debugger() -> bool: """This method is used to detect whether the app is runned with a debugger attached.""" if "LIGHTNING_DETECTED_DEBUGGER" in os.environ: return True @@ -528,7 +528,7 @@ def _use_debugger() -> bool: def _should_dispatch_app() -> bool: return ( - _use_debugger() + _using_debugger() and "_pytest.doctest" not in sys.modules and not _lightning_dispatched() and "LIGHTNING_APP_STATE_URL" not in os.environ From f4d9102408b1bf4aa66cfdbe9caa864272ae83d5 Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 12:10:53 +0100 Subject: [PATCH 03/14] update --- src/lightning_app/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lightning_app/CHANGELOG.md b/src/lightning_app/CHANGELOG.md index 7ce0b8bee6f6c..c6e8c0b68609d 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 From ebf4549117cffb2c6e6af7fd4b3818f44d12164e Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 12:27:53 +0100 Subject: [PATCH 04/14] resolve comments --- src/lightning_app/utilities/app_helpers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index d824133f1cb0f..f3dc02482348f 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -516,11 +516,12 @@ def _lightning_dispatched() -> bool: def _using_debugger() -> bool: - """This method is used to detect whether the app is runned with a debugger attached.""" + """This method is used to detect whether the app is runned run a debugger attached to it.""" if "LIGHTNING_DETECTED_DEBUGGER" in os.environ: return True parent_process = os.popen(f"ps -ej | grep -i {os.getpid()}").read() - use_debugger = "debugpy" in parent_process + + use_debugger = "debugpy" in parent_process or "pydev" in parent_process # VSCode # PyCharm if use_debugger: os.environ["LIGHTNING_DETECTED_DEBUGGER"] = "1" return use_debugger From 16ac5abe20f78c040677b630dcc7569286b7186f Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 12:31:11 +0100 Subject: [PATCH 05/14] update --- src/lightning_app/utilities/app_helpers.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index f3dc02482348f..95e675310cad1 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -519,9 +519,14 @@ def _using_debugger() -> bool: """This method is used to detect whether the app is runned run a debugger attached to it.""" if "LIGHTNING_DETECTED_DEBUGGER" in os.environ: return True + + # Collect the information about the parent process. parent_process = os.popen(f"ps -ej | grep -i {os.getpid()}").read() - use_debugger = "debugpy" in parent_process or "pydev" in parent_process # VSCode # PyCharm + # 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 @@ -529,10 +534,11 @@ def _using_debugger() -> bool: def _should_dispatch_app() -> bool: return ( - _using_debugger() + not _lightning_dispatched() and "_pytest.doctest" not in sys.modules - and not _lightning_dispatched() and "LIGHTNING_APP_STATE_URL" not in os.environ + # Keep last to avoid running it if already dispatched + and _using_debugger() ) From 1d493ea86ad336e2eb429fb19b9910b216c50d6e Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 12:33:17 +0100 Subject: [PATCH 06/14] udpate --- src/lightning_app/utilities/app_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index 95e675310cad1..745ce3615a774 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -521,7 +521,7 @@ def _using_debugger() -> bool: return True # Collect the information about the parent process. - parent_process = os.popen(f"ps -ej | grep -i {os.getpid()}").read() + parent_process = os.popen(f"ps -ej | grep -i {os.getppid()}").read() # Detect whether VSCode or PyCharm debugger are used use_debugger = "debugpy" in parent_process or "pydev" in parent_process From 254338c62948be730aba17c619dc2fc4c614e8bb Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 12:38:53 +0100 Subject: [PATCH 07/14] update --- .storage/boring_file.txt | 5 +++++ examples/app_boring/app_dynamic.py | 2 +- src/lightning_app/utilities/app_helpers.py | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 .storage/boring_file.txt diff --git a/.storage/boring_file.txt b/.storage/boring_file.txt new file mode 100644 index 0000000000000..2a8574fb54178 --- /dev/null +++ b/.storage/boring_file.txt @@ -0,0 +1,5 @@ + +Hello there! +This tab is currently an IFrame of the FastAPI Server running in `DestinationFileAndServeWork`. +Also, the content of this file was created in `SourceFileWork` and then transferred to `DestinationFileAndServeWork`. +Are you already 🤯 ? Stick with us, this is only the beginning. Lightning is 🚀. diff --git a/examples/app_boring/app_dynamic.py b/examples/app_boring/app_dynamic.py index 5edb1f2898012..b1defcdc76332 100644 --- a/examples/app_boring/app_dynamic.py +++ b/examples/app_boring/app_dynamic.py @@ -70,4 +70,4 @@ def configure_layout(self): return {"name": "Boring Tab", "content": self.dict["dst_w"].url + "/file" if "dst_w" in self.dict else ""} -app = L.LightningApp(BoringApp(), log_level="debug") +app = L.LightningApp(BoringApp(), log_level="info") diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index 745ce3615a774..23bde7930d0a7 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -521,7 +521,7 @@ def _using_debugger() -> bool: return True # Collect the information about the parent process. - parent_process = os.popen(f"ps -ej | grep -i {os.getppid()}").read() + parent_process = os.popen(f"ps -ej | grep -i {os.getppid()} | 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 From 7563e4e29862f2e06f7bfe3b7ab7af415be98d86 Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 12:39:06 +0100 Subject: [PATCH 08/14] update --- .storage/boring_file.txt | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .storage/boring_file.txt diff --git a/.storage/boring_file.txt b/.storage/boring_file.txt deleted file mode 100644 index 2a8574fb54178..0000000000000 --- a/.storage/boring_file.txt +++ /dev/null @@ -1,5 +0,0 @@ - -Hello there! -This tab is currently an IFrame of the FastAPI Server running in `DestinationFileAndServeWork`. -Also, the content of this file was created in `SourceFileWork` and then transferred to `DestinationFileAndServeWork`. -Are you already 🤯 ? Stick with us, this is only the beginning. Lightning is 🚀. From d681c34213bd20df47d482adc699e2b5b813acbc Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 12:39:26 +0100 Subject: [PATCH 09/14] update --- examples/app_boring/app_dynamic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/app_boring/app_dynamic.py b/examples/app_boring/app_dynamic.py index b1defcdc76332..5edb1f2898012 100644 --- a/examples/app_boring/app_dynamic.py +++ b/examples/app_boring/app_dynamic.py @@ -70,4 +70,4 @@ def configure_layout(self): return {"name": "Boring Tab", "content": self.dict["dst_w"].url + "/file" if "dst_w" in self.dict else ""} -app = L.LightningApp(BoringApp(), log_level="info") +app = L.LightningApp(BoringApp(), log_level="debug") From 540d5170afc9ac09abdffb0b0a943ca723f340bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Thu, 15 Dec 2022 13:27:11 +0100 Subject: [PATCH 10/14] Debugging with Thomas --- src/lightning_app/utilities/app_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index 23bde7930d0a7..7207c7c240a99 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -521,7 +521,7 @@ def _using_debugger() -> bool: return True # Collect the information about the parent process. - parent_process = os.popen(f"ps -ej | grep -i {os.getppid()} | grep -i {os.getpid()} | grep -v grep").read() + parent_process = os.popen(f"ps -aux | 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 From 9b108a4d095b3ff8db2e834572b7b1a2db37c6fd Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 13:32:59 +0100 Subject: [PATCH 11/14] update --- src/lightning_app/utilities/app_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index 7207c7c240a99..2a353f96e1a69 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -521,7 +521,7 @@ def _using_debugger() -> bool: return True # Collect the information about the parent process. - parent_process = os.popen(f"ps -aux | grep -i {os.getpid()} | grep -v grep").read() + 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 From 9debca517dc375460f637998b7d423d771f460ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Thu, 15 Dec 2022 13:33:00 +0100 Subject: [PATCH 12/14] Debugging with Thomas --- src/lightning_app/utilities/app_helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index 7207c7c240a99..ede526ff7a3de 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -520,8 +520,8 @@ def _using_debugger() -> bool: if "LIGHTNING_DETECTED_DEBUGGER" in os.environ: return True - # Collect the information about the parent process. - parent_process = os.popen(f"ps -aux | grep -i {os.getpid()} | grep -v grep").read() + # 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 From ebed1d23588c6532f472a2cbfd66d5ed9c3bbfda Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 13:37:55 +0100 Subject: [PATCH 13/14] update --- src/lightning_app/utilities/app_helpers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index ede526ff7a3de..2eaaacaad3301 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -535,7 +535,6 @@ def _using_debugger() -> bool: def _should_dispatch_app() -> bool: return ( not _lightning_dispatched() - and "_pytest.doctest" not in sys.modules and "LIGHTNING_APP_STATE_URL" not in os.environ # Keep last to avoid running it if already dispatched and _using_debugger() From a8212099d0da561973318ef99c98195759f3ea1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Thu, 15 Dec 2022 13:48:32 +0100 Subject: [PATCH 14/14] Update src/lightning_app/utilities/app_helpers.py --- src/lightning_app/utilities/app_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning_app/utilities/app_helpers.py b/src/lightning_app/utilities/app_helpers.py index 2eaaacaad3301..bc3d092b280dd 100644 --- a/src/lightning_app/utilities/app_helpers.py +++ b/src/lightning_app/utilities/app_helpers.py @@ -516,7 +516,7 @@ def _lightning_dispatched() -> bool: def _using_debugger() -> bool: - """This method is used to detect whether the app is runned run a debugger attached to it.""" + """This method is used to detect whether the app is run with a debugger attached.""" if "LIGHTNING_DETECTED_DEBUGGER" in os.environ: return True