Skip to content

Server shutdown does not close files for in-process server #2759

@greschd

Description

@greschd

Before submitting the issue

  • I have checked for Compatibility issues
  • I have searched among the existing issues
  • I am using a Python virtual environment

Description of the bug

The following script fails when tearing down the temporary directory, because the file.rst is still open in DPF:

import os
import shutil
import tempfile
from ansys.dpf import core as dpf
from ansys.dpf.core import examples

server = dpf.start_local_server()

with tempfile.TemporaryDirectory() as tmp_dir:
    test_file = os.path.join(tmp_dir, "file.rst")
    shutil.copyfile(
        examples.find_simple_bar(), test_file
    )
    model = dpf.Model(test_file, server=server)

    results = model.results

    server.shutdown()

Exception:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\dgresch\\AppData\\Local\\Temp\\tmpgjjd87jg\\file.rst'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\tmp\pydpf_core_test\test2.py", line 10, in <module>
    with tempfile.TemporaryDirectory() as tmp_dir:
  File "C:\Program Files\Python310\lib\tempfile.py", line 869, in __exit__
    self.cleanup()
  File "C:\Program Files\Python310\lib\tempfile.py", line 873, in cleanup
    self._rmtree(self.name, ignore_errors=self._ignore_cleanup_errors)
  File "C:\Program Files\Python310\lib\tempfile.py", line 855, in _rmtree
    _shutil.rmtree(name, onerror=onerror)
  File "C:\Program Files\Python310\lib\shutil.py", line 750, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Program Files\Python310\lib\shutil.py", line 620, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Program Files\Python310\lib\tempfile.py", line 846, in onerror
    cls._rmtree(path, ignore_errors=ignore_errors)
  File "C:\Program Files\Python310\lib\tempfile.py", line 855, in _rmtree
    _shutil.rmtree(name, onerror=onerror)
  File "C:\Program Files\Python310\lib\shutil.py", line 750, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Program Files\Python310\lib\shutil.py", line 601, in _rmtree_unsafe
    onerror(os.scandir, path, sys.exc_info())
  File "C:\Program Files\Python310\lib\shutil.py", line 598, in _rmtree_unsafe
    with os.scandir(path) as scandir_it:
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\dgresch\\AppData\\Local\\Temp\\tmpgjjd87jg\\file.rst'

Steps To Reproduce

Run the script shown above.

Which Operating System causes the issue?

Windows

Which DPF/Ansys version are you using?

Ansys 2026 R1

Which Python version causes the issue?

3.10

Installed packages

ansys-dpf-core==0.14.2
certifi==2025.11.12
charset-normalizer==3.4.4
colorama==0.4.6
contourpy==1.3.2
cycler==0.12.1
fonttools==4.60.1
grpcio==1.76.0
idna==3.11
imageio==2.37.2
imageio-ffmpeg==0.6.0
importlib-metadata==8.7.0
kiwisolver==1.4.9
matplotlib==3.10.7
numpy==2.2.6
packaging==25.0
pillow==12.0.0
platformdirs==4.5.0
pooch==1.8.2
protobuf==6.33.1
psutil==7.1.3
pyparsing==3.2.5
python-dateutil==2.9.0.post0
pyvista==0.46.4
requests==2.32.5
scooby==0.11.0
setuptools==80.9.0
six==1.17.0
tqdm==4.67.1
typing-extensions==4.15.0
urllib3==2.5.0
vtk==9.5.2
zipp==3.23.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions