Weird Windows Error: The File Already Exists

[2 minute read]

I ran into a very strange Windows error a while back. It started when I tried to make a commit in GitHub for Windows and got an error message. In the error log, I found this:

2015-03-31 19:20:08.5723|ERROR|thread:31|OperatingSystemBridge|Could not create temp file
System.IO.IOException: The file exists.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.Path.InternalGetTempFileName(Boolean checkHost)
at GitHub.IO.OperatingSystemBridge.CreateTempFile()

As you can see from that entry, this happened about two months ago so I can’t remember if I was able to get around that error somehow and successfully make a commit, but I do know that shortly after GitHub borked out I got this message in SQL Server Management Studio when I executed any query at all:

An error occurred while executing batch. Error message is: The file exists.

What the hell is going on here, Computer?

Luckily, the SSMS error led me to this Stack Overflow post which indicated that temp files, of all things, might be the culprit. This seemed like a highly improbable root cause, but I figured, what’s the harm? I went ahead and deleted all the temp files I could from my box - amounting to multiple gigabytes of data - and, amazingly, the problems with GitHub and SSMS immediately cleared up.

You’d think it would would be pretty near impossible to max out your stash of temp files on a modern computer, but as it turns out Windows has a rather paltry limit of 65,535 files. As indicated on the System.IO.Path.GetTempFileName() documentation:

The GetTempFileName method will raise an IOException if it is used to create more than 65535 files without deleting previous temporary files…[or] if no unique temporary file name is available. To resolve this error, delete all unneeded temporary files.

And there you have it. The world makes sense, yet again.