Skip to content

Commit

Permalink
lib: Fix race condition in Terminal size calculation
Browse files Browse the repository at this point in the history
On initializing the page there can be a short time window when the
terminal's CSS cell size is already set, but the parent element's
`client{Width,Height}` are still zero. In that case,
`calculateDimensions()` returned a negative rows/cols, which caused a
bridge crash:

```
fcntl.ioctl(self._pty_fd, termios.TIOCSWINSZ, struct.pack('2H4x', size.rows, size.cols))
struct.error: 'H' format requires 0 <= number <= 65535
```

Avoid that by restricting the computed rows/cols to be at least 1.
  • Loading branch information
martinpitt committed Aug 14, 2024
1 parent d20d475 commit 01357bd
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/lib/cockpit-components-terminal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,13 @@ export class Terminal extends React.Component {
const padding = 10; // Leave a bit of space around terminal
const realHeight = this.terminal._core._renderService.dimensions.css.cell.height;
const realWidth = this.terminal._core._renderService.dimensions.css.cell.width;
const parentHeight = this.terminalRef.current.parentElement.clientHeight;
const parentWidth = this.terminalRef.current.parentElement.clientWidth;
if (realHeight && realWidth && realWidth !== 0 && realHeight !== 0)
return {
rows: Math.floor((this.terminalRef.current.parentElement.clientHeight - padding) / realHeight),
cols: Math.floor((this.terminalRef.current.parentElement.clientWidth - padding - 12) / realWidth) // Remove 12px for scrollbar
// it can happen that parent{Width,Height} are not yet initialized (0), avoid negative values
rows: Math.max(Math.floor((parentHeight - padding) / realHeight), 1),
cols: Math.max(Math.floor((parentWidth - padding - 12) / realWidth), 1) // Remove 12px for scrollbar
};

return { rows: this.state.rows, cols: this.state.cols };
Expand Down

0 comments on commit 01357bd

Please sign in to comment.