Skip to content

Commit 08c2fd1

Browse files
committed
frontend: Terminal: guard send() when exec/attach socket is unavailable
The Terminal send() path could be invoked before the exec/attach connection was fully initialized, leading to undefined socket access.\n\nThis adds an early guard to safely return when execOrAttachRef is not available.
1 parent bd7b6ab commit 08c2fd1

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

frontend/src/components/common/Terminal.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ export default function Terminal(props: TerminalProps) {
140140
}
141141

142142
function send(channel: number, data: string) {
143-
const socket = execOrAttachRef.current!.getSocket();
143+
if (!execOrAttachRef.current) {
144+
return;
145+
}
146+
const socket = execOrAttachRef.current.getSocket();
144147

145148
// We should only send data if the socket is ready.
146149
if (!socket || socket.readyState !== 1) {
@@ -155,6 +158,7 @@ export default function Terminal(props: TerminalProps) {
155158
}
156159

157160
function onData(xtermc: XTerminalConnected, bytes: ArrayBuffer) {
161+
if (!execOrAttachRef.current) return;
158162
const xterm = xtermc.xterm;
159163
// Only show data from stdout, stderr and server error channel.
160164
const channel: Channel = new Int8Array(bytes.slice(0, 1))[0];
@@ -325,6 +329,7 @@ export default function Terminal(props: TerminalProps) {
325329
return function cleanup() {
326330
xtermRef.current?.xterm.dispose();
327331
execOrAttachRef.current?.cancel();
332+
execOrAttachRef.current = null;
328333
window.removeEventListener('resize', handler);
329334
};
330335
},

0 commit comments

Comments
 (0)