Repository navigation
GH-38795: [Go] Fix race GetToTimeFunc for Timestamp - #38797
Conversation
|
|
| t.mx.RUnlock() | ||
| t.mx.Lock() | ||
| defer t.mx.Unlock() |
There was a problem hiding this comment.
Wait... :)
A full execution of GetZone is possible between RUnlock() and Lock(), so it's probably a good idea to check t.loc after the write lock is acquired.
If you want to improve the whole solution and make it mostly lock-free, you can combine atomic load/stores with a writers-only mutex. Porting this C++ snippet [1] to Go:
std::atomic<Singleton*> Singleton::m_instance;
std::mutex Singleton::m_mutex;
Singleton* Singleton::getInstance() {
Singleton* tmp = m_instance.load();
if (tmp == nullptr) {
std::lock_guard<std::mutex> lock(m_mutex);
tmp = m_instance.load();
if (tmp == nullptr) {
tmp = new Singleton;
m_instance.store(tmp);
}
}
return tmp;
}There was a problem hiding this comment.
I'll add the check of t.loc after the write lock is acquired for now. My original attempt at this was to use atomic.Pointer for the loc but it made the test initialization for some of the tests annoying since you can't copy an atomic.Pointer and I didn't want to deal with refactoring the tests right now.
For now, the RWMutex should be good enough with the added check to avoid the duplicate work. Nice catch!
Co-authored-by: Ben Harkins <[email protected]>
Co-authored-by: Benjamin Kietzman <[email protected]>
|
After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit 62e1e9a. There were no benchmark performance regressions. 🎉 The full Conbench report has more details. |
### Rationale for this change Adding RWMutex to protect `loc` in `TimestampType` and fix the race condition. ### Are these changes tested? Yes, a unit test is added which is covered by the CI which runs with `-race`. ### Are there any user-facing changes? Copying `TimestampType` will now be problematic and linters will show it as an error. In theory this shouldn't be a problem as most uses of TimestampType should be utilizing pointers to it rather than the value directly. * Closes: apache#38795 Lead-authored-by: Matt Topol <[email protected]> Co-authored-by: Benjamin Kietzman <[email protected]> Co-authored-by: Ben Harkins <[email protected]> Signed-off-by: Benjamin Kietzman <[email protected]>
Rationale for this change
Adding RWMutex to protect
locinTimestampTypeand fix the race condition.Are these changes tested?
Yes, a unit test is added which is covered by the CI which runs with
-race.Are there any user-facing changes?
Copying
TimestampTypewill now be problematic and linters will show it as an error. In theory this shouldn't be a problem as most uses of TimestampType should be utilizing pointers to it rather than the value directly.GetToTimeFuncof fixed timestamp data type #38795