From bb8889db3ac54b3e0c2a27c24131e4a7e9381335 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Wed, 11 Nov 2026 13:12:00 +0100 Subject: [PATCH] Refuse dfs files shorter than 4 bytes before the native open The native dfsFileRead, dfsFileEdit and dfsFileAppend leave the file open when they can not read the first 4-byte tag (Error code 2001), and return null pointers, so DfsFile.Open has nothing to close. Each such open used up a file handle until no file could be opened (2003). DfsFile.Open now raises the same error for these files before calling the native library. Fixes #85 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 ++++ mikecore/DfsFile.py | 9 ++++++++- tests/test_dfs_basic.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ea5f6a..130cd4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- A failed open of a dfs file shorter than 4 bytes, such as an empty file, no + longer leaves the file open. Each such open used up a file handle, so after + about 500 of them no file could be opened (Error code 2003) + ([#85](https://github.com/DHI/mikecore-python/issues/85)). - `import mikecore` works with a MIKE installation's `bin` on `LD_LIBRARY_PATH`, for example after sourcing `mikevars.sh`. It failed with `undefined symbol: _Z13fnGetLanguagev`, because the bundled libraries diff --git a/mikecore/DfsFile.py b/mikecore/DfsFile.py index 1e987e3..a58a01d 100644 --- a/mikecore/DfsFile.py +++ b/mikecore/DfsFile.py @@ -5,7 +5,7 @@ import locale import numpy as np from mikecore.eum import * -from mikecore.DfsDLL import DfsDLL +from mikecore.DfsDLL import DfsDLL, DfsError from mikecore.eum import eumQuantity @@ -811,6 +811,13 @@ def Open(self, filename, mode=DfsFileMode.Read, parameters=None): "File is readonly and can not be opened for editing: " + filename ) + # The native library leaves the file open when it can not read the + # first 4-byte tag, so refuse such a file with the same error. + if os.path.getsize(filename) < 4: + raise Exception( + f"Could not load file {filename} (Error code {DfsError.F_ERR_READ})" + ) + if parameters is None: parameters = DfsParameters() diff --git a/tests/test_dfs_basic.py b/tests/test_dfs_basic.py index fb70f39..6c6e8c6 100644 --- a/tests/test_dfs_basic.py +++ b/tests/test_dfs_basic.py @@ -1,4 +1,6 @@ import ctypes +import os +import sys import pytest @@ -153,3 +155,30 @@ def test_item_quantity_descriptions(): assert ["Water Level", "Water Depth"] == [q.ItemDescription for q in quantities] assert ["meter", "meter"] == [q.UnitDescription for q in quantities] + + +def open_file_count(): + return len(os.listdir("/proc/self/fd")) + + +@pytest.mark.skipif(sys.platform != "linux", reason="counts /proc/self/fd") +@pytest.mark.parametrize("size, error_code", [(0, 2001), (3, 2001), (4, 2014)]) +@pytest.mark.parametrize( + "open_file", + [ + DfsFileFactory.DfsGenericOpen, + DfsFileFactory.DfsGenericOpenEdit, + DfsFileFactory.DfsGenericOpenAppend, + ], +) +def test_failed_open_of_short_file_leaves_no_file_open( + tmp_path, size, error_code, open_file +): + filename = tmp_path / "short.dfs0" + filename.write_bytes(b"x" * size) + before = open_file_count() + + with pytest.raises(Exception, match=rf"\(Error code {error_code}\)"): + open_file(str(filename)) + + assert open_file_count() == before