Improve `tarfile` streaming mode to handle very large archives
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
tarfile の実装で TarFile.addfile() と self.members.append(tarinfo) の行を中心に調べ、報告された再現手順を使ってメモリの増加を観察します。write-only または streaming モードで意図されている動作を特定し、そのうえで、非常に大きなアーカイブを書き込んでもメモリ使用量が一定範囲に収まり、tarfile の他のユースケースを壊さないことを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100