Go, HTTP handlers, panic, and deadlocks
Maybe the scenario I'm going to describe is just a silly bug no seasoned Go developer would ever make, but it is what it is.
I'm not an expert in Go but I do write code in this language from time to time. My cumulative number of LOC is probably still below 100 000 but it's definitely not just a few hundred lines of code. Go always looked like a simple language to me. But also it looked safe. Apparently, it's not as simple and safe as I've thought...
Here is a synthetic piece of code illustrating the erroneous logic I stumbled upon recently:
// main.go
package main
import (
"fmt"
"sync"
)
func main() {
mutex := &sync.Mutex{}
f := func() {
fmt.Println("In f()")
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered", r)
}
}()
dogs := []string{"Lucky"}
mutex.Lock()
fmt.Println("Last dog's name is", dogs[len(dogs)])
mutex.Unlock()
}
f()
fmt.Println("About to get a deadlock in main()")
mutex.Lock()
}