bodyclose: possible false positive #5345
              
                Unanswered
              
          
                  
                    
                      juan-carvajal
                    
                  
                
                  asked this question in
                Q&A
              
            Replies: 1 comment 5 replies
-
| This is the full handler code: type handlerChecker func(t *testing.T, r *http.Request)
func dummySSEHandler(t *testing.T, checks ...handlerChecker) http.Handler {
	t.Helper()
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		for _, check := range checks {
			check(t, r)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		w.Header().Set("Cache-Control", "no-cache")
		w.Header().Set("Connection", "keep-alive")
		w.WriteHeader(http.StatusOK)
		//nolint:bodyclose
		rc := http.NewResponseController(w)
		readFile, err := os.Open("./testdata/streaming-input.txt")
		require.NoError(t, err)
		defer readFile.Close()
		fileScanner := bufio.NewScanner(readFile)
		fileScanner.Split(bufio.ScanLines)
		for fileScanner.Scan() {
			_, err := w.Write([]byte(fileScanner.Text() + "\n\n"))
			require.NoError(t, err)
			err = rc.Flush()
			require.NoError(t, err)
		}
	})
} | 
Beta Was this translation helpful? Give feedback.
                  
                    5 replies
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey, I got a
response body must be closed (bodyclose)inside a handler usinghttp.NewResponseController. I tried closing the body in a defer at the beginning of the handler but the error still shows. In any case, the location that the linter is marking does not seem appropriate, is this a bug?Beta Was this translation helpful? Give feedback.
All reactions