Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic: runtime error: while running Query #39603

Closed
garimakhurania opened this issue Jun 16, 2020 · 3 comments
Closed

panic: runtime error: while running Query #39603

garimakhurania opened this issue Jun 16, 2020 · 3 comments

Comments

@garimakhurania
Copy link

@garimakhurania garimakhurania commented Jun 16, 2020

What version of Go are you using (go version)?

$ go version 
go1.13.5

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

stmt, err := d.db.Prepare(d.sqlMap[stmtName])
defer closePreparedStmt(stmt)
if err != nil {
fmt.Printf("error preparing %s: %s\n", stmtName, err)
return "", err
}

results, err := stmt.Query()

I noticed the panic below while running the service in production environment. My service does batch processing every hour and the issue happens randomly.

What did you expect to see?

Results from the query instead of panic

What did you see instead?

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0xa0a523]

goroutine 3936 [running]:
sync.(*RWMutex).RLock(...)
/usr/local/go/src/sync/rwmutex.go:48
database/sql.(*Stmt).QueryContext(0x0, 0xcfde00, 0xc0000280a0, 0xc0020d7c58, 0x2, 0x2, 0x0, 0x0, 0x0)
/usr/local/go/src/database/sql/sql.go:2539 +0x53
database/sql.(*Stmt).Query(...)
/usr/local/go/src/database/sql/sql.go:2594
service.(*DBStore).GetAll(0xc000206180, 0xc0001102a0, 0xc, 0xc000164270, 0x24, 0x0, 0x0, 0x0, 0x0, 0x0)
database/sql.(*Stmt).QueryContext(0x0, 0xcfde00, 0xc0000a2068, 0xc000219c58, 0x2, 0x2, 0x0, 0x0, 0x0)
/src/pkg/xxx.go:418 +0x35b

@davecheney
Copy link
Contributor

@davecheney davecheney commented Jun 16, 2020

stmt, err := d.db.Prepare(d.sqlMap[stmtName])
defer closePreparedStmt(stmt)

Stmt is nil because an error occurred. You must check each err value returned before you can assume anything about the state of other return values.

Unlike many projects on GitHub, the Go project does not use its bug tracker for general discussion or asking questions. We only use our bug tracker for tracking bugs and tracking proposals going through the Proposal Process.

Please see https://golang.org/wiki/Questions for good places to ask questions.

@davecheney davecheney closed this Jun 16, 2020
@garimakhurania
Copy link
Author

@garimakhurania garimakhurania commented Jun 16, 2020

//closePreparedStmt close prepared statement
func closePreparedStmt(stmt *sql.Stmt) {
if stmt == nil {
return
}
if err := stmt.Close(); err != nil {
fmt.Printf("error closing prepared statement: %s\n", err)
}
}

The nil check is done. As mentioned, the issue only happens occasionally and not every time when query is run.

@davecheney
Copy link
Contributor

@davecheney davecheney commented Jun 16, 2020

Your nil statement is causing your core to panic

From the stack trace you provided

usr/local/go/src/sync/rwmutex.go:48
database/sql.(*Stmt).QueryContext(0x0, 0xcfde00, 0xc0000280a0, 0xc0020d7c58, 0x2, 0x2, 0x0, 0x0, 0x0)

0x0 as the first argument implies the receiver is nil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.