Skip to content

Commit

Permalink
add try-finally for resources closing (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
nightblure authored Dec 29, 2024
1 parent 5c5fdea commit bd119f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/injection/auto_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
providers.append(provider)
kwargs[param_name] = provider()

result = f(*args, **kwargs)
close_related_function_scope_resources_sync(providers)
try:
result = f(*args, **kwargs)
finally:
close_related_function_scope_resources_sync(providers)
return result

return wrapper
Expand Down Expand Up @@ -113,8 +115,10 @@ async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:

kwargs[param_name] = resolved_provide

result = await f(*args, **kwargs)
await close_related_function_scope_resources_async(providers)
try:
result = await f(*args, **kwargs)
finally:
await close_related_function_scope_resources_async(providers)
return result

return wrapper
Expand Down
12 changes: 8 additions & 4 deletions src/injection/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:

kwargs[param_name] = resolved_provide

result = await f(*args, **kwargs)
await close_related_function_scope_resources_async(providers)
try:
result = await f(*args, **kwargs)
finally:
await close_related_function_scope_resources_async(providers)
return result

return wrapper
Expand All @@ -154,8 +156,10 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
providers.append(provider)
kwargs[param_name] = provider()

result = f(*args, **kwargs)
close_related_function_scope_resources_sync(providers)
try:
result = f(*args, **kwargs)
finally:
close_related_function_scope_resources_sync(providers)
return result

return wrapper
Expand Down

0 comments on commit bd119f9

Please sign in to comment.