Skip to content

fix(settings): replace configuration files atomically - #1072

Open
abhinavrastogi2627 wants to merge 1 commit into
tursodatabase:mainfrom
abhinavrastogi2627:fix/atomic-settings-write
Open

abhinavrastogi2627 wants to merge 1 commit into
tursodatabase:mainfrom
abhinavrastogi2627:fix/atomic-settings-write

Conversation

@abhinavrastogi2627

Copy link
Copy Markdown

Problem

Settings persistence currently truncates settings.json before writing it. Another CLI process can read an empty or partial document, and a serialization error can erase the previous valid configuration.

Change

Write to a mode-0600 temporary file in the destination directory, sync and close it, then replace the settings file by rename. Preserve a configured symlink by replacing its target, and remove temporary files on failure. Record the resolved configuration path after initial loading/creation so the first save also uses this path.

Validation

  • Two new regressions failed on the original implementation: an already-open reader saw the replacement contents, and a serialization failure left an empty configuration.
  • All five settings tests pass with go test -race ./internal/settings -count=10, including original permissions coverage, symlink preservation and failed-replacement cleanup.
  • On macOS, go test ./... has one failure, TestGetReservedBytes (12 versus expected 0); the same test fails identically on unchanged upstream cc95e98. The complete suite passes when that known baseline failure is skipped.
  • go vet ./..., go build ./..., Staticcheck 2025.1.1, the workflow's pinned sqlclosecheck, and git diff --check pass. go mod tidy produces no dependency changes.

Limits

Atomic replacement prevents torn reads; this does not merge concurrent writers' independent updates. Rename atomicity depends on the operating system/filesystem, and this change does not claim power-loss durability of the parent directory or Windows validation. Initial configuration creation and explicit --reset-config retain their existing behavior.

Fixes #911

Write settings to a private temporary file beside the destination, then sync, close and rename it. Existing readers keep the previous complete JSON, and serialization failures leave the saved configuration intact. Preserve settings symlinks and clean up temporary files on errors.

Refs tursodatabase#911

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.

Configuration file gets corrupted

1 participant