# Buggy code
def notify_of_subprocess(self, conn):
    with self.session:
        if self.start_request is None or conn in self._known_subprocesses:
            return
        if "processId" in self.start_request.arguments:
            log.warning(
                "Not reporting subprocess for {0}, because the parent process "
                'was attached to using "processId" rather than "port".',
                self.session,
            )
            return

        log.info("Notifying {0} about {1}.", self, conn)
        body = dict(self.start_request.arguments)
        self._known_subprocesses.add(conn)

    body["name"] = fmt("Subprocess {0}", conn.pid)
    body["request"] = "attach"
    if "host" not in body:
        body["host"] = "127.0.0.1"
    if "port" not in body:
        _, body["port"] = self.listener.getsockname()
    if "processId" in body:
        del body["processId"]
    body["subProcessId"] = conn.pid

    self.channel.send_event("debugpyAttach", body)


# Traceback (ConnectionRefusedError)
C:\Python37\python.exe c:\Users\kanadig\.vscode\extensions\ms-python.python-2020.2.60897-dev\pythonFiles\lib\python\new_ptvsd\wheels\ptvsd\launcher C:\GIT\issues\issue9214/test_mp.py
Could not connect to 127.0.0.1: 50734
Traceback (most recent call last):
File "c:\Users\kanadig\.vscode\extensions\ms-python.python-2020.2.60897-dev\pythonFiles\lib\python\new_ptvsd\wheels\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 514, in start_client
s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\Users\kanadig\.vscode\extensions\ms-python.python-2020.2.60897-dev\pythonFiles\lib\python\new_ptvsd\wheels\ptvsd\_vendored\pydevd\pydevd.py", line 2550, in settrace
client_access_token,
File "c:\Users\kanadig\.vscode\extensions\ms-python.python-2020.2.60897-dev\pythonFiles\lib\python\new_ptvsd\wheels\ptvsd\_vendored\pydevd\pydevd.py", line 2610, in _locked_settrace
py_db.connect(host, port)  # Note: connect can raise error.
File "c:\Users\kanadig\.vscode\extensions\ms-python.python-2020.2.60897-dev\pythonFiles\lib\python\new_ptvsd\wheels\ptvsd\_vendored\pydevd\pydevd.py", line 1179, in connect
s = start_client(host, port)
File "c:\Users\kanadig\.vscode\extensions\ms-python.python-2020.2.60897-dev\pythonFiles\lib\python\new_ptvsd\wheels\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 514, in start_client
s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Python37\lib\concurrent\futures\process.py", line 102, in _python_exit
thread_wakeup.wakeup()
File "C:\Python37\lib\concurrent\futures\process.py", line 90, in wakeup
self._writer.send_bytes(b"")
File "C:\Python37\lib\multiprocessing\connection.py", line 183, in send_bytes
self._check_closed()
File "C:\Python37\lib\multiprocessing\connection.py", line 136, in _check_closed
raise OSError("handle is closed")
OSError: handle is closed

# Fixed code
def notify_of_subprocess(self, conn):
    with self.session:
        if self.start_request is None or conn in self._known_subprocesses:
            return
        if "processId" in self.start_request.arguments:
            log.warning(
                "Not reporting subprocess for {0}, because the parent process "
                'was attached to using "processId" rather than "port".',
                self.session,
            )
            return

        log.info("Notifying {0} about {1}.", self, conn)
        body = dict(self.start_request.arguments)
        self._known_subprocesses.add(conn)

    body["name"] = fmt("Subprocess {0}", conn.pid)
    body["request"] = "attach"
    if "host" not in body:
        body["host"] = "127.0.0.1"
    if "port" not in body:
        _, body["port"] = listener.getsockname()
    if "processId" in body:
        del body["processId"]
    body["subProcessId"] = conn.pid

    self.channel.send_event("debugpyAttach", body)