CalDAV: close the DAVClient on sync and write-back paths (#4793)

_sync_blocking (src/caldav_sync.py) and _writeback_blocking
(src/caldav_writeback.py) each open their own caldav.DAVClient via
_build_dav_client, but never close it. The client owns an HTTP session
with a pooled connection; without a close() that connection is held until
process exit.

Previously the fix added explicit client.close() calls before each early
return and at the end of the DB finally block. This still leaked the
client when SessionLocal() raised before the DB try/finally was entered.

Now _sync_blocking wraps the entire post-construction path in an outer
try/finally that calls client.close() unconditionally, covering:
  - AuthorizationError / NotFoundError early return
  - URL-fallback failure early return
  - no-calendars early return
  - normal return after sync
  - SessionLocal() construction failure (new regression coverage)

_writeback_blocking already used a try/finally (unchanged).

- src/caldav_sync.py: replace scattered client.close() calls with a
  single outer try/finally block around the discovery + DB sync path
- tests/test_caldav_client_cleanup.py: add CalendarDeletedEvent to the
  database stub; add regression test for SessionLocal() failure path

Closes #4593
This commit is contained in:
tanmayraut45
2026-07-11 17:33:24 +05:30
committed by GitHub
parent 6efc07c5fc
commit e6d9d68729
4 changed files with 347 additions and 193 deletions
@@ -93,6 +93,10 @@ class _FakeClient:
def calendar(self, url=None):
return _FakeCalendar(url)
def close(self):
# Mirror the real DAVClient: sync now closes the client on every path.
self.closed = True
def _install_fake_caldav(monkeypatch):
fake = types.ModuleType("caldav")