Repository: vscode
Issue: Awaiting Terminal processId never returns value on Windows
Labels: bug,api,terminal-process

The PowerShell extension [creates a console using the `vscode.window.createTerminal` API](https://github.com/PowerShell/vscode-powershell/blob/3dff85b849f61b34776517e05fa02297342f6ecc/src/process.ts#L106-L111) and then [tries to log its PID with `terminal.processId`](https://github.com/PowerShell/vscode-powershell/blob/3dff85b849f61b34776517e05fa02297342f6ecc/src/process.ts#L131).

A simplified version of this startup might be something like:

```typescript
const pwshTerminal = vscode.window.createTerminal({
    name: 'PowerShell Console',
    shellPath: 'C:\\Program Files\\PowerShell\\7-preview\\pwsh.exe',
    shellArgs: ['-NonInteractive', '-NoProfile'], // Options suppress things like PSReadLine, so simplify repro
    hideFromUser: false});

pwshTerminal.processId.then((pid) => console.log(pid));
```

In https://github.com/PowerShell/vscode-powershell/pull/2482 I changed our `then()` call to an `async`/`await` version, like:

```typescript
const pwshTerminal = vscode.window.createTerminal({
    name: 'PowerShell Console',
    shellPath: 'C:\\Program Files\\PowerShell\\7-preview\\pwsh.exe',
    shellArgs: ['-NonInteractive', '-NoProfile'], // Options suppress things like PSReadLine, so simplify repro
    hideFromUser: false});

const pid = await pwshTerminal.processId;
console.log(pid);
```

This led to https://github.com/PowerShell/vscode-powershell/issues/2491, where the extension client would not progress past this await call (as discovered with log points).

As I say in https://github.com/PowerShell/vscode-powershell/issues/2491#issuecomment-593500969, I can reproduce this with the following change from https://github.com/PowerShell/vscode-powershell/commit/d431617d6997c75fdce35342323e016f1bbaf271:

```diff
diff --git a/src/process.ts b/src/process.ts
index aa957b5..b70966e 100644
--- a/src/process.ts
+++ b/src/process.ts
@@ -128,7 +128,9 @@ export class PowerShellProcess {

         // Log that the PowerShell terminal process has been started
         this.log.write("Registering terminal PID log callback");
-        this.consoleTerminal.processId.then((pid) => this.logTerminalPid(pid, pwshName));
+        const pid = await this.consoleTerminal.processId;
+        this.logTerminalPid(pid, pwshName);
+        // this.consoleTerminal.processId.then((pid) => this.logTerminalPid(pid, pwshName));

         return sessionDetails;
     }
```

So startup pauses, since the client never opens a named pipe. Debugging the server shows it waiting on a named pipe connection.

In the client, pausing the debugger shows us stopped here:

![pidHang](https://user-images.githubusercontent.com/7009879/75698013-c4693100-5c62-11ea-9da7-c08ee4bb3a05.JPG)

Log points on each line showed us progressing through the startup code as expected until awaiting the `processId` value of the terminal, at which point we stopped progressing.

We worked around this issue https://github.com/PowerShell/vscode-powershell/pull/2498 by moving back to the `.then()` (I suspect there are startups where we never log the PID).

I've only seen this on Windows. My machine's OS version, where I've managed to reproduce:

```
OS Name:                   Microsoft Windows 10 Enterprise Insider Preview
OS Version:                10.0.19535 N/A Build 19535
```

This is running VSCode 1.43.0-insider (user setup).