Skip to content

Use Django connections in the geomap data view#4113

Open
adobloug wants to merge 1 commit into
Uninett:masterfrom
adobloug:geomap-django-connection-pooling
Open

Use Django connections in the geomap data view#4113
adobloug wants to merge 1 commit into
Uninett:masterfrom
adobloug:geomap-django-connection-pooling

Conversation

@adobloug

@adobloug adobloug commented Jul 1, 2026

Copy link
Copy Markdown

Scope and purpose

Follow-up to #4111 (the #4104 connection-leak fix). That PR hardened the
legacy nav.db ObjectCache so it survives concurrent access. This PR
removes the last multithreaded web caller of that cache, which was the
actual path that triggered the race under mod_wsgi.

The geomap data view opened its cursor via nav.db.getConnection(),
sharing the process-wide ObjectCache across request threads. It now uses
Django's django.db.connection, which is thread-local and pooled via
CONN_MAX_AGE — the same pattern already used by the report system and 10+
other NAV modules.

Because the geomap queries need a psycopg2 DictCursor, the view reaches
through to the underlying psycopg2 connection (connection.connection) and
closes only the cursor; Django owns the connection's lifecycle. The explicit
set_isolation_level(1) call is dropped — Django runs in autocommit and
READ COMMITTED is PostgreSQL's default.

The remaining nav.db callers are non-web scripts and daemons (snmptrapd,
smsd, logengine, cron bins) that run outside Django's request lifecycle, so
the legacy cache stays in place for them.

This pull request

  • migrates the geomap data view to Django's connection pooling
  • removes the last multithreaded web dependency on the legacy ObjectCache

Contributor Checklist

  • Added a changelog fragment for towncrier
  • Added/amended tests for new/changed code
  • Added/changed documentation — not applicable; internal implementation change
  • Linted/formatted the code with ruff
  • Wrote the commit message per the "If applied, this commit will ..." rule (<= 50 char subject)
  • Based this pull request on the correct upstream branch (master)
  • If it's not obvious from a linked issue, described how to observe the effect (geomap integration test test_geomap_data_should_not_crash exercises the view end-to-end)
  • If this results in changes in the UI: screenshots — not applicable; no UI changes
  • If this adds a new Python source code file: boilerplate header — added to the new test file

🤖 Generated with Claude Code

The geomap data view was the last multithreaded web caller of the
legacy nav.db connection cache (ObjectCache). Under mod_wsgi that cache
is shared across request threads and was the path that triggered the
connection-leak race in Uninett#4104.

Switch the view to Django's connection (django.db.connection), which is
thread-local and pooled via CONN_MAX_AGE, so no shared cache is
involved. The geomap queries still need a psycopg2 DictCursor, so we
reach through to the underlying psycopg2 connection and close only the
cursor -- Django owns the connection's lifecycle. The explicit
set_isolation_level(1) call is dropped; Django runs in autocommit and
READ COMMITTED is PostgreSQL's default.

The remaining nav.db callers are non-web scripts and daemons that run
outside Django's request lifecycle, so the legacy cache stays for them.

Added a unit test asserting the view uses the Django-managed connection
and never closes it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sonarqubecloud

sonarqubecloud Bot commented Jul 1, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants