Loading...
Loading...
### Terraform Version ```shell Terraform v1.6.4 on linux_amd64 ``` ### Terraform Configuration Files N/A ### Debug Output N/A ### Expected Behavior **Note:** I originally thought this was a `cdktf` bug but it seems this issue is related to `terraform` directly. The expected behaviour and reproduction steps still used `cdktf` although it shouldn't be necessary to reproduce. --- I am running `cdktf destroy` via a python using `subprocess.run`. The exact command is ```python import subprocess subprocess.run(["cdktf", "destroy", "--auto-approve"], check=True) ``` When this is destroying instances, pressing `ctrl +c` should stop the destroy and leave the `terraform..tfstate` file with an accurate list of state. ### Actual Behavior The `terraform..tfstate` file is empty. ### Steps to Reproduce **Note:** The below instructions are one way to induce the issue but it is likely there are others. Using raw `terraform` it should be possible if the state is large and the `destroy` command is exited forcefully enough. --- 1. Start an instance with a lot of resources. For example many files. 2. Run `cdktf destroy` via python ```python import subprocess subprocess.run(["cdktf", "destroy", "--auto-approve"], check=True) ``` 4. When the resources are being destroyed, press `ctrl +c`. It seems like the `python` subprocess call is required for the state to be emptied permanently, however using a raw `cdktf destroy` I noticed that the state file is briefly emptied and then recreated. This seems dependent on the size of the state. I can reliably reproduce this with a ~5MB state file. ### Additional Context I believe this issue is related to the `TODO` comment here https://github.com/hashicorp/terraform/blob/1f9734619f953ecc7252d7b98a6d40d751b4ea1e/internal/states/statemgr/filesystem.go#L124C1-L129C18 With a large state file, an interrupt can happen between [clearing the file](https://github.com/hashicorp/terraform/blob/1f9734619f953ecc7252d7b98a6d40d751b4ea1e/internal/states/statemgr/filesystem.go#L194), and the updated state being completely [written](https://github.com/hashicorp/terraform/blob/1f9734619f953ecc7252d7b98a6d40d751b4ea1e/internal/states/statemgr/filesystem.go#L219). The solution is likely what the comment suggests. Write the state file to a temporary file and then do a "swap" to update the state file. This would ensure the state file is never in a "corrupted" state. ### References I originally reported this against under cdktf https://github.com/hashicorp/terraform-cdk/issues/3269
Click on a version to see all relevant bugs
Terraform Integration
Learn more about where this data comes from
Bug Scrub Advisor
Streamline upgrades with automated vendor bug scrubs
BugZero Enterprise
Wish you caught this bug sooner? Get proactive today.