fix: prevent FetcherSession state corruption and lazy session close crash
This commit is contained in:
@@ -716,8 +716,13 @@ class FetcherSession:
|
|||||||
config["selector_config"] = self.selector_config
|
config["selector_config"] = self.selector_config
|
||||||
config["proxy_rotator"] = self._proxy_rotator
|
config["proxy_rotator"] = self._proxy_rotator
|
||||||
self._client = _SyncSessionLogic(**config)
|
self._client = _SyncSessionLogic(**config)
|
||||||
|
try:
|
||||||
|
result = self._client.__enter__()
|
||||||
|
except Exception:
|
||||||
|
self._client = None
|
||||||
|
raise
|
||||||
self._is_alive = True
|
self._is_alive = True
|
||||||
return self._client.__enter__()
|
return result
|
||||||
raise RuntimeError("This FetcherSession instance already has an active synchronous session.")
|
raise RuntimeError("This FetcherSession instance already has an active synchronous session.")
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
@@ -737,8 +742,13 @@ class FetcherSession:
|
|||||||
config["selector_config"] = self.selector_config
|
config["selector_config"] = self.selector_config
|
||||||
config["proxy_rotator"] = self._proxy_rotator
|
config["proxy_rotator"] = self._proxy_rotator
|
||||||
self._client = _ASyncSessionLogic(**config)
|
self._client = _ASyncSessionLogic(**config)
|
||||||
|
try:
|
||||||
|
result = await self._client.__aenter__()
|
||||||
|
except Exception:
|
||||||
|
self._client = None
|
||||||
|
raise
|
||||||
self._is_alive = True
|
self._is_alive = True
|
||||||
return await self._client.__aenter__()
|
return result
|
||||||
raise RuntimeError("This FetcherSession instance already has an active asynchronous session.")
|
raise RuntimeError("This FetcherSession instance already has an active asynchronous session.")
|
||||||
|
|
||||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
|||||||
@@ -93,7 +93,9 @@ class SessionManager:
|
|||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
"""Close all registered sessions."""
|
"""Close all registered sessions."""
|
||||||
for session in self._sessions.values():
|
for sid, session in self._sessions.items():
|
||||||
|
if sid in self._lazy_sessions and not session._is_alive:
|
||||||
|
continue
|
||||||
_ = await session.__aexit__(None, None, None)
|
_ = await session.__aexit__(None, None, None)
|
||||||
|
|
||||||
self._started = False
|
self._started = False
|
||||||
|
|||||||
Reference in New Issue
Block a user