Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple response.WriteHeader calls in really simple example?

Tags:

http

server

go

I have the most basic net/http program that I'm using to learn the namespace in Go:

package main  import (     "fmt"     "log"     "net/http" )  func main() {     http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {         fmt.Println(r.URL)         go HandleIndex(w, r)     })      fmt.Println("Starting Server...")     log.Fatal(http.ListenAndServe(":5678", nil)) }  func HandleIndex(w http.ResponseWriter, r *http.Request) {     w.WriteHeader(200)     w.Write([]byte("Hello, World!")) } 

When I run the program and connect to localhost:5678 in Chrome, I get this in the console:

Starting Server... / 2015/01/15 13:41:29 http: multiple response.WriteHeader calls /favicon.ico 2015/01/15 13:41:29 http: multiple response.WriteHeader calls 

But I don't see how that's possible. I print the URL, start up a new goroutine, write the header once, and give it a static body of Hello, World! It seems like one of two things is happening. Either something behind the scenes is writing another header or somehow HandleIndex is called twice for the same request. What can I do to stop writing multiple headers?

EDIT: It seems to have something to do with the go HandleIndex(w, r) line because if I remove go and just make it a function call instead of a goroutine, I don't get any issues and the browser gets it's data. With it being a goroutine, I get the multiple WriteHeader error and the browser doesn't show "Hello World." Why is making this a goroutine breaking it?

like image 268
Corey Ogburn Avatar asked Jan 15 '15 20:01

Corey Ogburn


Video Answer


1 Answers

Take a look at the anonymous function you register as the handler of incoming requests:

func(w http.ResponseWriter, r *http.Request) {     fmt.Println(r.URL)     go HandleIndex(w, r) } 

It prints the URL (to the standard output) then calls HandleIndex() in a new goroutine and continues execution.

If you have a handler function where you do not set the response status before the first call to Write, Go will automatically set the response status to 200 (HTTP OK). If the handler function does not write anything to the response (and does not set the response status and completes normally), that is also treated as a successful handling of the request and the response status 200 will be sent back. Your anonymous function does not set it, it does not even write anything to the response. So Go will do just that: set the response status to 200 HTTP OK.

Note that handling each request runs in its own goroutine.

So if you call HandleIndex in a new goroutine, your original anonymous function will continue: it will end and so the response header will be set - meanwhile (concurrently) your started new goroutine will also set the response header - hence the "multiple response.WriteHeader calls" error.

If you remove the "go", your HandleIndex function will set the response header in the same goroutine before your handler function returns, and the "net/http" will know about this and will not try to set the response header again, so the error you experienced will not happen.

like image 75
icza Avatar answered Oct 12 '22 01:10

icza