Skip to content

Instantly share code, notes, and snippets.

@deepak1556
Created August 6, 2025 11:22
Show Gist options
  • Save deepak1556/60cfc35464d73e94771894e0b7aff3cc to your computer and use it in GitHub Desktop.
Save deepak1556/60cfc35464d73e94771894e0b7aff3cc to your computer and use it in GitHub Desktop.
node-pty write lock on macOS
// Run: node test.js test.py
const pty = require('node-pty');
const fs = require('fs');
const path = require('path');
const net = require('net');
const ptyProcess = pty.spawn('zsh', [], {
name: "xterm-color",
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
ptyProcess.on('data', function (data) {
process.stdout.write(data);
});
let writeTimeout;
let chunks;
let socket;
//const socketPath = '/tmp/my_unix_socket';
//const server = net.createServer();
function chunkInput(data) {
const chunks = [];
let nextChunkStartIndex = 0;
for (let i = 0; i < data.length - 1; i++) {
if (
// If the max chunk size is reached
i - nextChunkStartIndex + 1 >= 50 ||
// If the next character is ESC, send the pending data to avoid splitting the escape
// sequence.
data[i + 1] === '\x1b'
) {
chunks.push(data.substring(nextChunkStartIndex, i + 1));
nextChunkStartIndex = i + 1;
// Skip the next character as the chunk would be a single character
i++;
}
}
// Push final chunk
if (nextChunkStartIndex !== data.length) {
chunks.push(data.substring(nextChunkStartIndex));
}
return chunks;
}
function startWrite() {
// Don't write if it's already queued of is there is nothing to write
if (writeTimeout !== undefined || chunks.length === 0) {
return;
}
//socket.write(Buffer.from(chunks.shift(), 'binary'));
ptyProcess.write(Buffer.from(chunks.shift(), 'binary'));
// Don't queue more writes if the queue is empty
if (chunks.length === 0) {
chunks = undefined;
console.log('All chunks written');
return;
}
// Queue the next write
writeTimeout = setTimeout(() => {
writeTimeout = undefined;
startWrite();
}, 5);
}
function chunkFile(filePath) {
try {
// Read the file with binary encoding
const data = fs.readFileSync(filePath, 'binary');
// Chunk size in bytes
chunks = chunkInput(data);
/*server.listen(socketPath, () => {
console.log(`Server listening on ${socketPath}`);
socket = net.createConnection({ path: socketPath });
socket.setEncoding('utf8');
socket.writable = true;
});
server.on('connection', (socket) => {
console.log('Client connected');
startWrite();
socket.on('data', (data) => {
process.stdout.write(data);
});
socket.on('end', () => {
console.log('Client disconnected');
});
socket.on('error', (err) => {
console.error('Socket error:', err);
});
});*/
startWrite();
} catch (error) {
console.error(`Error reading file: ${error.message}`);
process.exit(1);
}
}
// Get file path from command line arguments
const filePath = process.argv[2];
const resolvedPath = path.resolve(filePath);
chunkFile(resolvedPath);
import time
time.sleep(1)
def longfunction():
'''
Contains 200 print('hello') statements,
each on a new line. Nothing else.
'''
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment