From 99214033275234793ecde098f36e7c30e66382c7 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Tue, 19 Oct 2021 14:05:36 +0530 Subject: [PATCH 1/4] Don't open file twice when downloading conda Unnecessary, and could also possibly cause races - see https://github.com/jupyterhub/the-littlest-jupyterhub/pull/710#issuecomment-946165463 --- tljh/conda.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tljh/conda.py b/tljh/conda.py index 3fa8a87..ee8568c 100644 --- a/tljh/conda.py +++ b/tljh/conda.py @@ -49,9 +49,8 @@ def download_miniconda_installer(installer_url, sha256sum): of given version, verifies the sha256sum & provides path to it to the `with` block to run. """ - with tempfile.NamedTemporaryFile() as f: - with open(f.name, 'wb') as f: - f.write(requests.get(installer_url).content) + with tempfile.NamedTemporaryFile('wb') as f: + f.write(requests.get(installer_url).content) if sha256_file(f.name) != sha256sum: raise Exception('sha256sum hash mismatch! Downloaded file corrupted') From 63ced8eb7dbb9c1eda247d9f48bca3c1351d3e2a Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Tue, 19 Oct 2021 15:07:44 +0530 Subject: [PATCH 2/4] Flush conda installer to disk before calculating sha --- tljh/conda.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tljh/conda.py b/tljh/conda.py index ee8568c..558c218 100644 --- a/tljh/conda.py +++ b/tljh/conda.py @@ -51,6 +51,8 @@ def download_miniconda_installer(installer_url, sha256sum): """ with tempfile.NamedTemporaryFile('wb') as f: f.write(requests.get(installer_url).content) + f.flush() + os.fsync() if sha256_file(f.name) != sha256sum: raise Exception('sha256sum hash mismatch! Downloaded file corrupted') From 6d6ec61102657e4eb12f819164a00fccb72ae56c Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Tue, 19 Oct 2021 12:24:34 +0200 Subject: [PATCH 3/4] Apply suggestions from code review --- tljh/conda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tljh/conda.py b/tljh/conda.py index 558c218..1dbeb55 100644 --- a/tljh/conda.py +++ b/tljh/conda.py @@ -52,7 +52,7 @@ def download_miniconda_installer(installer_url, sha256sum): with tempfile.NamedTemporaryFile('wb') as f: f.write(requests.get(installer_url).content) f.flush() - os.fsync() + os.fsync(f) if sha256_file(f.name) != sha256sum: raise Exception('sha256sum hash mismatch! Downloaded file corrupted') From de146690add072ca53ef11f6cb421eed45958da0 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Tue, 19 Oct 2021 13:47:11 +0200 Subject: [PATCH 4/4] Apply suggestions from code review --- tljh/conda.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tljh/conda.py b/tljh/conda.py index 1dbeb55..1ffe7ff 100644 --- a/tljh/conda.py +++ b/tljh/conda.py @@ -51,8 +51,10 @@ def download_miniconda_installer(installer_url, sha256sum): """ with tempfile.NamedTemporaryFile('wb') as f: f.write(requests.get(installer_url).content) + # Remain in the NamedTemporaryFile context, but flush changes, see: + # https://docs.python.org/3/library/os.html#os.fsync f.flush() - os.fsync(f) + os.fsync(f.fileno()) if sha256_file(f.name) != sha256sum: raise Exception('sha256sum hash mismatch! Downloaded file corrupted')