Skip to content

RHINENG-27056: fix waitForSessionClosed query#2235

Open
TenSt wants to merge 1 commit into
RedHatInsights:masterfrom
TenSt:stepan/RHINENG-27056-fix-wait-for-sessions-query
Open

RHINENG-27056: fix waitForSessionClosed query#2235
TenSt wants to merge 1 commit into
RedHatInsights:masterfrom
TenSt:stepan/RHINENG-27056-fix-wait-for-sessions-query

Conversation

@TenSt

@TenSt TenSt commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

This PR:

  • Fixes waitForSessionClosed to use lib/pq ANY($1) and fail closed on query errors
  • Replaces GORM-style IN (?) with usename = ANY($1) and pq.Array(lockUsers) for raw database/sql
  • Extracts findActiveAppSession() so session lookup is testable and errors are handled explicitly
  • Retries on query failure instead of treating errors as “no sessions found” (previous fail-open behavior)
  • Adds tests for error path, no sessions, and active manager session detection

Test steps locally:

  • docker compose -f docker-compose.test.yml run --rm test ./scripts/go_test.sh './database_admin' (with DB already migrated)

Summary by Sourcery

Ensure database session checks for application lock users are robust against query errors and correctly detect active sessions.

Bug Fixes:

  • Correct session lookup to use a parameterized ANY() query compatible with database/sql and lib/pq.
  • Treat query failures as errors that are retried instead of incorrectly assuming no active sessions (fail-closed behavior).

Enhancements:

  • Extract session lookup into a dedicated findActiveAppSession helper to improve testability and explicit error handling.

Tests:

  • Add unit and integration-style tests covering query failures, absence of sessions, and detection of an active manager session.

@TenSt TenSt requested a review from a team as a code owner June 18, 2026 12:19
@sourcery-ai

sourcery-ai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Reviewer's Guide

Refactors the session-wait logic to use a safe, testable query helper that relies on lib/pq ANY($1), changes error handling in waitForSessionClosed to fail closed with retries on query errors, and adds tests covering error, empty, and active-session cases.

File-Level Changes

Change Details Files
Refactor session lookup to a dedicated helper using ANY($1) and explicit error semantics.
  • Introduce activeAppSessionsQuery constant using usename = ANY($1) over pg_stat_activity.
  • Add findActiveAppSession(db) that queries active sessions via pq.Array(lockUsers) and returns (session, found, err) with sql.ErrNoRows mapped to not found.
  • Replace inline QueryRow in waitForSessionClosed with findActiveAppSession to decouple lookup and make it unit-testable.
database_admin/update.go
Change waitForSessionClosed behavior to fail closed and retry on query errors.
  • On query error in waitForSessionClosed, log via utils.LogError and sleep for one second before retrying instead of treating errors as "no sessions".
  • Treat the absence of active sessions via the found flag rather than relying on an empty string session check.
  • Improve logging when no sessions are found by using strings.Join(lockUsers, ", ").
database_admin/update.go
Add tests to validate findActiveAppSession behavior for error, no-rows, and active-session scenarios.
  • Add openAppDB test helper to open a Postgres connection using CoreCfg settings.
  • Create TestFindActiveAppSessionInvalidDB to ensure connection/query failures surface as errors and do not set found=true.
  • Create TestFindActiveAppSessionNoRows to verify no active sessions returns found=false without error.
  • Create TestFindActiveAppSessionFound to verify an active manager session is detected and the returned session string contains "manager".
database_admin/update_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In waitForSessionClosed, a persistent query failure will now cause an endless loop with a log+sleep every second; consider adding a max retry count, timeout, or contextual cancellation so the function can fail explicitly instead of potentially hanging indefinitely on a broken DB connection.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `waitForSessionClosed`, a persistent query failure will now cause an endless loop with a log+sleep every second; consider adding a max retry count, timeout, or contextual cancellation so the function can fail explicitly instead of potentially hanging indefinitely on a broken DB connection.

## Individual Comments

### Comment 1
<location path="database_admin/update.go" line_range="64-67" />
<code_context>
-				"usename IN (?) LIMIT 30;", lockUsers,
-		).Scan(&session)
+		session, found, err := findActiveAppSession(db)
 		if err != nil {
-			log.Info(err)
+			utils.LogError("err", err.Error(), "failed to check app database sessions")
+			time.Sleep(time.Second)
+			continue
 		}
-		if session == "" {
</code_context>
<issue_to_address>
**issue (bug_risk):** Loop on error without a termination condition may mask persistent failures.

If `findActiveAppSession` keeps failing (e.g., due to permissions on `pg_stat_activity` or a persistent network issue), this loop will sleep and retry forever, causing the migration to hang on non-recoverable errors. Please add a max retry/timeout mechanism or surface the error after a threshold so the failure is explicit instead of an infinite wait.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread database_admin/update.go
Comment on lines 64 to +67
if err != nil {
log.Info(err)
utils.LogError("err", err.Error(), "failed to check app database sessions")
time.Sleep(time.Second)
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Loop on error without a termination condition may mask persistent failures.

If findActiveAppSession keeps failing (e.g., due to permissions on pg_stat_activity or a persistent network issue), this loop will sleep and retry forever, causing the migration to hang on non-recoverable errors. Please add a max retry/timeout mechanism or surface the error after a threshold so the failure is explicit instead of an infinite wait.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.06%. Comparing base (704b877) to head (9cd456b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2235   +/-   ##
=======================================
  Coverage   59.06%   59.06%           
=======================================
  Files         138      138           
  Lines        8848     8848           
=======================================
  Hits         5226     5226           
  Misses       3076     3076           
  Partials      546      546           
Flag Coverage Δ
unittests 59.06% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

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