fix concurrent map write errors with context #1523
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
closes #1503
This PR fixes 3 bugs:
clickhouse.WithSettings()
maps are re-used between context/callsclickhouse.WithSettings(nil)
map could result in a nil write panicThe root cause of #1503 is that maps were being shared between contexts. This is fine when nesting contexts, but there are some cases where the driver will override settings. For example
max_execution_time
is always overridden when the context has a deadline. Another example isasync_insert
being changed when usingAsyncInsert
. When the driver overrides these settings, the settings are persisted for the following queries. This leads to unexpected behavior as well as concurrency issues when overriding settings.As a fix, the
Settings
map will now always be copied when read in the driver. This is because the deadline context is almost always modified, and settings are almost always overridden (this will increase garbage but it's negligible).There were two places in the code where the settings are read but not modified: reading async status and reading user timezone location. I added two functions for these cases so we don't need to make an unnecessary copy.
I added many tests to solidify the behavior of the driver's context wrapper. I also added a test case that forces a concurrent map write issue (which doesn't fail anymore since it's fixed).
Function documentation has been updated too to make it clearer in the future. The code is also a bit more readable and easy to follow when seeing where the
Settings
map is initialized.Checklist