fix: Serialize entry writes #61
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a rare but critical issue where overlapping
.apply()
and.commit()
operations on the same SafeBox instance could cause data corruption orAEADBadTagException
. The root cause was concurrent writes to the underlying blob store, especially whencommit()
arrived before a priorapply()
had finished persisting to disk.Implementation Details
This patch introduces serialized write sequencing via a per-instance
writeBarrier: AtomicReference<CompletableDeferred<Unit>>
. Each new write operation, from either.apply()
or.commit()
, will now:previousWriteBarrier.await()
).writeMutex
to ensure mutual exclusion.This guarantees that apply and commit operations are serialized and isolated, preventing overlapping writes and eliminating the observed race conditions.
Test Adjustments
closeWhenIdle
state test that assertedCLOSED
emissions. On the 1.1.x line,close()
andcloseWhenIdle()
are deprecated/no-op and will be fully cleaned up in 1.2.0 (see Enforce singleton SafeBox per file to improve fail-safety and prevent race conditions #58).Notes: No public API changes, behavior is limited to internal write sequencing and stability.
Closes #60