Skip to content

Fix createTempDirectory name generation#11872

Open
Bodigrim wants to merge 1 commit into
masterfrom
fix-createTempDirectory
Open

Fix createTempDirectory name generation#11872
Bodigrim wants to merge 1 commit into
masterfrom
fix-createTempDirectory

Conversation

@Bodigrim
Copy link
Copy Markdown
Collaborator

@Bodigrim Bodigrim commented May 23, 2026

The existing createTempDirectory uses a suboptimal schema to generate temporary directory names. Namely, it starts with foo-$processId. If there is no such folder that's it, otherwise it tries to create foo-($processId+1), then foo-($processId+2), etc.

While on the surface this is OK, in practice it puts strain on the file system. If we need to create N temporary folders, the name assignment will cycle starting from foo-$processid every time, totaling N^2 IO calls. There are also seem to be issues on Windows when one thread is removing its temporary folder, while another it trying to create the very same one.

It would be so much better if our temporary names were more diverse by construction. Yes, there might be an odd chance of a clash because someone else just accidentally created the very same folder, but we should not routinely make our own life harder.

Normally this is achieved by generating random numbers, but Cabal cannot depend on non-boot libraries such as random. So this patch uses a global counter. The approach is similar to one used by file-io for temporary files or by temporary-ospath.

(Both file-io and temporary-ospath actually have even more refined schema for name generation, getting entropy from CPU time, but I think in the interest of simplicity Cabal can get away just with a global counter)


Template Α: This PR modifies behaviour or interface

Include the following checklist in your PR:

@ffaf1
Copy link
Copy Markdown
Collaborator

ffaf1 commented May 24, 2026

I tested this on my machine and it works.
As now a changelog is missing.

@Bodigrim Bodigrim force-pushed the fix-createTempDirectory branch from 9d7fc11 to 5770b21 Compare May 24, 2026 11:24
@Bodigrim
Copy link
Copy Markdown
Collaborator Author

As now a changelog is missing.

@ffaf1 done.

@Bodigrim Bodigrim added merge me Tell Mergify Bot to merge and removed attention: needs-review labels May 24, 2026
The existing `createTempDirectory` uses a suboptimal schema to generate
temporary directory names. Namely, it starts with `foo-$processId`.
If there is no such folder that's it, otherwise it tries
to create `foo-($processId+1)`, then `foo-($processId+2)`, etc.

While on the surface this is OK, in practice it puts strain
on the file system. If we need to create N temporary folders,
the name assignment will cycle starting from `foo-$processid` every
time, totaling N^2 IO calls. There are also seem to be issues
on Windows when one thread is removing its temporary folder, while
another it trying to create the very same one.

It would be so much better if our temporary names were more diverse
by construction. Yes, there might be an odd chance of a clash because
someone else just accidentally created the very same folder, but we
should not routinely make our own life harder.

Normally this is achieved by generating random numbers, but Cabal cannot
depend on non-boot libraries such as `random`. So this patch uses a global
counter. The approach is similar to one used by [`file-io`](https://hackage-content.haskell.org/package/file-io-0.2.0/docs/src/System.File.Platform.html#tempCounter)
for temporary files or by [`temporary-ospath`](https://hackage-content.haskell.org/package/temporary-ospath-1.3/docs/src/System.IO.Temp.OsPath.html#tempDirectoryCounter).

(Both `file-io` and `temporary-ospath` actually have even more refined schema
for name generation, getting entropy from CPU time, but I think in the interest
of simplicity Cabal can get away just with a global counter)
@Bodigrim Bodigrim force-pushed the fix-createTempDirectory branch from 5770b21 to d8ba5e8 Compare May 24, 2026 11:53
@mergify mergify Bot added the ready and waiting Mergify is waiting out the cooldown period label May 24, 2026
@Bodigrim Bodigrim mentioned this pull request May 24, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge me Tell Mergify Bot to merge ready and waiting Mergify is waiting out the cooldown period

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants