fix: session tab I/O — stdin direct write, pollSession triggers re-render

This commit is contained in:
swanadiva
2026-06-23 14:59:48 +07:00
parent 98dfc1c79c
commit 809ac1d2e8
+32 -46
View File
@@ -3,6 +3,7 @@ package tui
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -32,7 +33,8 @@ type SessionTab struct {
done bool done bool
err error err error
stdinPipe chan []byte stdinPipe io.WriteCloser
stdinMu sync.Mutex
outputCh chan sessionOutputMsg outputCh chan sessionOutputMsg
doneCh chan sessionDoneMsg doneCh chan sessionDoneMsg
windowCh chan struct{} windowCh chan struct{}
@@ -50,11 +52,10 @@ func NewSessionTab(host *models.Host, dataDir string) *SessionTab {
width: 80, width: 80,
height: 24, height: 24,
stdinPipe: make(chan []byte, 256), outputCh: make(chan sessionOutputMsg, 256),
outputCh: make(chan sessionOutputMsg, 64), doneCh: make(chan sessionDoneMsg, 1),
doneCh: make(chan sessionDoneMsg, 1), windowCh: make(chan struct{}, 8),
windowCh: make(chan struct{}, 8), closed: make(chan struct{}),
closed: make(chan struct{}),
} }
} }
@@ -72,14 +73,15 @@ func (t *SessionTab) Init() tea.Cmd {
return t.pollSession return t.pollSession
} }
// pollSession polls for session output and done signals // pollSession polls for session output and returns it as a message to trigger re-render
func (t *SessionTab) pollSession() tea.Msg { func (t *SessionTab) pollSession() tea.Msg {
select { select {
case msg := <-t.outputCh: case msg := <-t.outputCh:
t.mu.Lock() t.mu.Lock()
t.buffer.WriteString(string(msg)) t.buffer.WriteString(string(msg))
t.mu.Unlock() t.mu.Unlock()
return nil // Return the msg to trigger Update → View re-render
return msg
case done := <-t.doneCh: case done := <-t.doneCh:
return done return done
default: default:
@@ -89,7 +91,6 @@ func (t *SessionTab) pollSession() tea.Msg {
// connectAndStream connects to the host and streams output // connectAndStream connects to the host and streams output
func (t *SessionTab) connectAndStream(ctx context.Context) { func (t *SessionTab) connectAndStream(ctx context.Context) {
// Create SSH client
timeout := 30 * time.Second timeout := 30 * time.Second
client := sshclient.NewClient(t.host, timeout) client := sshclient.NewClient(t.host, timeout)
@@ -103,7 +104,6 @@ func (t *SessionTab) connectAndStream(ctx context.Context) {
t.connected = true t.connected = true
t.mu.Unlock() t.mu.Unlock()
// Create session
sshClient := client.GetClient() sshClient := client.GetClient()
sess, err := sshClient.NewSession() sess, err := sshClient.NewSession()
if err != nil { if err != nil {
@@ -119,7 +119,6 @@ func (t *SessionTab) connectAndStream(ctx context.Context) {
width := t.width width := t.width
height := t.height height := t.height
// Request PTY
modes := cryptossh.TerminalModes{ modes := cryptossh.TerminalModes{
cryptossh.ECHO: 1, cryptossh.ECHO: 1,
cryptossh.TTY_OP_ISPEED: 14400, cryptossh.TTY_OP_ISPEED: 14400,
@@ -132,13 +131,13 @@ func (t *SessionTab) connectAndStream(ctx context.Context) {
return return
} }
// Set up pipes
stdin, err := sess.StdinPipe() stdin, err := sess.StdinPipe()
if err != nil { if err != nil {
client.Close() client.Close()
t.doneCh <- sessionDoneMsg{err: fmt.Errorf("stdin pipe failed: %w", err)} t.doneCh <- sessionDoneMsg{err: fmt.Errorf("stdin pipe failed: %w", err)}
return return
} }
t.stdinPipe = stdin
stdout, err := sess.StdoutPipe() stdout, err := sess.StdoutPipe()
if err != nil { if err != nil {
@@ -154,17 +153,14 @@ func (t *SessionTab) connectAndStream(ctx context.Context) {
return return
} }
// Start shell
if err := sess.Shell(); err != nil { if err := sess.Shell(); err != nil {
client.Close() client.Close()
t.doneCh <- sessionDoneMsg{err: fmt.Errorf("shell start failed: %w", err)} t.doneCh <- sessionDoneMsg{err: fmt.Errorf("shell start failed: %w", err)}
return return
} }
// Read stdout in a goroutine // Stream stdout
stdoutDone := make(chan struct{})
go func() { go func() {
defer close(stdoutDone)
buf := make([]byte, 4096) buf := make([]byte, 4096)
for { for {
n, err := stdout.Read(buf) n, err := stdout.Read(buf)
@@ -180,10 +176,8 @@ func (t *SessionTab) connectAndStream(ctx context.Context) {
} }
}() }()
// Read stderr in a goroutine // Stream stderr
stderrDone := make(chan struct{})
go func() { go func() {
defer close(stderrDone)
buf := make([]byte, 4096) buf := make([]byte, 4096)
for { for {
n, err := stderr.Read(buf) n, err := stderr.Read(buf)
@@ -195,22 +189,19 @@ func (t *SessionTab) connectAndStream(ctx context.Context) {
} }
if err != nil { if err != nil {
return return
} }
} }
}() }()
// Write stdin (from channel to SSH pipe) // Handle window resize
stdinDone := make(chan struct{})
go func() { go func() {
defer close(stdinDone)
for { for {
select { select {
case data := <-t.stdinPipe:
stdin.Write(data)
case <-t.windowCh: case <-t.windowCh:
t.mu.Lock() t.mu.Lock()
sess.WindowChange(t.height, t.width) w, h := t.width, t.height
t.mu.Unlock() t.mu.Unlock()
sess.WindowChange(h, w)
case <-t.closed: case <-t.closed:
return return
case <-ctx.Done(): case <-ctx.Done():
@@ -219,16 +210,8 @@ func (t *SessionTab) connectAndStream(ctx context.Context) {
} }
}() }()
// Wait for session to finish
sess.Wait() sess.Wait()
// Cleanup
close(stdinDone)
<-stdoutDone
<-stderrDone
client.Close() client.Close()
t.doneCh <- sessionDoneMsg{err: nil} t.doneCh <- sessionDoneMsg{err: nil}
} }
@@ -242,7 +225,7 @@ func (t *SessionTab) Update(msg tea.Msg) (Tab, tea.Cmd) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
t.mu.Lock() t.mu.Lock()
t.width = msg.Width t.width = msg.Width
t.height = msg.Height - 1 // account for status bar t.height = msg.Height
t.mu.Unlock() t.mu.Unlock()
select { select {
case t.windowCh <- struct{}{}: case t.windowCh <- struct{}{}:
@@ -250,15 +233,19 @@ func (t *SessionTab) Update(msg tea.Msg) (Tab, tea.Cmd) {
} }
case tea.KeyMsg: case tea.KeyMsg:
// Send all key input to SSH stdin
data := keyMsgToBytes(msg) data := keyMsgToBytes(msg)
if len(data) > 0 { if len(data) > 0 {
select { t.stdinMu.Lock()
case t.stdinPipe <- data: if t.stdinPipe != nil {
default: t.stdinPipe.Write(data)
} }
t.stdinMu.Unlock()
} }
case sessionOutputMsg:
// Buffer already updated in pollSession, but we return pollSession
// to keep the polling loop alive
case sessionDoneMsg: case sessionDoneMsg:
t.mu.Lock() t.mu.Lock()
t.done = true t.done = true
@@ -277,7 +264,8 @@ func (t *SessionTab) View() string {
defer t.mu.Unlock() defer t.mu.Unlock()
if !t.connected { if !t.connected {
return HighlightStyle.Render(fmt.Sprintf("Connecting to %s...", t.host.Name)) return lipgloss.NewStyle().Height(t.height - 2).Render(
HighlightStyle.Render(fmt.Sprintf("Connecting to %s...", t.host.Name)))
} }
if t.done && t.err != nil { if t.done && t.err != nil {
@@ -294,18 +282,18 @@ func (t *SessionTab) View() string {
content := t.buffer.String() content := t.buffer.String()
if content == "" { if content == "" {
return "Connected. Waiting for output..." return lipgloss.NewStyle().Height(t.height - 2).Render(
SubtitleStyle.Render("Connected. Waiting for output..."))
} }
// Only show last N lines to avoid unbounded memory
lines := strings.Split(content, "\n") lines := strings.Split(content, "\n")
const maxLines = 500 const maxLines = 1000
if len(lines) > maxLines { if len(lines) > maxLines {
lines = lines[len(lines)-maxLines:] lines = lines[len(lines)-maxLines:]
} }
visible := strings.Join(lines, "\n") visible := strings.Join(lines, "\n")
return lipgloss.NewStyle().MaxHeight(t.height - 2).Render(visible) return lipgloss.NewStyle().Height(t.height - 3).Render(visible)
} }
// Close terminates the session // Close terminates the session
@@ -364,5 +352,3 @@ func keyMsgToBytes(msg tea.KeyMsg) []byte {
return []byte(msg.String()) return []byte(msg.String())
} }
} }