Skip to content

fix: drop stale DB connections around LaunchDarkly fetch phase#7219

Merged
gagantrivedi merged 1 commit intomainfrom
claude/investigate-flagsmith-7167-kfSfA
Apr 13, 2026
Merged

fix: drop stale DB connections around LaunchDarkly fetch phase#7219
gagantrivedi merged 1 commit intomainfrom
claude/investigate-flagsmith-7167-kfSfA

Conversation

@gagantrivedi
Copy link
Copy Markdown
Member

@gagantrivedi gagantrivedi commented Apr 13, 2026

Changes

Closes #7167

The LaunchDarkly import task holds a Django DB connection idle during a slow HTTP fetch of LD data (can exceed 5 minutes on large projects). RDS terminates the session on idle_session_timeout, and the subsequent write phase fails with InterfaceError: connection already closed (Sentry: FLAGSMITH-API-5MD).

  • Adds util.db.closing_stale_connections() — a context manager that calls django.db.close_old_connections() when the wrapped block exits. Healthy connections are untouched; only obsolete/unusable ones are dropped.
  • Applies it around the LD fetch block in process_import_request, so the write phase opens a fresh connection if the prior one was killed during the fetch.

How did you test this code?

  • New unit test test_closing_stale_connections__exit__calls_close_old_connections covers the helper.
  • Existing test_process_import_request__* tests were switched to @pytest.mark.django_db(transaction=True) because pytest-django's default atomic() wrapping collides with close_old_connections' autocommit check. All 17 LD service tests pass locally.

Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Code review skipped — your organization's overage spend limit has been reached.

Code review is billed via overage credits. To resume reviews, an organization admin can raise the monthly limit at claude.ai/admin-settings/claude-code.

Once credits are available, reopen this pull request to trigger a review.

@gagantrivedi gagantrivedi requested a review from a team as a code owner April 13, 2026 02:49
@gagantrivedi gagantrivedi requested review from khvn26 and removed request for a team April 13, 2026 02:49
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs Ignored Ignored Preview Apr 13, 2026 5:52am
flagsmith-frontend-preview Ignored Ignored Preview Apr 13, 2026 5:52am
flagsmith-frontend-staging Ignored Ignored Preview Apr 13, 2026 5:52am

Request Review

@gagantrivedi gagantrivedi marked this pull request as draft April 13, 2026 02:50
@github-actions github-actions bot added the api Issue related to the REST API label Apr 13, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-e2e:pr-7219 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api-test:pr-7219 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-7219 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api:pr-7219 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-7219 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-7219 Finished ✅ Results

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  11 passed

Details

stats  11 tests across 8 suites
duration  24.1 seconds
commit  4b08f9d
info  🔄 Run: #15990 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  11 passed

Details

stats  11 tests across 8 suites
duration  56.5 seconds
commit  4b08f9d
info  🔄 Run: #15990 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  17 passed

Details

stats  17 tests across 14 suites
duration  1 minute, 1 second
commit  4b08f9d
info  🔄 Run: #15990 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  52.1 seconds
commit  4b08f9d
info  🔄 Run: #15990 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  11 passed

Details

stats  11 tests across 8 suites
duration  9.3 seconds
commit  a2cd61d
info  🔄 Run: #15992 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  11 passed

Details

stats  11 tests across 8 suites
duration  28.7 seconds
commit  a2cd61d
info  🔄 Run: #15992 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  17 passed

Details

stats  17 tests across 14 suites
duration  1 minute, 3 seconds
commit  a2cd61d
info  🔄 Run: #15992 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  46 seconds
commit  a2cd61d
info  🔄 Run: #15992 (attempt 1)

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.35%. Comparing base (de527ae) to head (a2cd61d).
⚠️ Report is 13 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7219   +/-   ##
=======================================
  Coverage   98.35%   98.35%           
=======================================
  Files        1348     1350    +2     
  Lines       50666    50713   +47     
=======================================
+ Hits        49834    49881   +47     
  Misses        832      832           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

Visual Regression

16 screenshots compared. See report for details.
View full report

The LaunchDarkly import task holds a Django DB connection idle during
a slow HTTP fetch of LD data (can exceed 5 min). RDS terminates the
session on idle_session_timeout, and the following write phase fails
with 'InterfaceError: connection already closed'.

Adds a reusable context manager (closing_stale_connections) that calls
django.db.close_old_connections when the wrapped block exits, and
applies it around the LD fetch block so the write phase opens a fresh
connection if the prior one was killed.

pytest-django wraps tests in atomic(), which collides with
close_old_connections' autocommit check; the affected
test_process_import_request tests are switched to transaction=True.
@gagantrivedi gagantrivedi force-pushed the claude/investigate-flagsmith-7167-kfSfA branch from 4b08f9d to a2cd61d Compare April 13, 2026 05:52
@gagantrivedi gagantrivedi changed the title Handle stale DB connections in Launch Darkly import service fix: drop stale DB connections around LaunchDarkly fetch phase Apr 13, 2026
@gagantrivedi gagantrivedi marked this pull request as ready for review April 13, 2026 06:00
@github-actions github-actions bot added the fix label Apr 13, 2026
Copy link
Copy Markdown
Member

@khvn26 khvn26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@gagantrivedi gagantrivedi merged commit 7f219c0 into main Apr 13, 2026
54 checks passed
@gagantrivedi gagantrivedi deleted the claude/investigate-flagsmith-7167-kfSfA branch April 13, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import from Launch Darkly fails with 'InterfaceError: connection already closed'

2 participants