Skip to content

Commit 6618985

Browse files
tchatonthomaspre-commit-ci[bot]
authored andcommitted
[App] Improve pdb for multiprocessing (#15950)
Co-authored-by: thomas <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> (cherry picked from commit 482b279)
1 parent f142ce0 commit 6618985

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

examples/app_dag/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
sklearn
1+
scikit-learn
22
pandas

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ module = [
9494
"lightning_app.frontend.streamlit_base",
9595
"lightning_app.frontend.utils",
9696
"lightning_app.frontend.web",
97+
"lightning_app.perf.pdb",
9798
"lightning_app.runners.backends.__init__",
9899
"lightning_app.runners.backends.backend",
99100
"lightning_app.runners.backends.cloud",

src/lightning_app/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
4646
- Fixed a bug where using `L.app.structures` would cause multiple apps to be opened and fail with an error in the cloud ([#15911](https://github.com/Lightning-AI/lightning/pull/15911))
4747
- Fixed PythonServer generating noise on M1 ([#15949](https://github.com/Lightning-AI/lightning/pull/15949))
4848

49+
- Fixed multiprocessing breakpoint ([#15950](https://github.com/Lightning-AI/lightning/pull/15950))
4950

5051
## [1.8.3] - 2022-11-22
5152

src/lightning_app/perf/pdb.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1+
import multiprocessing
2+
import os
13
import pdb
24
import sys
3-
from typing import Any
45

6+
_stdin = [None]
7+
_stdin_lock = multiprocessing.Lock()
8+
try:
9+
_stdin_fd = sys.stdin.fileno()
10+
except Exception:
11+
_stdin_fd = None
512

13+
14+
# Taken from https://github.com/facebookresearch/metaseq/blob/main/metaseq/pdb.py
615
class MPPdb(pdb.Pdb):
7-
"""debugger for forked programs."""
16+
"""A Pdb wrapper that works in a multiprocessing environment."""
17+
18+
def __init__(self) -> None:
19+
pdb.Pdb.__init__(self, nosigint=True)
820

9-
def interaction(self, *args: Any, **kwargs: Any) -> None:
10-
_stdin = sys.stdin
11-
try:
12-
sys.stdin = open("/dev/stdin")
13-
pdb.Pdb.interaction(self, *args, **kwargs)
14-
finally:
15-
sys.stdin = _stdin
21+
def _cmdloop(self) -> None:
22+
stdin_back = sys.stdin
23+
with _stdin_lock:
24+
try:
25+
if _stdin_fd is not None:
26+
if not _stdin[0]:
27+
_stdin[0] = os.fdopen(_stdin_fd)
28+
sys.stdin = _stdin[0]
29+
self.cmdloop()
30+
finally:
31+
sys.stdin = stdin_back
1632

1733

18-
def set_trace(*args: Any, **kwargs: Any) -> None:
19-
MPPdb().set_trace(*args, **kwargs)
34+
def set_trace() -> None:
35+
pdb = MPPdb()
36+
pdb.set_trace(sys._getframe().f_back)

0 commit comments

Comments
 (0)