On Thu, Jul 21, 2022 at 11:02 AM Michal Hruby <michal....@gmail.com> wrote:
>
> Hello, I have a code snippet equivalent to this one:
>
> rows, err := stmt.QueryContext(ctx)
> if err != nil {
>     return info, nil, err
> }
> defer rows.Close()
>
> var data sql.RawBytes
> for rows.Next() {
>     err = rows.Scan(&data)
>     if err != nil {
>         return nil, err
>     }
>     // if ctx is canceled around this point, rows.Close() will be called and 
> the data can e overwritten, causing a data race
>     _ = data[0] // <--- possible concurrent read here and write inside 
> driver.Rows.Close()
> }
>
> And as the comments say, if the context is cancelled, there's a possibility 
> of a data race where this goroutine is trying to read data[0], and another 
> goroutine can be writing to it, cause the sql package calls 
> driver.Rows.Close() (from 
> https://github.com/golang/go/blob/176b63e7113b82c140a4ecb2620024526c2c42e3/src/database/sql/sql.go#L2974
>  )
>
> I've opened an issue about this on github ( 
> https://github.com/golang/go/issues/53970 ), but was told that's not the 
> proper forum to talk about data races.
>
> Any help would be appreciated, thanks.

I don't quite understand what you are asking.  I think you're correct
that there is a data race there.  It seems to me that the answer is to
not cancel the context.

What kind of answer are you looking for?

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUwGN5b-DEwCqLRu6kWwpAnCvO7o6u5TjzS3bmbUgksdw%40mail.gmail.com.

Reply via email to