Skip to content

bgagent reports a valid login as corrupt when several CLI processes run at once #1080

Done

Description

@isadeks

Problem

bgagent can report a valid login as corrupt when several CLI processes run at once:

Error: Credentials file is corrupt. Run `bgagent login` to re-authenticate.

writeSecretFile (cli/src/config.ts:114-115) saves the credentials file with fs.writeFileSync, which truncates the file and then writes it. A refreshed token is saved this way. A second bgagent process that reads the file in between gets an empty or partial file, and loadCredentials (cli/src/config.ts:104) turns the JSON parse error into "corrupt".

Seen on 2026-11-24: scripts/microvm-acceptance.sh runs its scenarios in parallel, and each scenario calls bgagent. One scenario failed at its first CLI call with this error. Run on its own right after, it passed. The same write was on main before #1052.

Following the message's advice, bgagent login, does not help: the file was not corrupt. The next parallel run can fail the same way.

Fix

Write the file atomically. Write a temporary file in the same directory with the same mode, then fs.renameSync it over the credentials file. A rename within one file system replaces the file in one step, so a reader sees either the old file or the new one.

writeSecretFile is shared, so this covers the other secret files it saves as well. A test can check that the target is never seen truncated, or that the write goes through a temporary file.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions