GORM Playground Link
Sorry this is an EMPTY REPO, but I can't reliably reproduce this error, so there's no way for me to provide a Playground link for you. I'm just trying to not have my issue closed right away.
Description
reflect: slice index out of range
Stacktrace:
| goroutine 48979 [running]:
| runtime/debug.Stack(0x1466100, 0xc00a16e000, 0x0)
| /usr/local/go/src/runtime/debug/stack.go:24 +0x9d
| cool-package/log.StacktraceIndented(0xcafe7f, 0x1, 0xc007c0f420, 0x1)
| /local/path/to/cool-package/log/logger.go:350 +0x34
| cool-package/log.(*Logger).Errorf(0xc0003c6c30, 0xc00030a7e0, 0x53, 0xc007c0f420, 0x1, 0x1)
| /local/path/to/cool-package/log/logger.go:324 +0xa1
| cool-package/utils.(*LoggerTagged).Errorf(0xc008614240, 0xcd873a, 0x32, 0xc007c0f420, 0x1, 0x1)
| /local/path/to/cool-package/utils/logger.go:55 +0x12b
| cool-package/broker/core.saveDiscoveryResult.func1(0xc008614240)
| /local/path/to/cool-package/broker/core/rpc_methods.go:414 +0xbb
| panic(0xbbeb60, 0xdbbe10)
| /usr/local/go/src/runtime/panic.go:967 +0x15d
| reflect.Value.Index(0xc0025cfb00, 0xc00b64fae0, 0x97, 0xa, 0x0, 0x0, 0x196)
| /usr/local/go/src/reflect/value.go:939 +0x1e7
| gorm.io/gorm/callbacks.CreateWithReturning(0xc00895fd10)
| /local/path/to/go/pkg/mod/gorm.io/gorm@v1.20.11/callbacks/create.go:155 +0x7ad
| gorm.io/gorm.(*processor).Execute(0xc003e02b40, 0xc00895fd10)
| /local/path/to/go/pkg/mod/gorm.io/gorm@v1.20.11/callbacks.go:105 +0x224
| gorm.io/gorm.(*DB).Create(0xc00895fce0, 0xc0025cfb00, 0xc00b64fae0, 0x1)
| /local/path/to/go/pkg/mod/gorm.io/gorm@v1.20.11/finisher_api.go:24 +0xb9
| gorm.io/gorm/callbacks.saveAssociations(0xc00895fad0, 0xc0000dc630, 0xc0025cfb00, 0xc00b64fae0, 0xc00895fbf0, 0xc00b64fa00, 0x0, 0x0, 0x0, 0x456a2c, ...)
| /local/path/to/go/pkg/mod/gorm.io/gorm@v1.20.11/callbacks/associations.go:379 +0x84e
| gorm.io/gorm/callbacks.SaveBeforeAssociations(0xc00895fad0)
| /local/path/to/go/pkg/mod/gorm.io/gorm@v1.20.11/callbacks/associations.go:70 +0x926
| gorm.io/gorm.(*processor).Execute(0xc003e02b40, 0xc00895fad0)
| /local/path/to/go/pkg/mod/gorm.io/gorm@v1.20.11/callbacks.go:105 +0x224
| gorm.io/gorm.(*DB).Create(0xc00b1b2300, 0xb9bb60, 0xc00b64ee20, 0xc00895f830)
| /local/path/to/go/pkg/mod/gorm.io/gorm@v1.20.11/finisher_api.go:24 +0xb9
| cool-package/broker/scopedb.SaveDiscoveryResult.func1(0xc00b1b2300, 0x0, 0x0)
| /local/path/to/cool-package/broker/scopedb/scopedb_discovery.go:353 +0x23b1
| gorm.io/gorm.(*DB).Transaction(0xc00301a2a0, 0xc00b19a100, 0x0, 0x0, 0x0, 0x0, 0x0)
| /local/path/to/go/pkg/mod/gorm.io/gorm@v1.20.11/finisher_api.go:530 +0x3dc
| cool-package/broker/scopedb.SaveDiscoveryResult(0xde4440, 0xc008614240, 0xc00b0d5400, 0x3c65, 0xc008614270, 0x4, 0x0)
| /local/path/to/cool-package/broker/scopedb/scopedb_discovery.go:148 +0x256
| cool-package/broker/core.saveDiscoveryResult(0xc00062c640)
| /local/path/to/cool-package/broker/core/rpc_methods.go:463 +0x8cd
| created by /cool-package/broker/core.(*Broker).SubmitScanResult
| /local/path/to/cool-package/broker/core/rpc_methods.go:195 +0x8c
It seems like
| gorm.io/gorm/callbacks.CreateWithReturning(0xc00895fd10)
| /local/path/to/go/pkg/mod/gorm.io/gorm@v1.20.11/callbacks/create.go:155 +0x7ad
is the reason for this error. I would guess that simply inserting
if int64(db.Statement.ReflectValue.Len()) <= db.RowsAffected {
break
}
would solve this issue, but I'm not to sure whether this would have any side effects or whether this is another issue earlier.
Comment From: jinzhu
Hi, this line is used to avoid the issue https://github.com/go-gorm/gorm/blob/3d3208ed602cdf219cc0501a05bd9f00c6b4bd12/callbacks/create.go#L166-L167, so not sure what caused the panic, it would be helpful if you can reproduce this issue and post the value / code you used when this error happen, Or it is hard to me to make the change, thank you.
Comment From: botastic
Sorry, I must have overseen that check.
So the way I see it there are two possible ways how we can fail even with this check:
1. We try to access an empty slice (i.e. no 0 index)
2. RowsAffected gets incremented at the end of the loop
https://github.com/go-gorm/gorm/blob/3d3208ed602cdf219cc0501a05bd9f00c6b4bd12/callbacks/create.go#L176
where we have no check afterwards
However in the second case we should not enter the loop again, because there is no next row.
Therefore I think it would be sufficient (and equivalent to the current implementation * ) to move the check before accessing the slice.
Edit: * equivalent, except that we also check the 0 index
Comment From: jinzhu
1 We try to access an empty slice (i.e. no 0 index)
should returns error gorm.ErrEmptySlice in this case. https://github.com/go-gorm/gorm/blob/3d3208ed602cdf219cc0501a05bd9f00c6b4bd12/callbacks/create.go#L259
Comment From: jinzhu
Closing due to failed to reproduce the issue. feel free to reopen it if possible to reproduce it.
Comment From: alimoli
@jinzhu
Sorry to comment to this old thread, but I am having the same issue using the method Pluck in a concurrent environment using transaction. Removing the transaction from the pluck statement fixes the issue.
The panic comes from a situation where the driver returns conn busy and then context canceled (voluntarily set).
panic runtime error: index out of range [0] with length 0
github.com/jackc/pgx/v4/stdlib.(*Rows).Next(0x140000a2320, {0x10549fb80, 0x0, 0x104896cdc?})
external/com_github_jackc_pgx_v4/stdlib/sql.go:767 +0x14d0
database/sql.(*Rows).nextLocked(0x140004a4d00)
GOROOT/src/database/sql/sql.go:2974 +0x160
database/sql.(*Rows).Next.func1()
GOROOT/src/database/sql/sql.go:2952 +0x30
database/sql.withLock({0x104ea0a58, 0x140004a4d30}, 0x140005692a8)
GOROOT/src/database/sql/sql.go:3405 +0x7c
database/sql.(*Rows).Next(0x140004a4d00)
GOROOT/src/database/sql/sql.go:2951 +0x64
gorm.io/gorm.Scan({0x104ea5890, 0x140004a4d00}, 0x140003b0540, 0x0)
external/io_gorm_gorm/scan.go:159 +0x15c8
gorm.io/gorm/callbacks.Query(0x140003b0540)
external/io_gorm_gorm/callbacks/query.go:26 +0xec
gorm.io/gorm.(*processor).Execute(0x14000447130, 0x1400001aab0?)
external/io_gorm_gorm/callbacks.go:130 +0x3d0
gorm.io/gorm.(*DB).Pluck(0x140007a2180?, {0x104b663b1, 0x2}, {0x104d3a3c0?, 0x140006228c0})
external/io_gorm_gorm/finisher_api.go:542 +0x24c