Skip to content

Conversation

@snissn
Copy link

@snissn snissn commented Jan 14, 2026

This PR fixes a lock/unlock mismatch in BatchWithFlusher.Set and Delete that can cause a runtime panic when Write() returns an error on the flush path.

Both methods use defer b.mtx.Unlock() while also manually unlocking the mutex prior to calling Write(). If Write() fails, the function returns while the mutex is already unlocked, and the deferred Unlock() triggers:

fatal error: sync: unlock of unlocked mutex

Changes

  • Reworked the flush path to always re-acquire the mutex before returning.
  • Ensures that all return paths maintain the invariant expected by the deferred Unlock().

Impact

  • Converts a hard panic into normal error propagation.
  • Preserves existing behavior on successful writes.
  • Makes BatchWithFlusher safe when Write() can legitimately fail (e.g., alternative backends, injected faults, or stricter error handling).

Notes

This change intentionally does not alter higher-level error handling semantics for failed writes; it only ensures mutex correctness and stability.


If you want, I can also:

  • add a minimal unit test demonstrating the original panic,
  • tune the tone for Cosmos governance style,
  • or help you anticipate maintainer questions (e.g. about panic-safety or write-failure semantics).

BatchWithFlusher.Set and Delete could panic with
"sync: unlock of unlocked mutex" when the flush path was taken
and Write() returned an error.

Both methods used defer b.mtx.Unlock() while also manually
unlocking the mutex before calling Write(). On a Write() error,
the function returned while the mutex was already unlocked,
causing the deferred Unlock() to panic.

Rework the flush path to always re-acquire the mutex before
returning, ensuring lock/unlock invariants are preserved and
Write() errors propagate normally instead of crashing the process.
@snissn
Copy link
Author

snissn commented Jan 14, 2026

issue #1135

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant