marcboeker / go-duckdb

go-duckdb provides a database/sql driver for the DuckDB database engine.
MIT License
714 stars 106 forks source link

Segmentation fault on QueryContext #118

Closed ivanovaleksey closed 9 months ago

ivanovaleksey commented 1 year ago

Hey!

Got a segmentation fault error recently. Running Go 1.20 and go-duckdb v1.4.1 Unfortunately I currently don't have a clear use case how to reproduce the error. It happened only once so far.

Please see the stack trace below. Is there any additional info that could help to spot the problem?

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x138f pc=0x7f7750b0d5d1]

runtime stack:
runtime.throw({0x23f9c94?, 0x0?})
    /usr/local/go/src/runtime/panic.go:1047 +0x5d fp=0x7f777c93f1c0 sp=0x7f777c93f190 pc=0x8fba5d
runtime.sigpanic()
    /usr/local/go/src/runtime/signal_unix.go:825 +0x3e9 fp=0x7f777c93f220 sp=0x7f777c93f1c0 pc=0x912189

goroutine 3814 [syscall]:
runtime.cgocall(0x1211cd0, 0xc0007fee30)
    /usr/local/go/src/runtime/cgocall.go:157 +0x5c fp=0xc0007fee08 sp=0xc0007fedd0 pc=0x8ca8dc
github.com/marcboeker/go-duckdb._Cfunc_duckdb_prepare_extracted_statement(0x7f7770001410, 0x7f7764000de0, 0x0, 0xc000490348)
    _cgo_gotypes.go:1385 +0x4c fp=0xc0007fee30 sp=0xc0007fee08 pc=0x100ee4c
github.com/marcboeker/go-duckdb.(*conn).prepareExtractedStmt.func1(0xc0007feeb0?, 0x8d4407?, 0x18?, 0x22d5ec0?)
    /builds/.ci-gopath/pkg/mod/github.com/marcboeker/github.com/marcboeker/go-duckdb@v1.4.1/connection.go:187 +0xa7 fp=0xc0007fee88 sp=0xc0007fee30 pc=0x10115e7
github.com/marcboeker/go-duckdb.(*conn).prepareExtractedStmt(0xc0000fb4c0, 0xc000836000?, 0xc0007fef48?)
    /builds/.ci-gopath/pkg/mod/github.com/marcboeker/github.com/marcboeker/go-duckdb@v1.4.1/connection.go:187 +0x4f fp=0xc0007feee0 sp=0xc0007fee88 pc=0x101132f
github.com/marcboeker/go-duckdb.(*conn).QueryContext(0xc0000fb4c0, {0x2534078, 0xc000577920}, {0xc000836000, 0x25f}, {0xc000859900, 0x2, 0x2})
    /builds/.ci-gopath/pkg/mod/github.com/marcboeker/github.com/marcboeker/go-duckdb@v1.4.1/connection.go:91 +0x2ca fp=0xc0007fef98 sp=0xc0007feee0 pc=0x101028a
database/sql.ctxDriverQuery({0x2534078?, 0xc000577920?}, {0x7f776ee8ab28?, 0xc0000fb4c0?}, {0x0?, 0x0?}, {0xc000836000?, 0x218a644?}, {0xc000859900, 0x2, ...})
    /usr/local/go/src/database/sql/ctxutil.go:48 +0xdd fp=0xc0007ff020 sp=0xc0007fef98 pc=0x9d139d
database/sql.(*DB).queryDC.func1()
    /usr/local/go/src/database/sql/sql.go:1748 +0x175 fp=0xc0007ff0e8 sp=0xc0007ff020 pc=0x9d91d5
database/sql.withLock({0x252e748, 0xc0000e8e10}, 0xc0007ff238)
    /usr/local/go/src/database/sql/sql.go:3405 +0x8c fp=0xc0007ff128 sp=0xc0007ff0e8 pc=0x9e112c
database/sql.(*DB).queryDC(0x8d4101?, {0x2534078, 0xc000577920}, {0x0, 0x0}, 0xc0000e8e10, 0xc000480f30, {0xc000836000, 0x25f}, {0xc00028af40, ...})
    /usr/local/go/src/database/sql/sql.go:1743 +0x211 fp=0xc0007ff2c8 sp=0xc0007ff128 pc=0x9d8ad1
database/sql.(*DB).query(0x5?, {0x2534078, 0xc000577920}, {0xc000836000, 0x25f}, {0xc00028af40, 0x2, 0x2}, 0xc7?)
    /usr/local/go/src/database/sql/sql.go:1726 +0xfd fp=0xc0007ff348 sp=0xc0007ff2c8 pc=0x9d883d
database/sql.(*DB).QueryContext.func1(0x0?)
    /usr/local/go/src/database/sql/sql.go:1704 +0x55 fp=0xc0007ff3b0 sp=0xc0007ff348 pc=0x9d8635
