Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix data races #1080

Merged
merged 6 commits into from
Jan 4, 2025
Merged

chore: fix data races #1080

merged 6 commits into from
Jan 4, 2025

Conversation

nktks
Copy link
Contributor

@nktks nktks commented Dec 2, 2024

PR Checklist

  • Read the Contributing documentation.
  • Read the Code of conduct documentation.
  • Name your Pull Request title clearly, concisely, and prefixed with the name of the primarily affected package you changed according to Good commit messages (such as memory: add interfaces for X, Y or util: add whizzbang helpers).
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. Fixes #123).
  • Describes the source of new concepts. <- I don't add new concept
  • References existing implementations as appropriate.
  • Contains test coverage for new functions. <- I don't add new function
  • Passes all golangci-lint checks.

What

Hello.
Thank you for maintaining this great module!
I would like to:

@nktks nktks marked this pull request as ready for review December 2, 2024 09:10
@nktks nktks changed the title fix data race in openaiclient fix data races Dec 2, 2024
@nktks nktks changed the title fix data races chore: fix data races Dec 2, 2024
@leventov
Copy link

leventov commented Dec 4, 2024

There is at least one more race, ernieclient.Client.accessToken field is written in autoRefresh() through a race. This is pretty much a syntactically-catchable thing, I wonder why go vet or other tools don't catch this.

@nktks
Copy link
Contributor Author

nktks commented Dec 5, 2024

@leventov
Hello.
You mentioned about https://github.com/nktks/langchaingo/blob/fix/data-race/llms/ernie/internal/ernieclient/ernieclient.go#L178 ?

I think, the normal use case is to call ernieclient.New() only once for one *ernieclient.Client instance.
In this case, autoRefresh() is called only in ernieclient.New(), and the accessToken is written in only one goroutine in autoRefresh(), so rata races won't occur.

@leventov
Copy link

leventov commented Dec 5, 2024

@nktks yes, only one goroutine. But isn't writing a plain field c.accessToken from one goroutine and then reading from others (any goroutines that use the Client for making LLM calls) a data race? Shouldn't such value hand over happen either via atomic.Value, or a channel, but not just plain field write/read?

@leventov
Copy link

leventov commented Dec 5, 2024

Here's from https://go.dev/ref/mem:

Reads of memory locations larger than a single machine word are encouraged but not required to meet the same semantics as word-sized memory locations, observing a single allowed write w. For performance reasons, implementations may instead treat larger operations as a set of individual machine-word-sized operations in an unspecified order. This means that races on multiword data structures can lead to inconsistent values not corresponding to a single write. When the values depend on the consistency of internal (pointer, length) or (pointer, type) pairs, as can be the case for interface values, maps, slices, and strings in most Go implementations, such races can in turn lead to arbitrary memory corruption.

@nktks
Copy link
Contributor Author

nktks commented Dec 5, 2024

@leventov Thank you!
Indeed reading/writting accessToken happens data race.

I added test case to reproduce.
https://github.com/tmc/langchaingo/actions/runs/12176399051/job/33962066313

Then I fixed it by using sync.RWMutex.

Copy link
Collaborator

@FluffyKebab FluffyKebab left a comment

Choose a reason for hiding this comment

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

LGTM. I would like to see the test in ernieclient_test.go use testify, but that can be done later.

@FluffyKebab FluffyKebab merged commit 7266c9e into tmc:main Jan 4, 2025
3 checks passed
@nktks nktks deleted the fix/data-race branch January 6, 2025 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAI Chat LLM thread safety
3 participants