-
Notifications
You must be signed in to change notification settings - Fork 239
Closed
Labels
Description
When a context is used, when that context has timed out it causes the following operation to panic.
I took the live example from https://upper.io/db.v3/examples#mapping-only-one-result and modified it to add a timeout context:
package main
import (
"context"
"log"
"time"
"upper.io/db.v3/postgresql" // Imports the postgresql adapter.
)
var settings = postgresql.ConnectionURL{
Database: `booktown`,
Host: `demo.upper.io`,
User: `demouser`,
Password: `demop4ss`,
}
// Book represents a book.
type Book struct {
ID uint `db:"id"`
Title string `db:"title"`
AuthorID uint `db:"author_id"`
SubjectID uint `db:"subject_id"`
}
func main() {
sess, err := postgresql.Open(settings)
if err != nil {
log.Fatal(err)
}
defer sess.Close()
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
sess = sess.WithContext(ctx)
var book Book
if err := sess.Collection("books").Find("title LIKE ?", "Perl%").OrderBy("-title").One(&book); err != nil {
log.Fatal(err)
}
log.Println("We have one Perl related book:")
log.Printf("%q (ID: %d)\n", book.Title, book.ID)
}I would have expected the call to One to return an error, not to panic with the following message:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x78 pc=0x5979ab]
goroutine 1 [running]:
upper.io/db.v3/internal/sqladapter.(*Result).buildPaginator(0xc00007a3c0, 0x7c7d80, 0x72a801, 0xc00007a3c0, 0xc0000e5e68)
/go/src/upper.io/db.v3/internal/sqladapter/result.go:386 +0xcb
upper.io/db.v3/internal/sqladapter.(*Result).One(0xc00007a3c0, 0x75bc20, 0xc0000fcde0, 0x1, 0x873d40)
/go/src/upper.io/db.v3/internal/sqladapter/result.go:237 +0x2f
main.main()
/tmp/sandbox708627888/main.go:39 +0x2db
Program exited.
gonzaloserrano and jkmrto