Skip to content

Commit 0f915d1

Browse files
authored
Use unique tmp file names to avoid race conditions (#1119)
1 parent 81de4e7 commit 0f915d1

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/explorer/polars_backend/shared.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ defmodule Explorer.PolarsBackend.Shared do
269269
Builds and returns a path for a new file.
270270
271271
It saves in a directory called "elixir-explorer-datasets" inside
272-
the `System.tmp_dir()`.
272+
the `System.tmp_dir()`, the file name contains a random component to avoid collisions.
273273
"""
274274
def build_path_for_entry(%FSS.S3.Entry{} = entry) do
275275
bucket = entry.config.bucket || "default-explorer-bucket"
@@ -278,15 +278,18 @@ defmodule Explorer.PolarsBackend.Shared do
278278
:crypto.hash(:sha256, entry.config.endpoint <> "/" <> bucket <> "/" <> entry.key)
279279
|> Base.url_encode64(padding: false)
280280

281-
id = "s3-file-#{hash}"
281+
rand = Base.url_encode64(:crypto.strong_rand_bytes(8), padding: false)
282+
283+
id = "s3-file-#{hash}-#{rand}"
282284

283285
build_tmp_path(id)
284286
end
285287

286288
def build_path_for_entry(%FSS.HTTP.Entry{} = entry) do
287289
hash = :crypto.hash(:sha256, entry.url) |> Base.url_encode64(padding: false)
290+
rand = Base.url_encode64(:crypto.strong_rand_bytes(8), padding: false)
288291

289-
id = "http-file-#{hash}"
292+
id = "http-file-#{hash}-#{rand}"
290293

291294
build_tmp_path(id)
292295
end

0 commit comments

Comments
 (0)