From 0c5a8a2f04195fdddb9e3a5d4b8123bccdf70bce Mon Sep 17 00:00:00 2001 From: Bernhard Guillon Date: Tue, 7 Jul 2026 12:35:08 +0200 Subject: Add robust error handling for TCP communication Windows side: - sendManifest/sendEvent now return errors - Callers log errors and reconnect on failure Linux side: - 30s read timeout to detect dead connections - Better batch counting and logging - Proper error logging on disconnect --- cmd_linux.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'cmd_linux.go') diff --git a/cmd_linux.go b/cmd_linux.go index b80b197..d937000 100644 --- a/cmd_linux.go +++ b/cmd_linux.go @@ -87,11 +87,19 @@ func run() { func handleClient(conn net.Conn, stack *GitignoreStack) { defer conn.Close() + remoteManifest := &Manifest{Files: make(map[string]FileMeta)} + batches := 0 + scanner := bufio.NewScanner(conn) scanner.Buffer(make([]byte, 0, 1024*1024), 1024*1024) // 1MB buffer per batch - remoteManifest := &Manifest{Files: make(map[string]FileMeta)} - for scanner.Scan() { + for { + conn.SetReadDeadline(time.Now().Add(30 * time.Second)) + if !scanner.Scan() { + break + } + conn.SetReadDeadline(time.Time{}) // reset deadline + msg, err := DecodeMessage(scanner.Bytes()) if err != nil { logDebug("bad message: %v", err) @@ -108,10 +116,11 @@ func handleClient(conn net.Conn, stack *GitignoreStack) { Symlink: v.Symlink, } } - logDebug("manifest: received batch (%d files, total: %d)", len(msg.Files), len(remoteManifest.Files)) + batches++ + logDebug("manifest: batch %d received (%d files, total: %d)", batches, len(msg.Files), len(remoteManifest.Files)) case "manifest_done": - logInfo("manifest: received %d files from host", len(remoteManifest.Files)) + logInfo("manifest: received %d files in %d batches from host", len(remoteManifest.Files), batches) logInfo("manifest: comparing with local state ...") syncFromManifest(remoteManifest, stack) logInfo("sync: done") @@ -149,7 +158,7 @@ func handleClient(conn net.Conn, stack *GitignoreStack) { } if err := scanner.Err(); err != nil { - logErrorf("read: %v", err) + logErrorf("client read: %v", err) } } -- cgit v1.2.3