Skip to content

GH-38795: [Go] Fix race GetToTimeFunc for Timestamp - #38797

Merged
bkietz merged 4 commits into
apache:mainfrom
zeroshade:totime-func-race
Nov 27, 2023
Merged

bkietz merged 4 commits into
apache:mainfrom
zeroshade:totime-func-race

Conversation

@zeroshade

@zeroshade zeroshade commented Nov 20, 2023 •

Copy link
Copy Markdown
Member

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.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #38795 has been automatically assigned in GitHub to PR creator.

@benibus benibus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (besides the nit)

Comment thread go/arrow/datatype_fixedwidth_test.go Resolved
Comment on lines +411 to +413
t.mx.RUnlock()
t.mx.Lock()
defer t.mx.Unlock()

@felipecrv felipecrv Nov 21, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

[1] https://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/#using-c11-sequentially-consistent-atomics

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread go/arrow/datatype_fixedwidth.go Resolved
@bkietz
bkietz merged commit 62e1e9a into apache:main Nov 27, 2023
@conbench-apache-arrow

Copy link
Copy Markdown

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.

@zeroshade
zeroshade deleted the totime-func-race branch November 28, 2023 17:10
dgreiss pushed a commit to dgreiss/arrow that referenced this pull request Feb 19, 2024
### 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Go] Data race of calling GetToTimeFunc of fixed timestamp data type

5 participants