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

kgo: fix deadlock in Produce when using MaxBufferedBytes #787

Merged
merged 3 commits into from
Jul 29, 2024
Merged

kgo: fix deadlock in Produce when using MaxBufferedBytes #787

merged 3 commits into from
Jul 29, 2024

Conversation

twmb
Copy link
Owner

@twmb twmb commented Jul 22, 2024

Copying from the issue,
"""

  1. Produce() record A (100 bytes)
  2. Produce() record B (50 bytes), waiting for buffer to free
  3. Produce() record C (50 bytes), waiting for buffer to free
  4. Record A is produced, finishRecordPromise() gets called, detects it was over the limit so publish 1 message to waitBuffer
  5. Record B is unlocked, finishRecordPromise() gets called, does not detect it was over the limit (only 50 bytes), so record C is never unblocked and will wait indefinitely on waitBuffer
    """

The fix requires adding a lock while producing. This reuses the existing
lock on the producer type. This can lead to a few more spurious
wakeups in other functions that use this same mutex, but that's fine.

The prior algorithm counted anything to produce immediately into the
buffered records and bytes fields; the fix for #777 could not really be
possible unless we avoid counting the "buffered" aspect right away.
Specifically, we need to have a goroutine looping with a sync.Cond that
checks IF we add the record, will we still be blocked? This allows us
to wake up all blocked goroutines always (unlike one at a time, the
problem this issue points out), and each goroutine can check under a
lock if they still do not fit.

This also fixes an unreported bug where, if a record WOULD be blocked
but fails early due to no topic / not in a transaction while in a
transactional client, the serial promise finishing goroutine would
deadlock.

Closes #777.

The select has three cases; two use drainBuffered which properly added
-1 to the blocked count. The first case didn't. We now do.

Practically, this meant that if you ever were blocked while lingering,
you would never linger again.

For #726.
@twmb twmb changed the title 777 kgo: fix deadlock in Produce when using MaxBufferedBytes Jul 22, 2024
@twmb twmb force-pushed the 777 branch 5 times, most recently from 66ce331 to ff7ebfb Compare July 22, 2024 05:46
@twmb
Copy link
Owner Author

twmb commented Jul 22, 2024

I've loop tested this locally,

  • ~15 times against a 3 node Kafka cluster
  • ~4 times against a 3 node Kafka cluster with -race
  • ~15 times against a 1 node Bitnami cluster (to mirror CI)
  • 35 times against a 1 node Bitnami cluster with -race
  • 20 times so far against a 3 node Redpanda cluster

So far I am unable to recreate what fails in CI at all.

twmb added 2 commits July 28, 2024 22:07
Copying from the issue,
"""
1) Produce() record A (100 bytes)
2) Produce() record B (50 bytes), waiting for buffer to free
3) Produce() record C (50 bytes), waiting for buffer to free
4) Record A is produced, finishRecordPromise() gets called, detects it was over the limit so publish 1 message to waitBuffer
5) Record B is unlocked, finishRecordPromise() gets called, does not detect it was over the limit (only 50 bytes), so record C is never unblocked and will wait indefinitely on waitBuffer
"""

The fix requires adding a lock while producing. This reuses the existing
lock on the `producer` type. This can lead to a few more spurious
wakeups in other functions that use this same mutex, but that's fine.

The prior algorithm counted anything to produce immediately into the
buffered records and bytes fields; the fix for #777 could not really be
possible unless we avoid counting the "buffered" aspect right away.
Specifically, we need to have a goroutine looping with a sync.Cond that
checks *IF* we add the record, will we still be blocked? This allows us
to wake up all blocked goroutines always (unlike one at a time, the
problem this issue points out), and each goroutine can check under a
lock if they still do not fit.

This also fixes an unreported bug where, if a record WOULD be blocked
but fails early due to no topic / not in a transaction while in a
transactional client, the serial promise finishing goroutine would
deadlock.

Closes #777.
@twmb
Copy link
Owner Author

twmb commented Jul 29, 2024

Turns out the code was correct. The test needed to use newTestClient instead of NewClient.

I did find a small "optimization" at least, in all this looking.

@twmb twmb added the patch label Jul 29, 2024
@twmb twmb merged commit 718591a into master Jul 29, 2024
8 checks passed
@twmb twmb deleted the 777 branch July 29, 2024 04:40
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.

Deadlock in Produce() / TryProduce() when kgo.MaxBufferedBytes() is configured
1 participant