diff --git a/src/dependency_injector/providers.pyx b/src/dependency_injector/providers.pyx index 84c1fad7..39716ea0 100644 --- a/src/dependency_injector/providers.pyx +++ b/src/dependency_injector/providers.pyx @@ -3222,8 +3222,8 @@ cdef class ThreadLocalSingleton(BaseSingleton): return future_result self._storage.instance = instance - finally: - return instance + + return instance def _async_init_instance(self, future_result, result): try: diff --git a/tests/unit/providers/singleton/test_thread_local_singleton_py3.py b/tests/unit/providers/singleton/test_thread_local_singleton_py3.py new file mode 100644 index 00000000..fb0a3638 --- /dev/null +++ b/tests/unit/providers/singleton/test_thread_local_singleton_py3.py @@ -0,0 +1,20 @@ +import pytest + +from dependency_injector.containers import Container +from dependency_injector.providers import ThreadLocalSingleton + + +class FailingClass: + def __init__(self): + raise ValueError("FAILING CLASS") + + +class TestContainer(Container): + failing_class = ThreadLocalSingleton(FailingClass) + + +def test_on_failure_value_error_is_raised(): + container = TestContainer() + + with pytest.raises(ValueError, match="FAILING CLASS"): + container.failing_class()