Skip to content

Critical: Resource leak - Request body not closed #31

@sgaunet

Description

@sgaunet

Description

The HTTP request body is never closed, causing a resource leak that will accumulate over time on long-running servers.

Location

http-echo.go:66

body, _ := io.ReadAll(r.Body)

Impact

  • Severity: HIGH
  • Memory leaks on long-running servers
  • Connection pool exhaustion
  • Degraded performance under load
  • Potential server crashes in production

Root Cause

The r.Body is an io.ReadCloser that must be explicitly closed. Failure to close it prevents garbage collection of underlying resources.

Recommended Fix

Add a deferred close immediately after entering the function:

func (h helloWorldhandler) collectRequestInfo(r *http.Request, startTime time.Time) requestInfo {
    defer r.Body.Close() // Add this line
    
    // Parse form data
    _ = r.ParseForm()
    
    // Read body
    body, _ := io.ReadAll(r.Body)
    // ... rest of function
}

Testing

  • Run the server under load with memory profiling
  • Verify that memory usage stabilizes after the fix
  • Check for resource leaks using pprof

Priority

Critical - Should be fixed before next release as this affects server stability.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions