ClickHouse / clickhouse-go

Golang driver for ClickHouse
Apache License 2.0
2.88k stars 553 forks source link

Fixed panic on concurrent context key map write #1284

Closed Wang closed 5 months ago

Wang commented 5 months ago

Summary

When open a ClickHouse DB connection through the HTTP protocol and using the WithDeadline context during transaction execution, there is a small probability of concurrent writes to a map, which can lead to a panic. The reason is that when the PrepareContext function is called, it creates a goroutine to read data through the readData function. The readData function updates the key of “max_execution_time” in the map by calling the queryOptions function. Finally, when the transaction executes the Commit, it also calls the Send function, which in turn calls the queryOptions function to update the key of ”max_execution_time“ in the map.

fatal error: concurrent map writes
goroutine 4257644 [running]:
[github.com/ClickHouse/clickhouse-go/v2.queryOptions](http://github.com/ClickHouse/clickhouse-go/v2.queryOptions)({0x2219688, 0xc0238c33e0})
        /home/test/go1.19/global/pkg/mod/[github.com/!click!house/clickhouse-go/v2@v2.7.0/context.go:163](http://github.com/!click!house/clickhouse-go/v2@v2.7.0/context.go:163) +0x196
[github.com/ClickHouse/clickhouse-go/v2.(*httpConnect).readData](http://github.com/ClickHouse/clickhouse-go/v2.(*httpConnect).readData)(0xc01322e9b0, {0x2219688?, 0xc0238c33e0?}, 0xc031b80f00)
        /home/test/go1.19/global/pkg/mod/[github.com/!click!house/clickhouse-go/v2@v2.7.0/conn_http.go:367](http://github.com/!click!house/clickhouse-go/v2@v2.7.0/conn_http.go:367) +0x85
[github.com/ClickHouse/clickhouse-go/v2.(*httpConnect).query.func2()](http://github.com/ClickHouse/clickhouse-go/v2.(*httpConnect).query.func2())
        /home/test/go1.19/global/pkg/mod/[github.com/!click!house/clickhouse-go/v2@v2.7.0/conn_http_query.go:100](http://github.com/!click!house/clickhouse-go/v2@v2.7.0/conn_http_query.go:100) +0x8c
created by [github.com/ClickHouse/clickhouse-go/v2.(*httpConnect).query](http://github.com/ClickHouse/clickhouse-go/v2.(*httpConnect).query)
        /home/test/go1.19/global/pkg/mod/[github.com/!click!house/clickhouse-go/v2@v2.7.0/conn_http_query.go:98](http://github.com/!click!house/clickhouse-go/v2@v2.7.0/conn_http_query.go:98) +0x7ec
CLAassistant commented 5 months ago

CLA assistant check
All committers have signed the CLA.