Skip to content

Commit

Permalink
fix: improve process cleanup on socket disconnect and exit
Browse files Browse the repository at this point in the history
  • Loading branch information
purocean committed Jan 23, 2025
1 parent 560f2d0 commit ffa2794
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,18 @@ const server = (port = 3000) => {
env: process.env,
useConpty: false,
})

const kill = () => {
ptyProcess.kill()
}

ptyProcess.onData((data: any) => socket.emit('output', data))
ptyProcess.onExit(() => socket.disconnect())
ptyProcess.onExit(() => {
console.log('ptyProcess exit')
socket.disconnect()
process.off('exit', kill)
})

socket.on('input', (data: any) => {
if (data.startsWith(shell.CD_COMMAND_PREFIX)) {
ptyProcess.write(shell.transformCdCommand(data.toString()))
Expand All @@ -816,7 +826,9 @@ const server = (port = 3000) => {
}
})
socket.on('resize', (size: any) => ptyProcess.resize(size[0], size[1]))
socket.on('disconnect', () => ptyProcess.kill())
socket.on('disconnect', kill)

process.on('exit', kill)
} else {
socket.emit('output', 'node-pty is not compatible with this platform. Please install another version from GitHub https://github.com/purocean/yn/releases')
}
Expand Down

0 comments on commit ffa2794

Please sign in to comment.