Fix async context manager usage in BackgroundLoaderService
- Changed 'async for' to 'async with' for get_db_session() - get_db_session() is @asynccontextmanager, requires async with not async for - Created 5 comprehensive unit tests verifying the fix - All tests pass, background loading now works correctly
This commit is contained in:
@@ -121,35 +121,46 @@ For each task completed:
|
||||
|
||||
✅ **Task Completed Successfully**
|
||||
|
||||
### Issue Fixed: RuntimeError: generator didn't stop after athrow()
|
||||
### Issue Fixed: TypeError: 'async for' requires an object with __aiter__ method
|
||||
|
||||
**Problem:**
|
||||
The `/api/anime/add` endpoint was throwing a 500 error with `RuntimeError: generator didn't stop after athrow()` when validation errors (HTTPException) were raised after database session dependencies yielded.
|
||||
The BackgroundLoaderService was crashing when trying to load series data with the error:
|
||||
```
|
||||
TypeError: 'async for' requires an object with __aiter__ method, got _AsyncGeneratorContextManager
|
||||
```
|
||||
|
||||
This error occurred in `_load_series_data` method at line 282 of [background_loader_service.py](src/server/services/background_loader_service.py).
|
||||
|
||||
**Root Cause:**
|
||||
Async generator dependencies (`get_database_session` and `get_optional_database_session`) didn't properly handle exceptions thrown back into them after yielding. When an `HTTPException` was raised in the endpoint for validation errors, Python's async context manager tried to propagate the exception to the generator, but without proper exception handling around the yield statement, it resulted in the "generator didn't stop after athrow()" error.
|
||||
The code was incorrectly using `async for db in get_db_session():` to get a database session. However, `get_db_session()` is decorated with `@asynccontextmanager`, which returns an async context manager (not an async iterator). Async context managers must be used with `async with`, not `async for`.
|
||||
|
||||
**Solution:**
|
||||
Added proper exception handling in both database session dependencies by wrapping the `yield` statement with a try-except block that re-raises any exceptions, allowing FastAPI to handle them correctly.
|
||||
Changed the database session acquisition from:
|
||||
```python
|
||||
async for db in get_db_session():
|
||||
# ... code ...
|
||||
break # Exit loop after first iteration
|
||||
```
|
||||
|
||||
To the correct pattern:
|
||||
```python
|
||||
async with get_db_session() as db:
|
||||
# ... code ...
|
||||
```
|
||||
|
||||
**Files Modified:**
|
||||
1. [src/server/utils/dependencies.py](src/server/utils/dependencies.py) - Fixed exception handling in `get_database_session` and `get_optional_database_session`
|
||||
2. [tests/unit/test_dependency_exception_handling.py](tests/unit/test_dependency_exception_handling.py) - Created comprehensive unit tests for the fix
|
||||
3. [tests/api/test_anime_endpoints.py](tests/api/test_anime_endpoints.py) - Updated to mock BackgroundLoaderService and expect 202 status codes
|
||||
1. [src/server/services/background_loader_service.py](src/server/services/background_loader_service.py) - Fixed async context manager usage
|
||||
2. [tests/unit/test_background_loader_session.py](tests/unit/test_background_loader_session.py) - Created comprehensive unit tests
|
||||
|
||||
**Tests:**
|
||||
- ✅ 5 new unit tests for dependency exception handling (all passing)
|
||||
- ✅ 16 anime endpoint integration tests (all passing)
|
||||
- ✅ Tests verify proper handling of 400, 404, 422 status codes
|
||||
- ✅ Tests verify successful requests still work correctly
|
||||
- ✅ 5 new unit tests for background loader database session handling (all passing)
|
||||
- ✅ Tests verify proper async context manager usage
|
||||
- ✅ Tests verify error handling and progress tracking
|
||||
- ✅ Tests verify episode and NFO loading logic
|
||||
- ✅ Includes test demonstrating the difference between `async with` and `async for`
|
||||
|
||||
**Note for Users:**
|
||||
If you're experiencing this error on a running server, please **restart the server** to load the fixed code:
|
||||
```bash
|
||||
# Stop the server (Ctrl+C)
|
||||
# Then restart:
|
||||
conda run -n AniWorld python -m uvicorn src.server.fastapi_app:app --host 127.0.0.1 --port 8000 --reload
|
||||
```
|
||||
**Verification:**
|
||||
The fix allows the background loader service to properly load series data including episodes, NFO files, logos, and images without crashing.
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user