Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(shell): guard resizeSession against closed PTY master (#12070)
Adam Eivy4c2f9014a4f0
diff --git a/server/services/shell.js b/server/services/shell.jsindex 370bc3e35d..ec72a6975e 100644--- a/server/services/shell.js+++ b/server/services/shell.js@@ -808,8 +808,13 @@ export function getLastInputAt(sessionId) {export function resizeSession(sessionId, cols, rows) {const session = shellSessions.get(sessionId);if (session) {- session.pty.resize(cols, rows);- return true;+ try {+ session.pty.resize(cols, rows);+ return true;+ } catch (err) {+ console.warn(`🐚 Failed to resize shell session ${sessionId}: ${err.message}`);+ return false;+ }}return false;}diff --git a/server/services/shell.test.js b/server/services/shell.test.jsindex a3e19a5ad8..5d827983ac 100644--- a/server/services/shell.test.js+++ b/server/services/shell.test.js@@ -458,6 +458,14 @@ describe('writeToSession / resizeSession', () => {it('resizeSession returns false for missing session', () => {expect(shell.resizeSession('missing', 80, 24)).toBe(false);});++ it('resizeSession returns false without throwing when pty.resize throws', () => {+ const id = shell.createShellSession(makeSocket());+ ptyInstances[0].resize.mockImplementationOnce(() => {+ throw new Error('ioctl(2) failed, EBADF');+ });+ expect(shell.resizeSession(id, 80, 24)).toBe(false);+ });});describe('changeSessionDirectory', () => {diff --git a/server/sockets/shell.js b/server/sockets/shell.jsindex 038651a634..e336831168 100644--- a/server/sockets/shell.js+++ b/server/sockets/shell.js@@ -94,7 +94,9 @@ export const registerShellHandlers = (socket, _io) => {socket.on('shell:resize', (rawData) => {const validated = validateSocketData(shellResizeSchema, rawData, socket, 'shell:resize');if (!validated) return;- shellService.resizeSession(validated.sessionId, validated.cols, validated.rows);+ if (!shellService.resizeSession(validated.sessionId, validated.cols, validated.rows)) {+ socket.emit('shell:error', { sessionId: validated.sessionId, error: 'Session not found or resize failed' });+ }});socket.on('shell:stop', (rawData) => {diff --git a/server/sockets/shell.test.js b/server/sockets/shell.test.jsindex 1ecc81fbf7..7d561721f3 100644--- a/server/sockets/shell.test.js+++ b/server/sockets/shell.test.js@@ -1,8 +1,10 @@import { describe, it, expect, vi, beforeEach } from 'vitest';const createShellSession = vi.fn(() => 'sess-1');+const resizeSession = vi.fn(() => true);vi.mock('../services/shell.js', () => ({createShellSession,+ resizeSession,submitToSession: vi.fn(),attachSession: vi.fn(),detachSocketSessions: vi.fn()@@ -35,3 +37,26 @@ describe('shell:start payload validation', () => {expect(socket.emit).toHaveBeenCalledWith('shell:started', { sessionId: 'sess-1' });});});++describe('shell:resize handler', () => {+ beforeEach(() => resizeSession.mockClear());++ it('resizes session when resizeSession succeeds', () => {+ resizeSession.mockReturnValueOnce(true);+ const { handlers, socket } = setup();+ handlers['shell:resize']({ sessionId: 'sess-1', cols: 100, rows: 30 });+ expect(resizeSession).toHaveBeenCalledWith('sess-1', 100, 30);+ expect(socket.emit).not.toHaveBeenCalledWith('shell:error', expect.anything());+ });++ it('emits shell:error when resizeSession returns false', () => {+ resizeSession.mockReturnValueOnce(false);+ const { handlers, socket } = setup();+ handlers['shell:resize']({ sessionId: 'sess-1', cols: 100, rows: 30 });+ expect(resizeSession).toHaveBeenCalledWith('sess-1', 100, 30);+ expect(socket.emit).toHaveBeenCalledWith('shell:error', {+ sessionId: 'sess-1',+ error: 'Session not found or resize failed'+ });+ });+});