Skip to content

[MEDIUM] Goroutine Leak in WebSocket Connection #5

@MacJediWizard

Description

@MacJediWizard

Description

The ping loop goroutine in WebSocket connections and the update check goroutine after report have no mechanism to stop when the connection closes or parent context is cancelled.

Locations

WebSocket ping loop

`cmd/patchmon-agent/commands/serve.go` lines 372-379

go func() {
    t := time.NewTicker(30 * time.Second)
    defer t.Stop()
    for range t.C {
        _ = conn.WriteControl(websocket.PingMessage, nil, time.Now().Add(5*time.Second))
    }
}()

Post-report update check

`cmd/patchmon-agent/commands/report.go` lines 247-280

go func() {
    time.Sleep(5 * time.Second)
    // ... update check logic with no cancellation mechanism
}()

Impact

  • Goroutines accumulate over time, causing memory leaks
  • Resources not properly released when connections close

Recommended Fix

Pass context to goroutines and check for cancellation:

go func(ctx context.Context) {
    t := time.NewTicker(30 * time.Second)
    defer t.Stop()
    for {
        select {
        case <-ctx.Done():
            return
        case <-t.C:
            if err := conn.WriteControl(websocket.PingMessage, nil, time.Now().Add(5*time.Second)); err != nil {
                return
            }
        }
    }
}(ctx)

Severity

🟡 MEDIUM - Resource leak

Labels

bug, medium, code-quality

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcode-qualityCode qualitymediumMedium priority

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions