odbc icon indicating copy to clipboard operation
odbc copied to clipboard

QueryContext raises `panic: send on closed channel`

Open Shamus03 opened this issue 1 year ago • 2 comments

After the recent commit to add support for QueryContext: https://github.com/alexbrainman/odbc/commit/1293aff3fc513fdd5e9d521b64e35bf56fba8d22#diff-4f427d2b022907c552328e63f137561f6de92396d7a6e8f6c2ea1bcf0db52654R153

We are getting the following error:

Thursday, August 15th 2024 @ 3:41:50 pm	panic: send on closed channel
Thursday, August 15th 2024 @ 3:41:50 pm	goroutine 4794 [running]:
Thursday, August 15th 2024 @ 3:41:50 pm	github.com/alexbrainman/odbc.(*Conn).wrapQuery(0x1?, {0x99c900, 0xc0002a0150}, 0xc00031e320, {0xeb7c20?, 0x6fcc68?, 0xc0004c37d0?}, 0x6f20e5?, 0xc0007aa570?)
Thursday, August 15th 2024 @ 3:41:50 pm	/root/go/src/github.com/alexbrainman/odbc/conn.go:153 +0x145
Thursday, August 15th 2024 @ 3:41:50 pm	created by github.com/alexbrainman/odbc.(*Conn).QueryContext in goroutine 4792
Thursday, August 15th 2024 @ 3:41:50 pm	/root/go/src/github.com/alexbrainman/odbc/conn.go:104 +0x389

It is inside this goroutine: https://github.com/alexbrainman/odbc/commit/1293aff3fc513fdd5e9d521b64e35bf56fba8d22#diff-4f427d2b022907c552328e63f137561f6de92396d7a6e8f6c2ea1bcf0db52654R104

And it seems to kill the connection or permanently damage/deadlock it.

For context: we are using unixodbc to connect to an IBM DB2 database running on an AS400. Hopefully that is helpful.

Working on steps to reproduce, but I haven't been able to nail it yet. We have about 20-30 service instances using this version of the package, and they die with this error slowly over time.

Shamus03 avatar Aug 15 '24 20:08 Shamus03

@Shamus03 thanks for reporting the problem.

You can always go to previous version, like

go get github.com/alexbrainman/odbc@1421b829acc

I can even add git tags with version numbers, if it helps you any.

I don't have time to debug your reported panic. But I will accept fixes from anyone including @popeliv .

Alex

alexbrainman avatar Aug 17 '24 04:08 alexbrainman

Thanks. We have already reverted to a previous version for now.

Shamus03 avatar Aug 18 '24 22:08 Shamus03

Hello. This issue is still happening - popeliv's PR seems like a good fix - there is no need to check ctx.Err() inside the goroutine, because the caller already waits on <-ctx.Done() and checks that same error.

Another possible solution: instead of using defer close(errorChan), which can cause the channel to close before the goroutine has finished, you could do this:

go func() {
  c.wrapQuery(ctx, os, dargs, rowsChan, errorChan)
  close(errorChan)
}()

Shamus03 avatar Apr 16 '25 14:04 Shamus03

@Shamus03 and @popeliv

I created an alternative fix in https://github.com/alexbrainman/odbc/pull/212 .

Please, let me know if you have objections.

Thank you.

Alex

alexbrainman avatar Apr 19 '25 02:04 alexbrainman