简体   繁体   English

使用 pty / process.stdin / process.stdout 后无法让节点完全退出

[英]Can't get node to exit fully after using pty / process.stdin / process.stdout

I'm struggling to get the behavior I'd like from node and node-pty.我正在努力从 node 和 node-pty 获得我想要的行为。 Node doesn't seem to fully exit after executing this code.执行此代码后,节点似乎没有完全退出。 Looking for hints on what I'm missing.寻找关于我所缺少的东西的提示。 I've distilled things down to a small example to demonstrate.我已经将事情提炼成一个小例子来演示。 When you execute the code, it will spawn /bin/sh.当你执行代码时,它会产生 /bin/sh。 Once you exit that shell, it will spawn a second /bin/sh (in the same manner).退出 shell 后,它将生成第二个 /bin/sh(以相同的方式)。 Once you exit that shell, node hangs.一旦退出 shell,节点就会挂起。

let pty = require('node-pty');

main();

async function main() {
    let ptyProcess;

    console.log('Starting session 1');
    ptyProcess = pty.spawn('/bin/sh');
    await interact(ptyProcess);
    ptyProcess.kill();

    console.log('Starting session 2');
    ptyProcess = pty.spawn('/bin/sh');
    await interact(ptyProcess);
    ptyProcess.kill();

    console.log('Why won\'t I exit?');
}

function interact(ptyProcess) { // Allow user interaction from here on out
    return new Promise((resolve, reject) => {

        process.stdout.on('resize', () => {
            ptyProcess.resize(process.stdout.columns, process.stdout.rows);
        });

        process.stdin.on('data', data => {
            ptyProcess.write(data);
        });

        ptyProcess.on('data', data => {
            process.stdout.write(data);
        });

        ptyProcess.on('close', () => {
            console.log('Closing...');
            resolve();
        })
    });
}

Example session:示例 session:

MacBook-Pro:src mtwomey$ node demo.js
Starting session 1
sh-3.2$ exit
exit
exit
Closing...
Starting session 2
sh-3.2$ exit
exit
exit
Closing...
Why won't I exit?


[node remains running / open here]

It turns out this is not at all related to node-pty.事实证明,这与 node-pty 完全无关。 It's straight process.stdin.这是直接的 process.stdin。

process.stdin.unref() is what I needed. process.stdin.unref() 是我需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM