Skip to content

cvd: Acquire exclusive flock in DataViewer::WithExclusiveLock - #3606

Queued
rmuthiah wants to merge 1 commit into
google:mainfrom
rmuthiah:fix-cvd-data-viewer-flock
Queued

rmuthiah wants to merge 1 commit into
google:mainfrom
rmuthiah:fix-cvd-data-viewer-flock

Conversation

@rmuthiah

Copy link
Copy Markdown
Collaborator

DataViewer::WithExclusiveLock passed LOCK_SH instead of LOCK_EX to LockBackingFile, allowing concurrent cvd writers to hold shared locks simultaneously while truncating (ftruncate(0)) and rewriting instance_database.binpb, which could interleave writes and corrupt the serialized cvd::PersistentData protobuf.

  • Acquire LOCK_EX in DataViewer::WithExclusiveLock.
  • Pass O_CLOEXEC when opening the backing file so child daemons do not inherit an open database file descriptor across exec.
  • Centralize the signal-masked overwrite helper in DataViewer::StoreData.
  • Add DataViewer::WithConditionalExclusiveLock so read-modify-write callers that leave data unchanged can skip truncating and rewriting the backing file.
  • Add data_viewer_test (instances/BUILD.bazel) covering LOCK_EX mutual exclusion, concurrent writer serialization, and no-op conditional locks.

Bug: b/554100008
Bug: b/586523782
Test: bazel test //cuttlefish/host/commands/cvd/instances/...

`DataViewer::WithExclusiveLock` passed `LOCK_SH` instead of `LOCK_EX` to
`LockBackingFile`, allowing concurrent `cvd` writers to hold shared locks
simultaneously while truncating (`ftruncate(0)`) and rewriting
`instance_database.binpb`, which could interleave writes and corrupt the
serialized `cvd::PersistentData` protobuf.

Acquire `LOCK_EX` in `WithExclusiveLock`, pass `O_CLOEXEC` when opening
the backing file so child daemons do not inherit an open database file
descriptor across `exec`, centralize the signal-masked overwrite helper
in `DataViewer::StoreData`, and add `WithConditionalExclusiveLock` so
read-modify-write callers that leave `data` unchanged can skip
truncating and rewriting the backing file.

Add `data_viewer_test` (`instances/BUILD.bazel`) to verify `LOCK_EX`
mutual exclusion, concurrent writer serialization, and no-op conditional
locks.

Bug: b/554100008
Bug: b/586523782
Test: bazel test //cuttlefish/host/commands/cvd/instances/...
@adelva1984
adelva1984 added this pull request to the merge queue Nov 19, 2026

This branch has not been deployed

No deployments
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.

2 participants