Temp files are not removed after downloading providers (Windows)
When Terraform is downloading providers it creates temporary files in C:\Users\xxx\AppData\Local\Temp such as terraform-provider127643403. These files are never cleaned up by terraform and can eventually use up all available disk space if ran inside of a CI/CD process, which does not cache providers (see this example)
Terraform Version
Terraform v0.15.0
on windows_amd64
Terraform Configuration Files
provider "aws" {
region = "us-east-2"
}
Expected Behavior
Temporary files should be cleaned up after they are no longer needed.
Steps to Reproduce
- Use the configuration file provided above.
- Call
terraform init - Delete folder
.terraformand.terraform.lock.hclfile - Repeat
Notice that temporary files in C:\Users\xxx\AppData\Local\Temp are created, but never deleted.
Hello! Sorry for the long silence here.
At first glance this seems to be a duplicate of #25977, which was ostensibly fixed by #25990.
However, that fix went out in v0.13.1 and this issue is reported against v0.15.0, and so it seems like that solution was not complete enough.
One thing I notice is that the fix was to arrange to try to remove the directory once the installation process is complete, but nothing is actually testing whether that os.Remove operation succeeded. If there were any error during removal then the file would be left on disk as you described here.
This makes me think that this is a typical example of Windows' different treatment of filesystem consistency: Unix systems allow deleting a file that's open, by removing the directory entry but leaving the file itself intact until all of the open file handles are closed. Windows instead returns an error if you try to delete a file that's open elsewhere, and so if the actions here were happening in the wrong order (trying to delete the file while Terraform still has an open filehandle to it) then I expect the delete call would fail on Windows.
I don't have a Windows system available to try this on today, but I'm going to label this as "windows" to help us find it in future when working on Windows-related issues. I think the first step here is to modify the code that currently arranges to delete the file so we can determine if it's succeeding or not. If it isn't succeeding (which would confirm my theory above) then we can try to understand why it isn't succeeding, and fix it.
Thanks for reporting this and sorry it previously got lost in the heap!
In case it's of interest, the Windows API's CreateFile method includes a flag FILE_FLAG_DELETE_ON_CLOSE which tells the OS to automatically clean up the file the moment all handles to it are released (MS Docs). Of course, that assumes you're keeping at least one handle on the file for it's required existence.
Hope that hint's of some use / sorry if not... I'm not familiar enough with golang to say.
I have a Windows machine with over 100GB in the temp directory. Are there any plans to fix this issue in the near future? This issue has been open for over three years.
Are may exists any way to tell terraform to use another folder for the temp files as temporary workarround to avoid putting everything on the OS disk?
This is still a problem