Fix race condition in config handling
Description of the Change
The CLI encounters race conditions when handling config files if multiple commands are executed in parallel.
Currently, the CLI writes a new config for every command executed due to the code in command_parser.go. However, it also deletes all temporary config files when reading the config, leading to race conditions between multiple processes. This PR introduces two changes to mitigate this problem:
- Ignore "File Not Found" errors when attempting to delete a file. This change addresses the issue where multiple processes list and delete temporary files simultaneously.
- Only delete temp config files older than 5 minutes. This change resolves the problem where one process creates a temporary config that it later wants to rename, while a second process deletes this temp config in the meantime. Orphan files should be rare, as they are normally pruned by the signal handler, as seen in https://github.com/cloudfoundry/cli/blob/04df8ae6dd68b64c2080c863c1ddf4c3e06abcd6/util/configv3/write_config.go#L63-L66
Why Is This PR Valuable?
Enables usage of the CF CLI in scripts which execute commands in parallel
Applicable Issues
fixes #2232
How Urgent Is The Change?
Medium
Other Relevant Parties
Anyone using the CLI in scripts with parallel execution. Multiple users have encountered this issue, as discussed in #2232.
Related PRs
Will be added later after first feedback (main branch)
The committers listed above are authorized under a signed CLA.
- :white_check_mark: login: gururajsh / name: Shwetha Gururaj (324e1a1d5937b2486b82a31bba267b8ccb30351b)
- :white_check_mark: login: f-blass (dab1faf5afec4bc87857bbd056b62a287b453c2c, 85b5cdde872a343e6b2e4f86a3a700fea47441b4, 4bcc7c5f795d911dd6b210fec31bfc15fdf96f72, 9658c254ef37c4a5c5d044f4c621c602c193038c)
Hi @f-blass,
Thank you for your contribution. Looks like some tests need to be addressed as part of this fix. Can you please look into it.
Hi @gururajsh thank you! Should be fixed now.
Hi @gururajsh thank you. PRs for v7 and main branch are created and linked.