Skip to content

Opening an empty (0-byte) dfs file leaves the file open; after ~509 such opens no file can be opened (error 2003) #85

Closed

Description

@buchmann-dhi

Opening a 0-byte dfs file fails as expected (Error code 2001), but the file stays open in the process. It cannot be deleted until the process exits, and each failed open uses up one more file slot. After 509 failed opens the error changes to Error code 2003, and from then on valid dfs files cannot be opened either.
Reproduction (mikecore only):

import sys
import tempfile
from pathlib import Path

import mikecore
from mikecore.DfsFileFactory import DfsFileFactory

print(f"mikecore {mikecore.__version__}, Python {sys.version.split()[0]}, {sys.platform}")
empty = Path(tempfile.mkdtemp()) / "empty.dfs0"
empty.write_bytes(b"")

message = ""
for attempt in range(1, 601):
    try:
        DfsFileFactory.DfsGenericOpen(str(empty))
    except Exception as error:
        if str(error) != message:           # print the message each time it changes
            message = str(error)
            print(f"open {attempt}: {message.replace(str(empty), '<file>')}")

try:
    empty.unlink()
    print("the file can be deleted")
except OSError as error:
    print(f"the file cannot be deleted: {error.strerror}")

Output:

mikecore 0.2.2, Python 3.12.10, win32
open 1: Could not load file <file> (Error code 2001)
open 510: Could not load file <file> (Error code 2003)
the file cannot be deleted: The process cannot access the file because it is being used by another process

Expected: a failed open leaves no file open.

Notes:

  • A single failed open is enough to keep the file locked.
  • A non-empty file that is not a dfs file (Error code 2014) does not show this.
  • It looks like dfsFileRead does not close the file when it returns 2001; DfsFile.Open raises on the non-zero return code and has nothing to close.
  • A possible stopgap in DfsFile.Open: refuse a 0-byte file before calling dfsFileRead.
  • Seen through mikeio.read (mikeio 3.1.0) in a long-running process that checks many files; only tested on Windows.

Activity

  1. ryan-kipawa commented on Nov 11, 2026

    @ryan-kipawa
    Collaborator

    Thanks for the clear report and the reproduction script! Fixed in #87: files shorter than 4 bytes (not only empty ones, since 1–3 bytes leaked the same way) are now refused before the native library opens them, with the same error 2001, so the file is no longer left open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions