Skip to content

Commit

Permalink
pgconn: check if pipeline i closed in Sync/GetResults
Browse files Browse the repository at this point in the history
Otherwise there will be a nil pointer exception accessing the conn
  • Loading branch information
jameshartig authored and jackc committed Dec 23, 2023
1 parent dfd1980 commit 22fe501
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pgconn/pgconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// results are available, results and err will both be nil.
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 {
return nil, nil
}

return p.getResults()
}

func (p *Pipeline) getResults() (results any, err error) {
for {
msg, err := p.conn.receiveMessage()
if err != nil {
Expand Down Expand Up @@ -2166,6 +2177,7 @@ func (p *Pipeline) Close() error {
if p.closed {
return p.err
}

p.closed = true

if p.pendingSync {
Expand All @@ -2178,7 +2190,7 @@ func (p *Pipeline) Close() error {
}

for p.expectedReadyForQueryCount > 0 {
_, err := p.GetResults()
_, err := p.getResults()
if err != nil {
p.err = err
var pgErr *PgError
Expand Down

0 comments on commit 22fe501

Please sign in to comment.