Skip to content

Commit

Permalink
Handle ptyprocess exit normally
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <zyl.skysniper@gmail.com>
  • Loading branch information
yilunzhang committed Nov 14, 2019
1 parent 379a39c commit eb54df6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class Session {
this.client = client;
this.remoteAddr = remoteAddr;
this.outputBuffer = '';
this.exit = false;
this.ptyProcess = (0, _nodePty.spawn)(shell, [], Object.assign({
name: 'xterm-color',
cols: ptyCols,
Expand All @@ -219,6 +220,10 @@ class Session {
this.ptyProcess.onData(data => {
this.outputBuffer += data;
});
this.ptyProcess.onExit(() => {
this.exit = true;
delete sessions[this.remoteAddr];
});

this.flushSession = () => {
if (this.outputBuffer.length > 0) {
Expand All @@ -237,9 +242,13 @@ class Session {
console.error("Send msg error:", e);
}

this.flushTimeout = setTimeout(this.flushSession, sessionFlushIntervalInUse);
if (!this.exit) {
this.flushTimeout = setTimeout(this.flushSession, sessionFlushIntervalInUse);
}
} else {
this.flushTimeout = setTimeout(this.flushSession, sessionFlushIntervalIdle);
if (!this.exit) {
this.flushTimeout = setTimeout(this.flushSession, sessionFlushIntervalIdle);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nkn-shell-daemon",
"version": "1.0.3",
"version": "1.0.4",
"description": "NKN Shell Daemon",
"main": "nshd.js",
"bin": {
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class Session {
this.client = client
this.remoteAddr = remoteAddr
this.outputBuffer = ''
this.exit = false
this.ptyProcess = spawn(shell, [], Object.assign({
name: 'xterm-color',
cols: ptyCols,
Expand All @@ -208,6 +209,11 @@ class Session {
this.outputBuffer += data
})

this.ptyProcess.onExit(() => {
this.exit = true
delete sessions[this.remoteAddr]
})

this.flushSession = () => {
if (this.outputBuffer.length > 0) {
let res = {
Expand All @@ -221,9 +227,13 @@ class Session {
} catch (e) {
console.error("Send msg error:", e)
}
this.flushTimeout = setTimeout(this.flushSession, sessionFlushIntervalInUse)
if (!this.exit) {
this.flushTimeout = setTimeout(this.flushSession, sessionFlushIntervalInUse)
}
} else {
this.flushTimeout = setTimeout(this.flushSession, sessionFlushIntervalIdle)
if (!this.exit) {
this.flushTimeout = setTimeout(this.flushSession, sessionFlushIntervalIdle)
}
}
}

Expand Down

0 comments on commit eb54df6

Please sign in to comment.