Skip to content

Instantly share code, notes, and snippets.

@sdqali
Last active August 29, 2015 13:56
Show Gist options
  • Save sdqali/9284432 to your computer and use it in GitHub Desktop.
Save sdqali/9284432 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
func main() {
var err error
db, err := sql.Open("sqlite3", "./comments.db")
if err != nil {
panic(err)
}
defer db.Close()
router := mux.NewRouter()
router.HandleFunc("/comments/{domain}/{path}", createComment).Methods("POST")
http.Handle("/", router)
log.Println("Listening...")
http.ListenAndServe(":7654", nil)
}
func createComment(writer http.ResponseWriter, request *http.Request) {
query := `
INSERT INTO comments(domain, path, body) VALUES ("foo.com", "/bar", "some text");
`
_, err := db.Exec(query)
if err != nil {
log.Printf("%q", err)
return
}
}
2014/02/28 19:01:07 http: panic serving [::1]:56399: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.func·007()
/usr/local/Cellar/go/1.1.2/src/pkg/net/http/server.go:1022 +0xac
sync/atomic.CompareAndSwapUint32()
/usr/local/Cellar/go/1.1.2/src/pkg/sync/atomic/asm_amd64.s:14 +0xd
sync.(*Mutex).Lock(0x20)
/usr/local/Cellar/go/1.1.2/src/pkg/sync/mutex.go:43 +0x35
database/sql.(*DB).conn(0x0, 0xc2000b8be0, 0xc2000b8b60, 0xc2000b8b41)
/usr/local/Cellar/go/1.1.2/src/pkg/database/sql/sql.go:470 +0x35
database/sql.(*DB).exec(0x0, 0x4355f50, 0x5c, 0x0, 0x0, ...)
/usr/local/Cellar/go/1.1.2/src/pkg/database/sql/sql.go:659 +0x68
database/sql.(*DB).Exec(0x0, 0x4355f50, 0x5c, 0x0, 0x0, ...)
/usr/local/Cellar/go/1.1.2/src/pkg/database/sql/sql.go:650 +0x98
main.createComment(0xc2000a3cc0, 0xc2000c1230, 0xc2000ab5b0)
/Users/sdqali/src/play/commentary/simple_bug.go:35 +0x52
net/http.HandlerFunc.ServeHTTP(0x43569c8, 0xc2000a3cc0, 0xc2000c1230, 0xc2000ab5b0)
/usr/local/Cellar/go/1.1.2/src/pkg/net/http/server.go:1149 +0x3e
github.com/gorilla/mux.(*Router).ServeHTTP(0xc2000a7410, 0xc2000a3cc0, 0xc2000c1230, 0xc2000ab5b0)
/usr/local/lib/golang/src/github.com/gorilla/mux/mux.go:98 +0x217
net/http.(*ServeMux).ServeHTTP(0xc2000aa4e0, 0xc2000a3cc0, 0xc2000c1230, 0xc2000ab5b0)
/usr/local/Cellar/go/1.1.2/src/pkg/net/http/server.go:1416 +0x11d
net/http.serverHandler.ServeHTTP(0xc2000a7550, 0xc2000a3cc0, 0xc2000c1230, 0xc2000ab5b0)
/usr/local/Cellar/go/1.1.2/src/pkg/net/http/server.go:1517 +0x16c
net/http.(*conn).serve(0xc2000c0120)
/usr/local/Cellar/go/1.1.2/src/pkg/net/http/server.go:1096 +0x765
created by net/http.(*Server).Serve
/usr/local/Cellar/go/1.1.2/src/pkg/net/http/server.go:1564 +0x266
@stephanelpaul
Copy link

looks like you closed your db connection, then you call exec on the closed connection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment