-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest-kill-pty.js
More file actions
executable file
·98 lines (80 loc) · 2.82 KB
/
test-kill-pty.js
File metadata and controls
executable file
·98 lines (80 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
// Simple test to verify PTY process killing works
const pty = require('node-pty');
const fs = require('fs');
const path = require('path');
const testFile = `/tmp/pty-kill-test-${Date.now()}.txt`;
console.log(`Test file: ${testFile}`);
// Spawn a PTY
const ptyProcess = pty.spawn('/bin/bash', [], {
name: 'xterm-256color',
cols: 80,
rows: 30,
cwd: process.cwd(),
env: process.env
});
const pid = ptyProcess.pid;
console.log(`PTY PID: ${pid}`);
// Wait for shell to be ready
setTimeout(() => {
// Start a loop that writes timestamps
ptyProcess.write(`while true; do date +%s.%N > ${testFile}; sleep 0.1; done\r`);
console.log('Started timestamp writing process');
// Wait for process to start writing
setTimeout(() => {
console.log('File exists:', fs.existsSync(testFile));
if (!fs.existsSync(testFile)) {
console.error('Test file was not created!');
ptyProcess.kill('SIGKILL');
process.exit(1);
}
// Read initial content
const initial = fs.readFileSync(testFile, 'utf-8');
console.log('Initial timestamp:', initial.trim());
// Wait a bit
setTimeout(() => {
const updated = fs.readFileSync(testFile, 'utf-8');
console.log('Updated timestamp:', updated.trim());
console.log('File is being updated:', initial !== updated);
// Now try to kill the process group
console.log(`\nAttempting to kill process group -${pid}...`);
try {
process.kill(-pid, 'SIGTERM');
console.log('✓ Sent SIGTERM to process group');
} catch (error) {
console.error('✗ Failed to kill process group:', error.message);
// Fallback to killing PTY process
console.log('Fallback: killing PTY process directly');
ptyProcess.kill('SIGTERM');
}
// Wait and check if process stopped
setTimeout(() => {
const afterKill = fs.readFileSync(testFile, 'utf-8');
console.log('After kill:', afterKill.trim());
setTimeout(() => {
const final = fs.readFileSync(testFile, 'utf-8');
console.log('Final check:', final.trim());
if (afterKill === final) {
console.log('\n✓ SUCCESS: Process was killed (file stopped updating)');
} else {
console.log('\n✗ FAILURE: Process is still running (file still updating)');
// Force kill
console.log('Force killing...');
try {
process.kill(-pid, 'SIGKILL');
} catch (e) {
ptyProcess.kill('SIGKILL');
}
}
// Cleanup
setTimeout(() => {
if (fs.existsSync(testFile)) {
fs.unlinkSync(testFile);
}
process.exit(afterKill === final ? 0 : 1);
}, 500);
}, 500);
}, 1000);
}, 300);
}, 1000);
}, 500);