From 17dc1b0be19e4a0b0efaff87d7db296056e43d18 Mon Sep 17 00:00:00 2001 From: Owen Date: Sat, 17 Jan 2026 17:32:01 -0800 Subject: [PATCH] Dont start the ping until we are connected Former-commit-id: 43c8a14fda9d8f09cb8e8b31ff973a5f124979d1 --- olm/connect.go | 3 +++ olm/olm.go | 2 ++ websocket/client.go | 26 ++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/olm/connect.go b/olm/connect.go index a610ea4..7f3785e 100644 --- a/olm/connect.go +++ b/olm/connect.go @@ -198,6 +198,9 @@ func (o *Olm) handleConnect(msg websocket.WSMessage) { o.connected = true + // Start ping monitor now that we are registered and connected + o.websocket.StartPingMonitor() + // Invoke onConnected callback if configured if o.olmConfig.OnConnected != nil { go o.olmConfig.OnConnected() diff --git a/olm/olm.go b/olm/olm.go index f6e1980..b2df734 100644 --- a/olm/olm.go +++ b/olm/olm.go @@ -362,6 +362,8 @@ func (o *Olm) StartTunnel(config TunnelConfig) { if o.connected { logger.Debug("Already connected, skipping registration") + // Restart ping monitor on reconnect since the old one would have exited + o.websocket.StartPingMonitor() return nil } diff --git a/websocket/client.go b/websocket/client.go index d0ac73b..844bde3 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -100,6 +100,8 @@ type Client struct { processingMux sync.RWMutex // Protects processingMessage processingWg sync.WaitGroup // WaitGroup to wait for message processing to complete getPingData func() map[string]any // Callback to get additional ping data + pingStarted bool // Flag to track if ping monitor has been started + pingStartedMux sync.Mutex // Protects pingStarted } type ClientOption func(*Client) @@ -575,8 +577,14 @@ func (c *Client) establishConnection() error { c.conn = conn c.setConnected(true) - // Start the ping monitor - go c.pingMonitor() + // Reset ping started flag on new connection + c.pingStartedMux.Lock() + c.pingStarted = false + c.pingStartedMux.Unlock() + + // Note: ping monitor is NOT started here - it will be started when + // StartPingMonitor() is called after registration completes + // Start the read pump with disconnect detection go c.readPumpWithDisconnectDetection() @@ -715,6 +723,20 @@ func (c *Client) pingMonitor() { } } +// StartPingMonitor starts the ping monitor goroutine. +// This should be called after the client is registered and connected. +// It is safe to call multiple times - only the first call will start the monitor. +func (c *Client) StartPingMonitor() { + c.pingStartedMux.Lock() + defer c.pingStartedMux.Unlock() + + if c.pingStarted { + return + } + c.pingStarted = true + go c.pingMonitor() +} + // GetConfigVersion returns the current config version func (c *Client) GetConfigVersion() int { c.configVersionMux.RLock()