Improve `tarfile` streaming mode to handle very large archives
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dall’implementazione di tarfile intorno a TarFile.addfile() e alla riga self.members.append(tarinfo), utilizzando la riproduzione segnalata per osservare la crescita della memoria. Determina il comportamento previsto per la modalità di sola scrittura o streaming, quindi verifica che la scrittura di archivi molto grandi mantenga limitato l’uso della memoria senza compromettere gli altri casi d’uso di tarfile.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100