Skip to content

Commit

Permalink
Fix data race in Postgres engine on connection close (#32650)
Browse files Browse the repository at this point in the history
* Fix data race in Postgres engine.

* Use channel instead of `sync.WaitGroup`.
  • Loading branch information
Tener authored Sep 29, 2023
1 parent e93d165 commit 818d9ed
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/srv/db/postgres/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,11 @@ func (e *Engine) receiveFromServer(serverConn *pgconn.PgConn, serverErrCh chan<-
copyReader, copyWriter := io.Pipe()
defer copyWriter.Close()

closeChan := make(chan struct{})

go func() {
defer copyReader.Close()
defer close(closeChan)

// server will never be used to write to server,
// which is why we pass io.Discard instead of e.rawServerConn
Expand Down Expand Up @@ -455,6 +458,8 @@ func (e *Engine) receiveFromServer(serverConn *pgconn.PgConn, serverErrCh chan<-
log.WithError(err).Warn("Server -> Client copy finished with unexpected error.")
}

<-closeChan

serverErrCh <- trace.Wrap(err)
log.Debugf("Stopped receiving from server. Transferred %v bytes.", total)
}
Expand Down

0 comments on commit 818d9ed

Please sign in to comment.