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

Parent processes (winpty-agent.exe, etc.) do not exit when shell process exits by itself #333

Open
Luluno01 opened this issue Jul 20, 2019 · 5 comments
Labels
bug Issue identified by VS Code Team member as probable bug *out-of-scope Posted issue is not in scope of VS Code windows

Comments

@Luluno01
Copy link

Environment details

  • OS: Windows 10 Pro 64bit
  • OS version: Windows 10 Pro 64bit Version 1809 (OS Build 17763.615)
  • node-pty version: 0.9.0-beta19

Issue description

If a shell process suicides, its parent processes (not including the main node.js process) will not exit automatically unless pty.kill() is called. Check the following issue reproducing code:

import { spawn } from 'node-pty'
import * as os from 'os'


function getUserHome() {
  return process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME']
}

function getShell() {
  switch (os.platform()) {
    case 'win32': return process.env.ComSpec || 'cmd.exe'
    case 'android': return '/bin/sh'
    default: return '/bin/bash'
  }
}

function createProcess({ cols, rows }: { cols?: number, rows?: number } = { cols: 80, rows: 30 }) {
  const shell = getShell()
  return spawn(shell, [], {
    name: 'xterm-color',
    cols: cols || 80,
    rows: rows || 30,
    cwd: getUserHome(),
    env: process.env as { [key: string]: string },
    experimentalUseConpty: true
  })
}

function sleep(n): Promise<void> {
  return new Promise(res => setTimeout(res, n))
}

async function main() {
  await sleep(10 * 1000)
  for(let i = 0; i < 10; i++) {
    const pty = createProcess()
    console.log(pty.pid)
    await sleep(1000)
    pty.write('exit\r')
  }
  await sleep(60 * 1000)
}

main()

The code above creates 10 winpty-agent.exes and 10 conhost.exe. However, node-pty has notified user that those shell processes has exited by emitting exit event, misleading users that the process has exited cleanly.

It seems that there is no graceful way to determine whether the process is killed by calling pty.kill() or exited by itself (and therefore we should call pty.kill() inside the onExit handler). However, invoking pty.kill() twice on the same process will raise error (can be caught though).

@viceice
Copy link

viceice commented Sep 3, 2019

Seeing this since i use vscode remote dev feature using docker container:

image

@Tyriar Tyriar added bug Issue identified by VS Code Team member as probable bug windows labels Sep 5, 2019
@Luluno01 Luluno01 changed the title Parent processes (winpty-agent.exe, etc.) does not exit when shell process exits by itself Parent processes (winpty-agent.exe, etc.) do not exit when shell process exits by itself Sep 9, 2019
@gpetrov
Copy link

gpetrov commented Mar 4, 2020

@Tyriar any news in addressing this issue? We end up with hundreds of winpty-agent.exe processes ...

@Luluno01
Copy link
Author

Luluno01 commented Mar 4, 2020

@Tyriar any news in addressing this issue? We end up with hundreds of winpty-agent.exe processes ...

@gpetrov Are you using node-pty itself or VSCode? If you are using node-pty and have control on the PTY object, you can always call pty.kill() in onExit handler (note that pty.kill will throw an error if the group of processes has already been killed).

@gpetrov
Copy link

gpetrov commented Mar 4, 2020

@Luluno01 Yes I went indeed for pty.kill() onExit for now - this helps indeed. It just didn't seem like a neat solution but it does the job for now.

And no we have a different project using node-pty and not VSCode

@Tyriar
Copy link
Member

Tyriar commented Dec 17, 2024

winpty support is legacy at this point and is only sticking around until the conpty dll code is ready to turn on by default.

@Tyriar Tyriar added the *out-of-scope Posted issue is not in scope of VS Code label Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug *out-of-scope Posted issue is not in scope of VS Code windows
Projects
None yet
Development

No branches or pull requests

4 participants