`ConfigurationFile.write` can truncate existing configuration when writing fails
Checklist
- I added a descriptive title
- I searched open reports and couldn't find a duplicate
What happened?
ConfigurationFile.write() writes directly over an existing configuration file. If the write fails after truncation, it raises CondaError but leaves the previous configuration replaced by partial YAML.
The following POSIX reproduction limits the size of files written by this Python process. It only edits a temporary file and restores the original limit afterward.
import resource
import signal
from pathlib import Path
from tempfile import TemporaryDirectory
from conda import CondaError
from conda.cli.condarc import ConfigurationFile
with TemporaryDirectory() as directory:
path = Path(directory) / "condarc"
path.write_text("channels:\n - conda-forge\n")
config = ConfigurationFile(path)
config.set_key("proxy_servers.https", "https://example.org/" + "x" * 256)
previous_limit = resource.getrlimit(resource.RLIMIT_FSIZE)
previous_handler = signal.signal(signal.SIGXFSZ, signal.SIG_IGN)
try:
resource.setrlimit(resource.RLIMIT_FSIZE, (32, previous_limit[1]))
try:
config.write()
except CondaError:
print("write failed")
finally:
resource.setrlimit(resource.RLIMIT_FSIZE, previous_limit)
signal.signal(signal.SIGXFSZ, previous_handler)
print(repr(path.read_text()))Actual output:
write failed
'channels:\n - conda-forge\nproxy_'Expected behavior is to report the failed write and leave the original configuration intact. The context manager is also documented as supporting atomic edits.
Conda Info
Reproduced with conda 26.5.0 on macOS. The reproduction uses POSIX resource limits to induce a real filesystem write failure.
Conda Config
The complete temporary configuration is shown above.
Conda list
No package installation or solver operation is involved.
Additional Context
Current main at 4b1f1fe1aec1511d87548125ff25076e60202fd2 calls yaml.write(..., path=path) from ConfigurationFile.write(). The YAML writer then uses Path.write_text() on the destination.
Source: conda/conda