Open jtackaberry opened 2 months ago
I noticed this too, and it's fairly reproducible - about 50% of the times I start air
and so some HTTP request to the proxy port while it's starting up, I'll get this error.
the traceback points to this bit of code:
defer resp.Body.Close()
after adding some printf
, it's because resp
is nil
by the time this defer runs. I assumed it was the resp
getting cleaned up somehow before the defer
runs, but just fixing the defer
by making it a nested function that checks resp
and resp.Body
aren't nil
reveals there's a similar issue below. I think the error handling above isn't quite right. it will loop 10 times, with a 100ms wait between each, and after that it will proceed, even if it received (a wrapped) ECONNREFUSED error, thus resp
can be nil
when we fall off the end of the for loop and proceed in to the "happy case".
so! what to do? two obvious options:
first: increase the timeout or retry count. currently it only gives your service 1000ms (1s) to respond to queries or you get the panic. or at least make one or the other configurable. or use exponential-capped backoff to get a longer total wait when it's slow.
second: check if resp == nil
after the for loop, and then do something other than panic. return an error to the client? I suspect this is a bit complex with the reload JS - if we return a blank error page then that's the end of automatic reloads until a manual one happens. something like this:
if resp == nil {
http.Error(w, "proxy handler: unable to reach app", http.StatusInternalServerError)
return
}
just gets you an error page in the browser that can't automatically reload. returning a custom HTML page that just embeds the reload logic to start the process over again might work but is maybe a bit too cute.
This issue seems to have been fixed by #635 that was released in 1.61.0.
Given the following configuration using v1.52.3
I sometimes observe the following panic: