From b075aa0dd26105bb4589ba9ddee3e350a2f0a13b Mon Sep 17 00:00:00 2001 From: Sebastian Mohr Date: Mon, 9 Mar 2026 13:55:25 +0100 Subject: [PATCH 1/3] Changed heuristic for archive checks to only use file extensions. --- backend/beets_flask/disk.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/backend/beets_flask/disk.py b/backend/beets_flask/disk.py index 3e71e31f..1bd7c97d 100644 --- a/backend/beets_flask/disk.py +++ b/backend/beets_flask/disk.py @@ -1,5 +1,6 @@ from __future__ import annotations +from functools import cache import os import re import subprocess @@ -222,9 +223,37 @@ def from_path( ) +@cache +def allowed_archive_extensions() -> list[str]: + # Tar files + ext = [".tar", ".tar.gz", ".tgz", ".tar.bz2", ".tbz2", ".tar.xz", ".txz"] + # Zip files + ext += [".zip"] + try: + from rarfile import RarFile + + ext += [".rar"] + except ImportError: + pass + + try: + from py7zr import SevenZipFile + + ext += [".7z"] + except ImportError: + pass + return ext + + def is_archive_file(path: Path | str) -> bool: - """Check if a file is an archive file based on its extension.""" - return ArchiveImportTask.is_archive(str(path)) + """Check if a file is an archive file based on its extension. + + It seems like there is a memory issue with `tarfile.is_tarfile` + (see https://github.com/pSpitzner/beets-flask/issues/258). We try + to avoid this by only checking the file extension here, and not trying to open the file. + """ + allowed_extensions = allowed_archive_extensions() + return Path(path).suffix.lower() in allowed_extensions @dataclass From 5e1f8f255fe329381eb638062ec2f058ccbfd31f Mon Sep 17 00:00:00 2001 From: Sebastian Mohr Date: Mon, 9 Mar 2026 13:57:43 +0100 Subject: [PATCH 2/3] Added changelog entry. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1d3068d..4d417d9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Missing library stats dont cause a crash on first launch anymore [#264](https://github.com/pSpitzner/beets-flask/issues/264) +- Fixed a potential memory leak when checking if files are archives. We now only check the file extension instead of trying to open the file, which should avoid the issue with `tarfile.is_tarfile` [#258](https://github.com/pSpitzner/beets-flask/issues/258) ### Other (dev) From 539ceffd8e37ebb9cf6846275c6e73a56d3878be Mon Sep 17 00:00:00 2001 From: Sebastian Mohr Date: Mon, 9 Mar 2026 14:12:26 +0100 Subject: [PATCH 3/3] Fixed ruff issue. --- backend/beets_flask/disk.py | 19 ++++--------------- docs/develop/contribution.md | 4 ++-- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/backend/beets_flask/disk.py b/backend/beets_flask/disk.py index 1bd7c97d..557dc67d 100644 --- a/backend/beets_flask/disk.py +++ b/backend/beets_flask/disk.py @@ -1,6 +1,6 @@ from __future__ import annotations -from functools import cache +import importlib.util import os import re import subprocess @@ -8,14 +8,12 @@ from collections.abc import Iterator, Sequence from dataclasses import dataclass from fnmatch import fnmatch +from functools import cache from pathlib import Path from typing import ( Literal, ) -from beets.importer import ( - ArchiveImportTask, -) from beets.importer.tasks import ( MULTIDISC_MARKERS, MULTIDISC_PAT_FMT, @@ -229,19 +227,10 @@ def allowed_archive_extensions() -> list[str]: ext = [".tar", ".tar.gz", ".tgz", ".tar.bz2", ".tbz2", ".tar.xz", ".txz"] # Zip files ext += [".zip"] - try: - from rarfile import RarFile - + if importlib.util.find_spec("rarfile") is not None: ext += [".rar"] - except ImportError: - pass - - try: - from py7zr import SevenZipFile - + if importlib.util.find_spec("py7zr") is not None: ext += [".7z"] - except ImportError: - pass return ext diff --git a/docs/develop/contribution.md b/docs/develop/contribution.md index 0a2f9fc2..6dc5e624 100644 --- a/docs/develop/contribution.md +++ b/docs/develop/contribution.md @@ -68,8 +68,8 @@ Run [Ruff](https://docs.astral.sh/ruff/) manually or use the pre-commit hooks to ```bash cd backend # Code formatting and linting -ruff check -ruff lint +ruff check . --fix +ruff format . # Typing checks mypy . # Run the tests