Skip to content

Commit

Permalink
Fix rows.Close function (#1314)
Browse files Browse the repository at this point in the history
The function flushes the streams and errors channel.
However, because select on a closed channel returns immediately, the function
can return without reading the error from the errors channel
  • Loading branch information
yujiarista authored Jun 4, 2024
1 parent dd45741 commit 4d96914
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions clickhouse_rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,40 @@ func (r *rows) Close() error {
if r.errors == nil && r.stream == nil {
return r.err
}
active := 0
if r.errors != nil {
active++

if r.errors == nil {
for range r.stream {
}
return nil
}
if r.stream != nil {
active++

if r.stream == nil {
for err := range r.errors {
r.err = err
}
return r.err
}

errorsClosed := false
streamClosed := false
for {
select {
case _, ok := <-r.stream:
if !ok {
active--
if active == 0 {
return r.err
}
streamClosed = true
}
case err, ok := <-r.errors:
if err != nil {
r.err = err
}
if !ok {
active--
if active == 0 {
return r.err
}
errorsClosed = true
}
}

if errorsClosed && streamClosed {
return r.err
}
}
}

Expand Down

0 comments on commit 4d96914

Please sign in to comment.