Repository: microsoft/ptvsd

Issue:
Debugging fails with timeout when using conda run
@karthiknadig 

## Environment data

- PTVSD version: 5.0.0a7
- OS: Windows_NT x64 10.0.18363
- Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.4 , Anaconda
- Using VS Code or Visual Studio: Version: 1.41.0-insider (user setup)

## Steps to reproduce:
1. Add the following config in `launch.json`.
```
{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "python": [
        "conda",
        "run",
        "-n",
        "base",
        "python"
    ],
    "logToFile": true
}
```
2. Debug the python extension. Make sure you're in experiments `DebugAdapterFactory - experiment` and `PtvsdWheels37 - experiment`. Remove all references in the resolver to `"pythonPath"` as it's deprecated.
3. Select conda as interpreter.
4. Start debugging.


## Expected behavior

Debugging works. And the command should begin with `conda run -n base python ...`.

## Actual behavior

The command: 
![image](https://user-images.githubusercontent.com/13199757/69390277-8e8bd800-0c83-11ea-9ced-e5c3bc30e48d.png)

Debugging fails:
![image](https://user-images.githubusercontent.com/13199757/69390331-bc711c80-0c83-11ea-81c8-d83859200334.png)

## Log files

[debugger.vscode_dbc3a284-d158-4e8b-9cfc-ddd709011d6b.log](https://github.com/microsoft/ptvsd/files/3877489/debugger.vscode_dbc3a284-d158-4e8b-9cfc-ddd709011d6b.log)
[ptvsd.adapter-17808.log](https://github.com/microsoft/ptvsd/files/3877490/ptvsd.adapter-17808.log)
[ptvsd.launcher-18848.log](https://github.com/microsoft/ptvsd/files/3877491/ptvsd.launcher-18848.log)
[ptvsd.server-17816.log](https://github.com/microsoft/ptvsd/files/3877492/ptvsd.server-17816.log)


Discussion:
I believe that this is fundamentally the same problem as #1930, the fix for which has only just went in. Can you try it with ptvsd v5.0.0a8, and see if that fixes it?
Actually, looking at the logs, I don't think even that would help. Not even the parent PID matches what we spawned:
```
I+00001.203: /handling #2 request "launch" from IDE[1]/
             Waiting for connection from debug server with PID=35692...

D+00003.047: Server[?] --> {
                     ...
                     "process": {
                         "pid": 17816,
                         "ppid": 27720,
                         "executable": "C:\\Users\\karraj\\AppData\\Local\\Continuum\\anaconda3\\python.exe",
                         "bitness": 64
                     }
                 }
             }
```

Looking at the source code for `conda run`, it's because it generates a massive shell script, and then uses the shell to run that. So the chain is ptvsd.launcher -> conda -> cmd.exe -> python.exe, where the PID that the launcher has from spawning the process is that of conda, but neither the PID nor the parent PID it gets from python.exe matches that.

So it's the same fundamental problem, but the fix for #1930 won't work. It seems that we'll have to remove PID validation entirely, and just assume that the first connection that comes in is the launched process, regardless of which PID it reports. :/


Resolving patch:
diff --git a/src/ptvsd/adapter/ide.py b/src/ptvsd/adapter/ide.py
--- a/src/ptvsd/adapter/ide.py
+++ b/src/ptvsd/adapter/ide.py
@@ -228,6 +228,9 @@ def handle(self, request):
     def launch_request(self, request):
         from ptvsd.adapter import launchers
 
+        if self.session.id != 1 or len(servers.connections()):
+            raise request.cant_handle('"attach" expected')
+
         sudo = request("sudo", json.default("Sudo" in self.session.debug_options))
         if sudo:
             if sys.platform == "win32":
diff --git a/src/ptvsd/adapter/launchers.py b/src/ptvsd/adapter/launchers.py
--- a/src/ptvsd/adapter/launchers.py
+++ b/src/ptvsd/adapter/launchers.py
@@ -123,14 +123,9 @@ def spawn_launcher():
                 session.launcher,
             )
 
-        # Python can be started via a stub - e.g. py.exe on Windows, which doubles
-        # as python.exe in virtual environments. In this case, the PID of the process
-        # that connects to us won't match the PID of the process that we spawned, but
-        # will have the latter as its parent.
-        pid = session.launcher.pid
-        conn = servers.wait_for_connection(
-            session, (lambda conn: pid in (conn.pid, conn.ppid)), timeout=10
-        )
+        # Wait for the first incoming connection regardless of the PID - it won't
+        # necessarily match due to the use of stubs like py.exe or "conda run".
+        conn = servers.wait_for_connection(session, lambda conn: True, timeout=10)
         if conn is None:
             raise start_request.cant_handle(
                 "{0} timed out waiting for debuggee to spawn", session


Test patch: