pgconn: check if pipeline i closed in Sync/GetResults

Otherwise there will be a nil pointer exception accessing the conn
pull/1856/head
James Hartig 2023-12-22 14:19:09 -06:00 committed by Jack Christensen
parent dfd198003a
commit 22fe50149b
1 changed files with 13 additions and 1 deletions

View File

@ -2076,10 +2076,21 @@ func (p *Pipeline) Sync() error {
// *PipelineSync. If an ErrorResponse is received from the server, results will be nil and err will be a *PgError. If no // *PipelineSync. If an ErrorResponse is received from the server, results will be nil and err will be a *PgError. If no
// results are available, results and err will both be nil. // results are available, results and err will both be nil.
func (p *Pipeline) GetResults() (results any, err error) { func (p *Pipeline) GetResults() (results any, err error) {
if p.closed {
if p.err != nil {
return nil, p.err
}
return nil, errors.New("pipeline closed")
}
if p.expectedReadyForQueryCount == 0 { if p.expectedReadyForQueryCount == 0 {
return nil, nil return nil, nil
} }
return p.getResults()
}
func (p *Pipeline) getResults() (results any, err error) {
for { for {
msg, err := p.conn.receiveMessage() msg, err := p.conn.receiveMessage()
if err != nil { if err != nil {
@ -2166,6 +2177,7 @@ func (p *Pipeline) Close() error {
if p.closed { if p.closed {
return p.err return p.err
} }
p.closed = true p.closed = true
if p.pendingSync { if p.pendingSync {
@ -2178,7 +2190,7 @@ func (p *Pipeline) Close() error {
} }
for p.expectedReadyForQueryCount > 0 { for p.expectedReadyForQueryCount > 0 {
_, err := p.GetResults() _, err := p.getResults()
if err != nil { if err != nil {
p.err = err p.err = err
var pgErr *PgError var pgErr *PgError