Skip to content

Tests fails to clean up test environment on Windows if test doesn't pass (PermissionError) #95

Open

Description

@Andrej730

If test passes, there's no issues

py -m pytest -p no:pytest-blender tests\multilspy\test_multilspy_python.py
# tests\multilspy\test_multilspy_python.py::test_multilspy_python_black PASSED

But if test fails, it fails with the error below and folder .multispy/hash is not removed.

To make it fail, let's add exception to this part:

async with lsp.start_server():
result = await lsp.request_definition(str(PurePath("src/black/mode.py")), 163, 4)
assert isinstance(result, list)
assert len(result) == 1

E.g.

        async with lsp.start_server():
            raise Exception
            result = await lsp.request_definition(str(PurePath("src/black/mode.py")), 163, 4)

Error:

tests\multilspy\test_multilspy_python.py::test_multilspy_python_black FAILED

================================================================================= FAILURES =================================================================================
_______________________________________________________________________ test_multilspy_python_black ________________________________________________________________________

    @pytest.mark.asyncio
    async def test_multilspy_python_black():
        """
        Test the working of multilspy with python repository - black
        """
        code_language = Language.PYTHON
        params = {
            "code_language": code_language,
            "repo_url": "https://github.com/psf/black/",
            "repo_commit": "f3b50e466969f9142393ec32a4b2a383ffbe5f23"
        }
        with create_test_context(params) as context:
            lsp = LanguageServer.create(context.config, context.logger, context.source_directory)

            # All the communication with the language server must be performed inside the context manager
            # The server process is started when the context manager is entered and is terminated when the context manager is exited.
            # The context manager is an asynchronous context manager, so it must be used with async with.
            async with lsp.start_server():
>               raise Exception
E               Exception

tests\multilspy\test_multilspy_python.py:31: Exception

During handling of the above exception, another exception occurred:

    @pytest.mark.asyncio
    async def test_multilspy_python_black():
        """
        Test the working of multilspy with python repository - black
        """
        code_language = Language.PYTHON
        params = {
            "code_language": code_language,
            "repo_url": "https://github.com/psf/black/",
            "repo_commit": "f3b50e466969f9142393ec32a4b2a383ffbe5f23"
        }
>       with create_test_context(params) as context:

tests\multilspy\test_multilspy_python.py:24:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
C:\software\Python311\Lib\contextlib.py:158: in __exit__
    self.gen.throw(typ, value, traceback)
tests\test_utils.py:36: in create_test_context
    shutil.rmtree(temp_extract_directory)
C:\software\Python311\Lib\shutil.py:787: in rmtree
    return _rmtree_unsafe(path, onerror)
C:\software\Python311\Lib\shutil.py:629: in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
C:\software\Python311\Lib\shutil.py:638: in _rmtree_unsafe
    onerror(os.rmdir, path, sys.exc_info())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

path = 'C:\\.multilspy\\eced8f41c8ca472697ff15b023cbb571\\black-f3b50e466969f9142393ec32a4b2a383ffbe5f23'
onerror = <function rmtree.<locals>.onerror at 0x0000020844806C00>

    def _rmtree_unsafe(path, onerror):
        try:
            with os.scandir(path) as scandir_it:
                entries = list(scandir_it)
        except OSError:
            onerror(os.scandir, path, sys.exc_info())
            entries = []
        for entry in entries:
            fullname = entry.path
            if _rmtree_isdir(entry):
                try:
                    if entry.is_symlink():
                        # This can only happen if someone replaces
                        # a directory with a symlink after the call to
                        # os.scandir or entry.is_dir above.
                        raise OSError("Cannot call rmtree on a symbolic link")
                except OSError:
                    onerror(os.path.islink, fullname, sys.exc_info())
                    continue
                _rmtree_unsafe(fullname, onerror)
            else:
                try:
                    os.unlink(fullname)
                except OSError:
                    onerror(os.unlink, fullname, sys.exc_info())
        try:
>           os.rmdir(path)
E           PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\.multilspy\\eced8f41c8ca472697ff15b023cbb571\\black-f3b50e466969f9142393ec32a4b2a383ffbe5f23'

C:\software\Python311\Lib\shutil.py:636: PermissionError
---------------------------------------------------------------------------- Captured log call -----------------------------------------------------------------------------
INFO     multilspy:multilspy_logger.py:56 {"time": "2025-03-29 11:26:29", "level": "INFO", "caller_file": "multilspy\\tests\\..\\src\\multilspy\\language_servers\\jedi_language_server\\jedi_server.py", "caller_name": "start_server", "caller_line": 99, "message": "Starting jedi-language-server server process"}
INFO     multilspy:multilspy_logger.py:56 {"time": "2025-03-29 11:26:29", "level": "INFO", "caller_file": "multilspy\\tests\\..\\src\\multilspy\\language_servers\\jedi_language_server\\jedi_server.py", "caller_name": "start_server", "caller_line": 103, "message": "Sending initialize request from LSP client to LSP server and awaiting response"}
========================================================================= short test summary info ==========================================================================
FAILED tests\multilspy\test_multilspy_python.py::test_multilspy_python_black - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\.multilspy\\eced8f41c8ca472697ff15b02...
============================================================================ 1 failed in 3.83s =============================================================================

Activity

  1. changed the title [-]Tests fails to clean up test environent on Windows if test doesn't pass (PermissionError)[/-] [+]Tests fails to clean up test environment on Windows if test doesn't pass (PermissionError)[/+] on Mar 29, 2025
  2. LakshyAAAgrawal commented on Mar 31, 2025

    @LakshyAAAgrawal
    Collaborator

    I think potentially the changes in the PR #80 will fix this once the process cleanup is better. Would you be able to add this as a unittest? Maybe by forcing a testfailure, then catching this exception?

  3. LakshyAAAgrawal commented on Mar 31, 2025

    @LakshyAAAgrawal
    Collaborator

    I also realize we don't have a windows CI runner yet. Would you be able to create a windows github actions for testing?

  4. Andrej730 commented on Apr 19, 2025

    @Andrej730
    ContributorAuthor

    Sorry for the late reply, completely forgot about this.
    Maybe I'll be able to contribute later, if I come across multilspy again, but not now.

    I think this issue (adding a unittest + basic windows cli) and #89 are good candidates for a "good first issue" label if someone would want to chip in.

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