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

fix: add locking around broker throttle timer to prevent race condition #2826

Merged
merged 1 commit into from Mar 8, 2024

Conversation

chengsha
Copy link
Contributor

@chengsha chengsha commented Mar 5, 2024

The producer can be easily blocked due to a race condition in Broker.throttleTimer, which may result in a panic. Consider changing the type of Broker.throttleTimer to atomic.Value

Fixes #2823

Signed-off-by: chengsha shacheng@tencent.com

broker.go Outdated
Comment on lines 1703 to 1710
func (b *Broker) setThrottle(throttleTime time.Duration) {
if b.throttleTimer != nil {
value := b.throttleTimer.Load()
if value != nil && value != emptyTimer {
throttleTimer := value.(*time.Timer)
// if there is an existing timer stop/clear it
if !b.throttleTimer.Stop() {
<-b.throttleTimer.C
if !throttleTimer.Stop() {
<-throttleTimer.C
}
}
b.throttleTimer = time.NewTimer(throttleTime)
b.throttleTimer.CompareAndSwap(value, time.NewTimer(throttleTime))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I’m unsure about this approach. Since this is now an atomic operation, and there is a potentially blocking operation in it, we could easily end up with two setThrottle operations running at the same time.

Of particular note is the message on the timer.Stop godoc:

This cannot be done concurrent to other receives from the Timer's channel or other calls to the Timer's Stop method.

I think we need to be handling this within a properly locked mutex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are right. throttleTimer.Stop is not safe there. i change to locked mutex

@chengsha chengsha force-pushed the chengsha/fix-data-race-when-throttled branch 2 times, most recently from c6d9d39 to cc3fa66 Compare March 6, 2024 01:57
…throttleTimer, which may result in a panic. add throttleTimerLock to protect

Fixes IBM#2823

Signed-off-by: shacheng <shacheng@tencent.com>
@chengsha chengsha force-pushed the chengsha/fix-data-race-when-throttled branch from cc3fa66 to 7d6b70b Compare March 7, 2024 01:55
@dnwe dnwe added the fix label Mar 7, 2024
@dnwe dnwe changed the title The producer can be easily blocked due to a race condition in Broker.… fix: add locking around broker throttle timer to prevent race condition Mar 7, 2024
Copy link
Collaborator

@dnwe dnwe left a comment

Choose a reason for hiding this comment

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

Thanks for reporting and fixing this locking issue 🎉

@dnwe dnwe merged commit a8b3b3d into IBM:main Mar 8, 2024
13 checks passed
mx-psi pushed a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Apr 2, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/IBM/sarama](https://togithub.com/IBM/sarama) | `v1.43.0`
-> `v1.43.1` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fIBM%2fsarama/v1.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fIBM%2fsarama/v1.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fIBM%2fsarama/v1.43.0/v1.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fIBM%2fsarama/v1.43.0/v1.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>IBM/sarama (github.com/IBM/sarama)</summary>

### [`v1.43.1`](https://togithub.com/IBM/sarama/releases/tag/v1.43.1):
Version 1.43.1 (2024-03-27)

[Compare
Source](https://togithub.com/IBM/sarama/compare/v1.43.0...v1.43.1)

<!-- Release notes generated using configuration in .github/release.yaml
at main -->

#### What's Changed

##### 🐛 Fixes

- fix: message.max.bytes should default to
[`1048576`](https://togithub.com/IBM/sarama/commit/1048576) not 1 MB by
[@&#8203;puellanivis](https://togithub.com/puellanivis) in
[IBM/sarama#2804
- fix: add locking around broker throttle timer to prevent race
condition by [@&#8203;chengsha](https://togithub.com/chengsha) in
[IBM/sarama#2826

##### 📦 Dependency updates

- chore(deps): bump go.opentelemetry.io/otel/sdk from 1.23.1 to 1.24.0
in /examples/interceptors by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[IBM/sarama#2816
- chore(deps): bump the golang-org-x group with 1 update by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[IBM/sarama#2825
- chore(deps): bump github.com/stretchr/testify from 1.8.4 to 1.9.0 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[IBM/sarama#2822
- chore(deps): bump
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric from 1.23.1 to
1.24.0 in /examples/interceptors by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[IBM/sarama#2815

#### New Contributors

- [@&#8203;chengsha](https://togithub.com/chengsha) made their first
contribution in
[IBM/sarama#2826

**Full Changelog**:
IBM/sarama@v1.43.0...v1.43.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "on tuesday" (UTC), Automerge - At any
time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/open-telemetry/opentelemetry-collector-contrib).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yNjkuMiIsInVwZGF0ZWRJblZlciI6IjM3LjI2OS4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: opentelemetrybot <107717825+opentelemetrybot@users.noreply.github.com>
ycombinator pushed a commit to ycombinator/opentelemetry-collector-contrib that referenced this pull request Apr 9, 2024
…etry#32091)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/IBM/sarama](https://togithub.com/IBM/sarama) | `v1.43.0`
-> `v1.43.1` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fIBM%2fsarama/v1.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fIBM%2fsarama/v1.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fIBM%2fsarama/v1.43.0/v1.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fIBM%2fsarama/v1.43.0/v1.43.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>IBM/sarama (github.com/IBM/sarama)</summary>

### [`v1.43.1`](https://togithub.com/IBM/sarama/releases/tag/v1.43.1):
Version 1.43.1 (2024-03-27)

[Compare
Source](https://togithub.com/IBM/sarama/compare/v1.43.0...v1.43.1)

<!-- Release notes generated using configuration in .github/release.yaml
at main -->

#### What's Changed

##### 🐛 Fixes

- fix: message.max.bytes should default to
[`1048576`](https://togithub.com/IBM/sarama/commit/1048576) not 1 MB by
[@&open-telemetry#8203;puellanivis](https://togithub.com/puellanivis) in
[IBM/sarama#2804
- fix: add locking around broker throttle timer to prevent race
condition by [@&open-telemetry#8203;chengsha](https://togithub.com/chengsha) in
[IBM/sarama#2826

##### 📦 Dependency updates

- chore(deps): bump go.opentelemetry.io/otel/sdk from 1.23.1 to 1.24.0
in /examples/interceptors by
[@&open-telemetry#8203;dependabot](https://togithub.com/dependabot) in
[IBM/sarama#2816
- chore(deps): bump the golang-org-x group with 1 update by
[@&open-telemetry#8203;dependabot](https://togithub.com/dependabot) in
[IBM/sarama#2825
- chore(deps): bump github.com/stretchr/testify from 1.8.4 to 1.9.0 by
[@&open-telemetry#8203;dependabot](https://togithub.com/dependabot) in
[IBM/sarama#2822
- chore(deps): bump
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric from 1.23.1 to
1.24.0 in /examples/interceptors by
[@&open-telemetry#8203;dependabot](https://togithub.com/dependabot) in
[IBM/sarama#2815

#### New Contributors

- [@&open-telemetry#8203;chengsha](https://togithub.com/chengsha) made their first
contribution in
[IBM/sarama#2826

**Full Changelog**:
IBM/sarama@v1.43.0...v1.43.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "on tuesday" (UTC), Automerge - At any
time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/open-telemetry/opentelemetry-collector-contrib).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yNjkuMiIsInVwZGF0ZWRJblZlciI6IjM3LjI2OS4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: opentelemetrybot <107717825+opentelemetrybot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When the Kafka broker is throttled, the producer is easily blocked
3 participants