Skip to content

[BUG] TUI requests not cancelled, causing potential memory leak #522

@coding-shalabh

Description

@coding-shalabh

Description

In the TUI code at pkg/tui/gui/dispatch.go, there's a TODO comment on line 29 indicating that request contexts should be tracked and cancelled when new requests come in. Currently, the code on line 95 uses context.Background() which cannot be cancelled.

Impact

When users rapidly switch between views or make multiple requests, previous pending requests continue to run in the background. This can lead to:

  • Unnecessary memory usage
  • Wasted network bandwidth
  • Potential race conditions when outdated responses arrive

Location

File: pkg/tui/gui/dispatch.go
Lines: 29 (TODO comment), 95 (context creation)

Current Code

// Line 29
// TODO: Should these keep track of the context for pending requests
// and cancel previous ones as new ones come in?

// Line 95
g, _ := errgroup.WithContext(context.Background())

Suggested Fix

Implement context cancellation for pending requests:

type RequestManager struct {
    cancel context.CancelFunc
    mu     sync.Mutex
}

func (rm *RequestManager) NewRequest(ctx context.Context) context.Context {
    rm.mu.Lock()
    defer rm.mu.Unlock()
    
    // Cancel previous request
    if rm.cancel != nil {
        rm.cancel()
    }
    
    // Create new cancellable context
    newCtx, cancel := context.WithCancel(ctx)
    rm.cancel = cancel
    return newCtx
}

Steps to Reproduce

  1. Open the Doppler TUI
  2. Rapidly switch between different views
  3. Monitor network requests - you'll see multiple pending requests

Additional Context

This issue was identified during a security audit of the codebase. While not a security vulnerability, it affects resource management and user experience.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions