Skip to content

Conversation

pranavkonde
Copy link

@pranavkonde pranavkonde commented Jun 2, 2025

Improve Resource Management in scanIterator

Problem

  • Potential resource leaks when context is cancelled during operation
  • Race conditions possible due to unordered channel closing
  • Inadequate error propagation from worker goroutines

Solution

  • Added error channel for proper worker error handling
  • Implemented ordered cleanup sequence in Close() method
  • Enhanced context cancellation handling
  • Improved worker synchronization

Changes

type scanIterator struct {
    // ... existing fields ...
    errChan   chan error    // New error channel
}

func (s *scanIterator) Close() error {
    s.closeOnce.Do(func() {
        if s.cancel != nil {
            s.cancel()
        }
        s.doneWG.Wait()
        close(s.errChan)
        close(s.resultChan)
    })
    return nil
}

Testing

  • Verified with context cancellation scenarios
  • Tested concurrent operation cases
  • Checked error propagation paths

Checklist

  • Code follows project guidelines
  • Changes are focused and atomic
  • Manually tested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant