See Redmine issue 2377
Problem code
Using with NamedTemporaryFile leaves behind deleted-but-open files. To fix, we should handle the cleanup manually.
with NamedTemporaryFile(dir=outdir, delete=False) as outpath:
try:
#...
finally:
os.remove(outpath.name)