Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[App] Improve pdb for multiprocessing #15950

Merged
merged 23 commits into from
Dec 8, 2022
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/lightning_app/perf/pdb.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import multiprocessing
import os
import pdb
import sys
from typing import Any

_stdin = [None]
_stdin_lock = multiprocessing.Lock()
try:
_stdin_fd = sys.stdin.fileno()
except Exception:
_stdin_fd = None

# Taken from https://github.com/facebookresearch/metaseq/blob/main/metaseq/pdb.py
class MPPdb(pdb.Pdb):
"""debugger for forked programs."""
"""A Pdb wrapper that works in a multiprocessing environment."""

def interaction(self, *args: Any, **kwargs: Any) -> None:
_stdin = sys.stdin
try:
sys.stdin = open("/dev/stdin")
pdb.Pdb.interaction(self, *args, **kwargs)
finally:
sys.stdin = _stdin
def __init__(self):
pdb.Pdb.__init__(self, nosigint=True)

def _cmdloop(self):
stdin_back = sys.stdin
with _stdin_lock:
try:
if _stdin_fd is not None:
if not _stdin[0]:
_stdin[0] = os.fdopen(_stdin_fd)
sys.stdin = _stdin[0]
self.cmdloop()
finally:
sys.stdin = stdin_back

def set_trace(*args: Any, **kwargs: Any) -> None:
MPPdb().set_trace(*args, **kwargs)

def set_trace():
pdb = MPPdb()
pdb.set_trace(sys._getframe().f_back)