Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion gittensor/utils/github_api_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,15 @@ def session_scope() -> Iterator[None]:
finally:
cache = _session_cache
_session_cache = None
first_exc: Optional[Exception] = None
for s in cache.values():
s.close()
try:
s.close()
except Exception as exc:
if first_exc is None:
first_exc = exc
if first_exc is not None:
raise first_exc


def _build_session(token: str) -> requests.Session:
Expand Down
22 changes: 22 additions & 0 deletions tests/utils/test_github_api_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,28 @@ def fake_build_session(token):

assert github_api_tools._session_cache is None

def test_all_sessions_closed_even_when_first_close_raises(self, monkeypatch):
built: list = []

def fake_build_session(token):
session = Mock()
session.headers = {}
built.append(session)
return session

monkeypatch.setattr(github_api_tools, '_build_session', fake_build_session)

with pytest.raises(RuntimeError, match='first boom'):
with _real_session_scope():
_real_get_session('tokenA')
_real_get_session('tokenB')
built[0].close.side_effect = RuntimeError('first boom')

assert len(built) == 2
built[0].close.assert_called_once()
built[1].close.assert_called_once()
assert github_api_tools._session_cache is None


if __name__ == '__main__':
pytest.main([__file__, '-v'])
Loading