[1.6.x] Fixed #21122 -- Improved clean up of test temp directory on Windows

- Fixed test that didn't close the files it opened
- Caught and handled exception when temp directory cannot be removed

Backport of 978e1351a6 of master