database/sql.(*DB).retry(0xc000836000?, 0xc0007ff420)
    /usr/local/go/src/database/sql/sql.go:1538 +0x47 fp=0xc0007ff3f8 sp=0xc0007ff3b0 pc=0x9d6d87
database/sql.(*DB).QueryContext(0x21ff2c0?, {0x2534078?, 0xc000577920?}, {0xc000836000?, 0x30?}, {0xc00028af40?, 0x21ff2c0?, 0xc000480ee0?})
    /usr/local/go/src/database/sql/sql.go:1703 +0xcc fp=0xc0007ff488 sp=0xc0007ff3f8 pc=0x9d854c
marcboeker commented 1 year ago

Thanks for the report. Could you please check with the latest v1.5.1 to see, if the segfault still exists?

imysm commented 9 months ago

Hi,Marcboeker, I encountered the same error. version is v1.5.6

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x1000d38d6] goroutine 1 [running]: github.com/marcboeker/go-duckdb.(stmt).execute(0xc0000a00d8, {0x0, 0x0}, {0x0?, 0xc00005a900?, 0x1000cd525?}) /Users/samyang/go/pkg/mod/github.com/marcboeker/go-duckdb@v1.5.6/statement.go:209 +0x1d6 github.com/marcboeker/go-duckdb.(stmt).ExecContext(0xc000098100?, {0x0?, 0x0?}, {0x0?, 0x0?, 0x0?}) /Users/samyang/go/pkg/mod/github.com/marcboeker/go-duckdb@v1.5.6/statement.go:159 +0x47 github.com/marcboeker/go-duckdb.(conn).ExecContext(0xc000098100, {0x0, 0x0}, {0x10160fdea, 0xe}, {0x0, 0x0, 0x0}) /Users/samyang/go/pkg/mod/github.com/marcboeker/go-duckdb@v1.5.6/connection.go:62 +0x33d main.connection.func1({0x101639780, 0xc000098100}) /Users/samyang/GolandProjects/syncmaster/example/duckdb/main.go:161 +0xdc github.com/marcboeker/go-duckdb.(connector).Connect(0xc0000980e0, {0xc00005aba0, 0x100014eee}) /Users/samyang/go/pkg/mod/github.com/marcboeker/go-duckdb@v1.5.6/duckdb.go:100 +0x8a database/sql.(DB).conn(0xc00009e750, {0x101639c48, 0x101955c80}, 0x1) /usr/local/opt/go/libexec/src/database/sql/sql.go:1387 +0x717 database/sql.(DB).query(0x10194f868?, {0x101639c48, 0x101955c80}, {0x101614fd4, 0x25}, {0x0, 0x0, 0x0}, 0xc0?) /usr/local/opt/go/libexec/src/database/sql/sql.go:1721 +0x57 database/sql.(DB).QueryContext.func1(0x76?) /usr/local/opt/go/libexec/src/database/sql/sql.go:1704 +0x4f database/sql.(DB).retry(0x1026261c0?, 0xc00005adc0) /usr/local/opt/go/libexec/src/database/sql/sql.go:1538 +0x42 database/sql.(DB).QueryContext(0xc00009c360?, {0x101639c48?, 0x101955c80?}, {0x101614fd4?, 0x101639b38?}, {0x0?, 0x1000d7ab9?, 0xc00009e750?}) /usr/local/opt/go/libexec/src/database/sql/sql.go:1703 +0xc5 database/sql.(DB).QueryRowContext(...) /usr/local/opt/go/libexec/src/database/sql/sql.go:1804 database/sql.(*DB).QueryRow(0x10160f0cb?, {0x101614fd4?, 0xc00004a6d8?}, {0x0?, 0xc0000061a0?, 0xc00004a6f8?}) /usr/local/opt/go/libexec/src/database/sql/sql.go:1818 +0x45 main.main() /Users/samyang/GolandProjects/syncmaster/example/duckdb/main.go:36 +0x66 exit status 2

My Code:

connector, := duckdb.NewConnector(dsn, func(execer driver.ExecerContext) error { bootQueries := []string{ "INSTALL 'json'", "LOAD 'json'", } var ctx context.Context for , qry := range bootQueries { _, err := execer.ExecContext(ctx, qry, nil) log.Printf("%v", qry) if err != nil { return err } } return nil }) db := sql.OpenDB(connector) db.SetMaxOpenConns(100) return db

marcboeker commented 9 months ago

I was able to reproduce this. You can skip the connector and use the db, err := sql.Open("duckdb", "") and then db.QueryContext() until it's fixed. You manually have to install and load the JSON plugin using db.ExecContext("INSTALL 'json'; LOAD 'json';").

marcboeker commented 9 months ago

@imysm The problem lies in your code. You are providing a context variable which is not initialized with a proper context. See var ctx context.Context which should be ctx := context.Background() or provide context.Background() directly.

for _, qry := range bootQueries {
  _, err := execer.ExecContext(context.Background(), qry, nil)
  log.Printf("%v", qry)
  if err != nil {
    return err
  }
}
imysm commented 9 months ago

ehhh thank you ,this is my problem .