From 3135a0f78b8251cfb74cf447436371f615ce74e3 Mon Sep 17 00:00:00 2001 From: Ryan Duguid Date: Sat, 17 Oct 2026 17:42:08 +1100 Subject: [PATCH 1/2] fix: create owned downloads exclusively --- requests_toolbelt/downloadutils/stream.py | 9 ++++-- tests/test_download_creation_race.py | 39 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/test_download_creation_race.py diff --git a/requests_toolbelt/downloadutils/stream.py b/requests_toolbelt/downloadutils/stream.py index 7253d96e..ea4f9723 100644 --- a/requests_toolbelt/downloadutils/stream.py +++ b/requests_toolbelt/downloadutils/stream.py @@ -163,9 +163,12 @@ def stream_response_to_file(response, path=None, chunksize=_DEFAULT_CHUNKSIZE): filename = getattr(fd, 'name', None) else: filename = get_download_file_path(response, path) - if os.path.exists(filename): - raise exc.StreamingError("File already exists: %s" % filename) - fd = open(filename, 'wb') + try: + fd = open(filename, 'xb') + except FileExistsError as error: + raise exc.StreamingError( + "File already exists: %s" % filename + ) from error for chunk in response.iter_content(chunk_size=chunksize): fd.write(chunk) diff --git a/tests/test_download_creation_race.py b/tests/test_download_creation_race.py new file mode 100644 index 00000000..8c2fce07 --- /dev/null +++ b/tests/test_download_creation_race.py @@ -0,0 +1,39 @@ +"""A competing creator must never lose its existing bytes.""" +from io import BytesIO +from unittest.mock import Mock + +import pytest + +from requests_toolbelt.downloadutils import stream +from requests_toolbelt.exceptions import StreamingError + + +def response(): + result = Mock() + result.iter_content.return_value = [b"download"] + return result + + +def test_competing_file_creation_preserves_winner(tmp_path, monkeypatch): + target = tmp_path / "download.bin" + real_open = open + + def race(path, mode): + with real_open(path, "xb") as winner: + winner.write(b"competing file") + return real_open(path, mode) + + monkeypatch.setattr(stream, "open", race, raising=False) + with pytest.raises(StreamingError, match="File already exists"): + stream.stream_response_to_file(response(), str(target)) + assert target.read_bytes() == b"competing file" + + +def test_new_file_and_caller_owned_stream(tmp_path): + target = tmp_path / "download.bin" + assert stream.stream_response_to_file(response(), str(target)) == str(target) + assert target.read_bytes() == b"download" + supplied = BytesIO() + assert stream.stream_response_to_file(response(), supplied) is None + assert not supplied.closed + assert supplied.getvalue() == b"download" From 5b9313696477c5f04c79b67f43267358646c3322 Mon Sep 17 00:00:00 2001 From: Ryan Duguid Date: Tue, 27 Oct 2026 14:40:30 +1100 Subject: [PATCH 2/2] fix: keep exclusive download creation working on Python 2.7 Use os.open with O_CREAT | O_EXCL instead of the 'x' file mode, and map EEXIST from that call to the existing StreamingError, as Python 2.7 has neither the 'x' mode nor raise ... from. Wrap the descriptor with os.fdopen separately and close it if wrapping fails. --- requests_toolbelt/downloadutils/stream.py | 19 +++++++--- tests/test_download_creation_race.py | 44 +++++++++++++++++++---- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/requests_toolbelt/downloadutils/stream.py b/requests_toolbelt/downloadutils/stream.py index ea4f9723..f90854d8 100644 --- a/requests_toolbelt/downloadutils/stream.py +++ b/requests_toolbelt/downloadutils/stream.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """Utilities for dealing with streamed requests.""" +import errno import os.path import re @@ -13,6 +14,9 @@ _QUOTED_STRING_RE) ) _DEFAULT_CHUNKSIZE = 512 +# Create the file only if it does not exist yet, as one atomic step. +_EXCLUSIVE_CREATE_FLAGS = (os.O_WRONLY | os.O_CREAT | os.O_EXCL | + getattr(os, 'O_BINARY', 0)) def _get_filename(content_disposition): @@ -164,11 +168,16 @@ def stream_response_to_file(response, path=None, chunksize=_DEFAULT_CHUNKSIZE): else: filename = get_download_file_path(response, path) try: - fd = open(filename, 'xb') - except FileExistsError as error: - raise exc.StreamingError( - "File already exists: %s" % filename - ) from error + descriptor = os.open(filename, _EXCLUSIVE_CREATE_FLAGS, 0o666) + except OSError as error: + if error.errno != errno.EEXIST: + raise + raise exc.StreamingError("File already exists: %s" % filename) + try: + fd = os.fdopen(descriptor, 'wb') + except BaseException: + os.close(descriptor) + raise for chunk in response.iter_content(chunk_size=chunksize): fd.write(chunk) diff --git a/tests/test_download_creation_race.py b/tests/test_download_creation_race.py index 8c2fce07..99b467c2 100644 --- a/tests/test_download_creation_race.py +++ b/tests/test_download_creation_race.py @@ -1,6 +1,11 @@ """A competing creator must never lose its existing bytes.""" +import os from io import BytesIO -from unittest.mock import Mock + +try: + from unittest.mock import Mock +except ImportError: + from mock import Mock import pytest @@ -16,19 +21,46 @@ def response(): def test_competing_file_creation_preserves_winner(tmp_path, monkeypatch): target = tmp_path / "download.bin" - real_open = open + real_os_open = os.open - def race(path, mode): - with real_open(path, "xb") as winner: + def race(path, flags, mode=0o777): + with open(path, "wb") as winner: winner.write(b"competing file") - return real_open(path, mode) + return real_os_open(path, flags, mode) - monkeypatch.setattr(stream, "open", race, raising=False) + monkeypatch.setattr(stream.os, "open", race) with pytest.raises(StreamingError, match="File already exists"): stream.stream_response_to_file(response(), str(target)) assert target.read_bytes() == b"competing file" +def test_descriptor_is_closed_when_wrapping_fails(tmp_path, monkeypatch): + target = tmp_path / "download.bin" + real_os_open = os.open + real_os_close = os.close + opened = [] + closed = [] + + def record_open(path, flags, mode=0o777): + opened.append(real_os_open(path, flags, mode)) + return opened[-1] + + def record_close(descriptor): + closed.append(descriptor) + real_os_close(descriptor) + + def fail_fdopen(descriptor, mode): + raise MemoryError("synthetic fdopen failure") + + monkeypatch.setattr(stream.os, "open", record_open) + monkeypatch.setattr(stream.os, "close", record_close) + monkeypatch.setattr(stream.os, "fdopen", fail_fdopen) + with pytest.raises(MemoryError, match="synthetic fdopen failure"): + stream.stream_response_to_file(response(), str(target)) + assert len(opened) == 1 + assert closed == opened + + def test_new_file_and_caller_owned_stream(tmp_path): target = tmp_path / "download.bin" assert stream.stream_response_to_file(response(), str(target)) == str(target)