Skip to content

Unbounded growth in SpooledTemporaryFile.writelines() #127371

Closed
@bertptrs

Description

@bertptrs

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

Metadata

Metadata

Assignees

Labels

type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions