Skip to content

Resource finalizer never called in VS Code debugger #158

@chrisk314

Description

@chrisk314

Hi, I'm seeing some strange behaviour when attempting to use Resource type providers during debug sessions in VS Code. From adding both print statements and breakpoints into the code, it's clear that the code in the finally block is never reached when running in debug mode in VS Code. Running the code normally, i.e., as a python script in a terminal, I can see the print outputs as expected.

There is a minimal reproducible example below. In this case, it's not an issue; however the actual code where I noticed this problem launches a child process which must be terminated in the finally section (it runs an infinte loop - it's a message proxy). Failure to evaluate this finally block in VS Code debug mode causes test cases to run indefinitely. I can add similar code into a pytest conftest fixture and it works fine. It's worth mentioning that running the script inside VS Code but not in debug mode also works fine.

I'm running...
python: 3.12.8
that-depends: 1.27.0
vs code: 197.0
os: Windows 11 with WSL2 Ubuntu 24.04

# that_depends_example.py

import typing as _t

from that_depends import BaseContainer, Provide, inject
from that_depends.providers import Resource


def say_hello_goodbye() -> _t.Iterator[None]:
    try:
        print(f"Hello!")
        yield None
    finally:
        print(f"Goodbye!")


class DI(BaseContainer):
    """`DI` is a dependency injection container for plugboard."""

    say_hello_goodbye: Resource[None] = Resource(say_hello_goodbye)


@inject
def require_resource(resource: None = Provide[DI.say_hello_goodbye]) -> None:
    print("With resource...")


if __name__ == "__main__":
    require_resource()

VS Code launch config for convenience.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "that_depends_debug_bug",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/that_depends_example.py",
            "console": "integratedTerminal",
            "justMyCode": false,
        },
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions