Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (C) 2026 by the GRASS Development Team
# SPDX-License-Identifier: GPL-2.0-or-later

"""Open a vector map in update mode, close it, and check for a leaked sidx handle

Run in a GRASS session by ``grass_pygrass_vector_sidx_test.py`` in a
subprocess, because the vector library state is per process.
"""

import os
import sys
from pathlib import Path

from grass.pygrass.vector import VectorTopo


def sidx_is_open(path):
"""Return True if this process still has the file open"""
if sys.platform.startswith("linux"):
# A handle to a replaced file is reported with a " (deleted)" suffix.
return any(
os.path.realpath(fd).startswith(path)
for fd in Path("/proc/self/fd").iterdir()
)
# Windows refuses to delete an open file.
try:
os.remove(path)
except PermissionError:
return True
return False


def main():
"""Create a map, reopen it in update mode, and check the sidx file"""
vect = VectorTopo("sidx_map")
vect.open("w")
vect.close()
vect.open("rw")
vect.close()
mapset_path = os.environ["MAPSET_PATH"]
path = os.path.realpath(os.path.join(mapset_path, "vector", "sidx_map", "sidx"))
if sidx_is_open(path):
print(f"The file {path} was left open after closing the map")
return 1
return 0


if __name__ == "__main__":
sys.exit(main())
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (C) 2026 by the GRASS Development Team
# SPDX-License-Identifier: GPL-2.0-or-later

"""Test that closing a vector map opened in update mode releases the sidx file

An open file handle left behind makes the removal of the map fail on Windows
and leaks a file descriptor on all platforms.
"""

import os
import subprocess
import sys
from pathlib import Path

import pytest

import grass.script as gs

CHECKS = Path(__file__).with_name("grass_pygrass_vector_sidx_checks.py")


@pytest.fixture(scope="module")
def session(tmp_path_factory):
"""Active session in an XY project (scope: module)"""
project = tmp_path_factory.mktemp("sidx") / "project"
gs.create_project(project)
with gs.setup.init(project, env=os.environ.copy()) as session:
yield session


def test_update_mode_releases_sidx(session):
"""The sidx file is closed after a map opened with "rw" is closed"""
env = session.env.copy()
env["MAPSET_PATH"] = str(
Path(gs.gisenv(env=session.env)["GISDBASE"])
/ gs.gisenv(env=session.env)["LOCATION_NAME"]
/ gs.gisenv(env=session.env)["MAPSET"]
)
result = subprocess.run(
[sys.executable, str(CHECKS)],
env=env,
capture_output=True,
text=True,
check=False,
timeout=120,
)
assert result.returncode == 0, f"{result.stdout}\n{result.stderr}"

Check failure on line 47 in python/grass/pygrass/vector/tests/grass_pygrass_vector_sidx_test.py

View workflow job for this annotation

GitHub Actions / pytest (ubuntu-24.04, 3.13)

test_update_mode_releases_sidx AssertionError: The file /tmp/pytest-of-runner/pytest-0/popen-gw1/sidx0/project/PERMANENT/vector/sidx_map/sidx was left open after closing the map Building topology for vector map <sidx_map@PERMANENT>... Registering primitives... Building topology for vector map <sidx_map@PERMANENT>... assert 1 == 0 + where 1 = CompletedProcess(args=['/opt/hostedtoolcache/Python/3.13.16/x64/bin/python', '/home/runner/work/grass/grass/python/grass/pygrass/vector/tests/grass_pygrass_vector_sidx_checks.py'], returncode=1, stdout='The file /tmp/pytest-of-runner/pytest-0/popen-gw1/sidx0/project/PERMANENT/vector/sidx_map/sidx was left open after closing the map\n', stderr='Building topology for vector map <sidx_map@PERMANENT>...\nRegistering primitives...\n\nBuilding topology for vector map <sidx_map@PERMANENT>...\n').returncode

Check failure on line 47 in python/grass/pygrass/vector/tests/grass_pygrass_vector_sidx_test.py

View workflow job for this annotation

GitHub Actions / pytest (ubuntu-24.04, 3.11)

test_update_mode_releases_sidx AssertionError: The file /tmp/pytest-of-runner/pytest-0/popen-gw1/sidx0/project/PERMANENT/vector/sidx_map/sidx was left open after closing the map Building topology for vector map <sidx_map@PERMANENT>... Registering primitives... Building topology for vector map <sidx_map@PERMANENT>... assert 1 == 0 + where 1 = CompletedProcess(args=['/opt/hostedtoolcache/Python/3.11.17/x64/bin/python', '/home/runner/work/grass/grass/python/grass/pygrass/vector/tests/grass_pygrass_vector_sidx_checks.py'], returncode=1, stdout='The file /tmp/pytest-of-runner/pytest-0/popen-gw1/sidx0/project/PERMANENT/vector/sidx_map/sidx was left open after closing the map\n', stderr='Building topology for vector map <sidx_map@PERMANENT>...\nRegistering primitives...\n\nBuilding topology for vector map <sidx_map@PERMANENT>...\n').returncode
Loading