Skip to content

fix: atomic credential file writes to prevent corruption on crash (fixes #42)#52

Merged
jpoehnelt merged 1 commit intomainfrom
fix/atomic-credential-writes
Mar 4, 2026
Merged

fix: atomic credential file writes to prevent corruption on crash (fixes #42)#52
jpoehnelt merged 1 commit intomainfrom
fix/atomic-credential-writes

Conversation

@jpoehnelt
Copy link
Copy Markdown
Member

Fixes #42

Problem

All credential/token writes (credentials.enc, client_secret.json, token_cache.json) were non-atomic: a crash or Ctrl-C during the write could leave the file partially written or empty, permanently breaking auth.

Fix

  • Added src/fs_util.rs with atomic_write(path, data) (sync) and atomic_write_async(path, data) (tokio) helpers that write to a sibling .tmp file then rename() into place — an atomic operation on POSIX filesystems
  • Updated save_client_config in oauth_config.rs
  • Updated save_encrypted in credential_store.rs (the primary credentials file)
  • Updated save_to_disk in token_storage.rs (the token cache)

Tests

4 unit tests in fs_util.rs: creates file, overwrites existing, no leftover .tmp file, async variant

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 0603bce

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jpoehnelt jpoehnelt merged commit 77f0f04 into main Mar 4, 2026
21 checks passed
@jpoehnelt jpoehnelt deleted the fix/atomic-credential-writes branch March 4, 2026 08:01
shigechika pushed a commit to shigechika/gws-cli that referenced this pull request Mar 20, 2026
…-credential-writes

fix: atomic credential file writes to prevent corruption on crash (fixes googleworkspace#42)
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.

Bug: Race Condition in ~/.config/gws File Writes

1 participant