Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unbounded growth in SpooledTemporaryFile.writelines() #127371

Open
bertptrs opened this issue Nov 28, 2024 · 0 comments
Open

Unbounded growth in SpooledTemporaryFile.writelines() #127371

bertptrs opened this issue Nov 28, 2024 · 0 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@bertptrs
Copy link

bertptrs commented Nov 28, 2024

Bug report

Bug description:

SpooledTemporaryFile provides a temporary file backed by a buffer in memory that spills over to disk when it gets too large. However, the writelines() method only checks whether it should roll over after the entire lines iterator is exhausted. This causes unexpectedly high memory use when feeding it large iterators.

from tempfile import SpooledTemporaryFile

with SpooledTemporaryFile(mode="w", max_size=1024) as temp:
    temp.writelines(map(str, range(1000)))

With the above code, one might expect that the buffer doesn't grow (much) past 1024 bytes, but it grows to almost three times that size before finally rolling over.

CPython versions tested on:

3.12, 3.13, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant