node-pty icon indicating copy to clipboard operation
node-pty copied to clipboard

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

Open Luluno01 opened this issue 5 years ago • 4 comments

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).

Luluno01 avatar Jul 20 '19 06:07 Luluno01

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

image

viceice avatar Sep 03 '19 06:09 viceice

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

gpetrov avatar Mar 04 '20 10:03 gpetrov

@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).

Luluno01 avatar Mar 04 '20 11:03 Luluno01

@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

gpetrov avatar Mar 04 '20 11:03 gpetrov