88 "encoding/json"
99 "errors"
1010 "fmt"
11- "io"
1211 "net"
1312 "os"
1413 "path/filepath"
@@ -17,7 +16,6 @@ import (
1716 "runtime/trace"
1817 "strings"
1918 "sync"
20- "sync/atomic"
2119 "time"
2220
2321 "github.com/keybase/client/go/chat/globals"
@@ -61,63 +59,20 @@ var (
6159 connMutex sync.Mutex // Protects conn operations
6260)
6361
64- var (
65- connGeneration atomic.Uint64
66- resetGeneration atomic.Uint64
67- jsReadySignalCount atomic.Uint64
68- writeErrCount atomic.Uint64
69- readErrCount atomic.Uint64
70- readErrStreak atomic.Uint64
71- readErrStreakStart atomic.Int64
72- )
73-
7462func describeConn (c net.Conn ) string {
7563 if c == nil {
7664 return "<nil>"
7765 }
7866 return fmt .Sprintf ("%T@%p" , c , c )
7967}
8068
81- func describeErr (err error ) string {
82- if err == nil {
83- return "<nil>"
84- }
85- return fmt .Sprintf ("%T: %v" , err , err )
86- }
87-
8869func appStateForLog () string {
8970 if kbCtx == nil || kbCtx .MobileAppState == nil {
9071 return "<unknown>"
9172 }
9273 return fmt .Sprintf ("%v" , kbCtx .MobileAppState .State ())
9374}
9475
95- func noteReadSuccess (c net.Conn , n int ) {
96- streak := readErrStreak .Swap (0 )
97- startUnix := readErrStreakStart .Swap (0 )
98- if streak == 0 {
99- return
100- }
101- var dur time.Duration
102- if startUnix > 0 {
103- dur = time .Since (time .Unix (0 , startUnix ))
104- }
105- log ("Go: ReadArr recovered after streak=%d conn=%s nextReadBytes=%d appState=%s duration=%s" ,
106- streak , describeConn (c ), n , appStateForLog (), dur )
107- }
108-
109- func noteReadError (c net.Conn , err error ) {
110- total := readErrCount .Add (1 )
111- streak := readErrStreak .Add (1 )
112- if streak == 1 {
113- readErrStreakStart .Store (time .Now ().UnixNano ())
114- }
115- if streak <= 5 || streak == 10 || streak % 50 == 0 {
116- log ("Go: ReadArr error streak=%d total=%d conn=%s appState=%s err=%s eof=%v" ,
117- streak , total , describeConn (c ), appStateForLog (), describeErr (err ), errors .Is (err , io .EOF ))
118- }
119- }
120-
12176// log writes to kbCtx.Log if available, otherwise falls back to stderr.
12277// Stderr is captured in crash logs and the Xcode console, making early Init
12378// messages (before kbCtx.Log is set up by Configure) visible in diagnostics.
@@ -530,9 +485,8 @@ func WriteArr(b []byte) (err error) {
530485
531486 n , err := currentConn .Write (bytes )
532487 if err != nil {
533- total := writeErrCount .Add (1 )
534- log ("Go: WriteArr error total=%d conn=%s len=%d appState=%s err=%s" ,
535- total , describeConn (currentConn ), len (bytes ), appStateForLog (), describeErr (err ))
488+ log ("Go: WriteArr error conn=%s len=%d appState=%s err=%v" ,
489+ describeConn (currentConn ), len (bytes ), appStateForLog (), err )
536490 return fmt .Errorf ("Write error: %s" , err )
537491 }
538492 if n != len (bytes ) {
@@ -573,12 +527,10 @@ func ReadArr() (data []byte, err error) {
573527
574528 n , err := currentConn .Read (buffer )
575529 if n > 0 && err == nil {
576- noteReadSuccess (currentConn , n )
577530 return buffer [0 :n ], nil
578531 }
579532
580533 if err != nil {
581- noteReadError (currentConn , err )
582534 // Attempt to fix the connection
583535 if ierr := Reset (); ierr != nil {
584536 log ("failed to Reset: %v" , ierr )
@@ -592,7 +544,6 @@ func ReadArr() (data []byte, err error) {
592544// ensureConnection establishes the loopback connection if not already connected.
593545// Must be called with connMutex held.
594546func ensureConnection () error {
595- start := time .Now ()
596547 if ! isInited () {
597548 log ("ensureConnection: keybase not initialized" )
598549 return errors .New ("keybase not initialized" )
@@ -603,8 +554,6 @@ func ensureConnection() error {
603554 }
604555
605556 var err error
606- log ("ensureConnection: attempting dial listener=%T@%p existingConn=%s appState=%s" ,
607- kbCtx .LoopbackListener , kbCtx .LoopbackListener , describeConn (conn ), appStateForLog ())
608557 conn , err = kbCtx .LoopbackListener .Dial ()
609558 if err != nil {
610559 // The listener was closed (isClosed=true, returns syscall.EINVAL). Recreate it and
@@ -621,14 +570,12 @@ func ensureConnection() error {
621570 log ("ensureConnection: Dial failed after restart: %v" , err )
622571 return fmt .Errorf ("failed to dial after loopback restart: %s" , err )
623572 }
624- gen := connGeneration .Add (1 )
625- log ("ensureConnection: loopback server restarted successfully in %v gen=%d conn=%s appState=%s" ,
626- time .Since (start ), gen , describeConn (conn ), appStateForLog ())
573+ log ("ensureConnection: loopback server restarted successfully conn=%s appState=%s" ,
574+ describeConn (conn ), appStateForLog ())
627575 return nil
628576 }
629- gen := connGeneration .Add (1 )
630- log ("Go: Established loopback connection in %v gen=%d conn=%s appState=%s" ,
631- time .Since (start ), gen , describeConn (conn ), appStateForLog ())
577+ log ("Go: Established loopback connection conn=%s appState=%s" ,
578+ describeConn (conn ), appStateForLog ())
632579 return nil
633580}
634581
@@ -637,38 +584,30 @@ func Reset() error {
637584 connMutex .Lock ()
638585 defer connMutex .Unlock ()
639586
640- resetID := resetGeneration .Add (1 )
641- log ("Go: Reset #%d start conn=%s appState=%s" , resetID , describeConn (conn ), appStateForLog ())
587+ log ("Go: Reset start conn=%s appState=%s" , describeConn (conn ), appStateForLog ())
642588 if conn != nil {
643589 conn .Close ()
644590 conn = nil
645591 }
646592 if kbCtx == nil || kbCtx .LoopbackListener == nil {
647- log ("Go: Reset #%d complete without listener appState=%s" , resetID , appStateForLog ())
593+ log ("Go: Reset complete without listener appState=%s" , appStateForLog ())
648594 return nil
649595 }
650596
651597 // Connection will be re-established lazily on next read/write
652- log ("Go: Connection reset, will reconnect on next operation (reset=%d appState=%s)" , resetID , appStateForLog ())
598+ log ("Go: Connection reset, will reconnect on next operation appState=%s" , appStateForLog ())
653599 return nil
654600}
655601
656602// NotifyJSReady signals that the JavaScript side is ready to send/receive RPCs.
657603// This unblocks the ReadArr loop and allows bidirectional communication.
658604// jsReadyCh is closed once and stays closed — repeated calls from engine resets are no-ops.
659605func NotifyJSReady () {
660- call := jsReadySignalCount .Add (1 )
661- notified := false
662606 jsReadyOnce .Do (func () {
663- notified = true
664- log ("Go: JS signaled ready, unblocking RPC communication (call=%d appState=%s conn=%s)" ,
665- call , appStateForLog (), describeConn (conn ))
607+ log ("Go: JS signaled ready, unblocking RPC communication appState=%s conn=%s" ,
608+ appStateForLog (), describeConn (conn ))
666609 close (jsReadyCh )
667610 })
668- if ! notified {
669- log ("Go: NotifyJSReady called again (no-op, channel already closed — engine reset?) call=%d appState=%s conn=%s" ,
670- call , appStateForLog (), describeConn (conn ))
671- }
672611}
673612
674613// ForceGC Forces a gc
0 commit comments