Improve `tarfile` streaming mode to handle very large archives
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
I am trying to use tarfile to write very large archives, and the process is being killed by the OOM killer. I would expect+want to be able to use it in streaming mode (write an unlimited number of files) like the standard tar command line utility supports by default.
Reproduction case attached.
import gc
import io
import os
import psutil
import tarfile
if __name__ == "__main__":
t = tarfile.open("a.tar", mode="w") # default compresslevel 9
for i in range(1,100_000_000):
if i % 10_000 == 0:
gc.collect()
process = psutil.Process(os.getpid())
mem_info = process.memory_info()
mem = mem_info.rss
print(f"Iteration {i}, memory usage: {mem}")
bs = (" "*1000 + str(i)).encode('utf8')
with io.BytesIO(bs) as file:
tarinfo = tarfile.TarInfo(name="cool_files/{i}.txt")
tarinfo.size = len(bs)
t.addfile(tarinfo, file)
The memory usage increases without bound because the list of this line in addfile():
self.members.append(tarinfo)
I'm not sure what the use-case is for this line. In write-only mode, it does not seem useful. Maybe mixed read/write? But in general it does not seem correct to assume you can fit all the tarinfo's in memory.
Edit: As a workaround, I'm setting t.members=[] manually.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the tarfile implementation around TarFile.addfile() and the self.members.append(tarinfo) line, using the reported reproduction to observe memory growth. Determine the intended behavior for write-only or streaming mode, then verify that writing very large archives keeps memory bounded without breaking other tarfile use cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100