Skip to content

fix(cache): guard empty repolist and add bounded cache polling - #1219

Open
neo0007777 wants to merge 1 commit into
chaoss:devfrom
neo0007777:fix/cache-empty-repolist-handling
Open

neo0007777 wants to merge 1 commit into
chaoss:devfrom
neo0007777:fix/cache-empty-repolist-handling

Conversation

@neo0007777

Copy link
Copy Markdown

Summary of Changes

Closes #1218

This PR addresses critical failure modes in the cache layer and visualization workers:

  1. Guard empty and None repolist queries:

    • get_uncached, retrieve_from_cache, and caching_wrapper now check if not repolist and short-circuit immediately with empty results ([] or pd.DataFrame()).
    • Prevents PostgreSQL psycopg2.errors.SyntaxError: syntax error at or near ")" caused by WHERE repo_id IN () when a user clears repository selection or callbacks trigger prior to repo ID population.
    • Prevents TypeError: 'NoneType' object is not iterable when None is passed.
  2. Database connection lifecycle cleanup:

  3. Safe SQL table identifier escaping:

    • Uses psycopg2.sql.Identifier(tablename) in retrieve_from_cache rather than raw Python string .format().
  4. Centralized bounded polling helper:

    • Implements wait_for_cache(func_name, repolist, timeout=60, caller="") with deadline enforcement and exponential backoff (0.5s → 1.0s → 2.0s up to 5.0s) to provide a standardized, bounded polling utility for visualization callbacks.
  5. Unit test suite:

    • Adds unit test coverage in tests/test_cache_facade.py covering all edge cases (empty lists, None, connection closing, identifier escaping, timeouts, and early returns) with all 8 tests passing.

cc @EngCaioFonseca @cdolfi @MoralCode
Would appreciate your review on this PR! It resolves unhandled database syntax crashes and Celery worker thread exhaustion when repository selections change or initial queries load.

- Short-circuit on empty or None repolist in get_uncached, retrieve_from_cache, and caching_wrapper to prevent PostgreSQL SyntaxError ('IN ()') and TypeError.
- Ensure psycopg2 connections are explicitly closed in try...finally blocks across cache_facade to prevent database connection leaks.
- Safely escape table names using psycopg2.sql.Identifier in retrieve_from_cache.
- Introduce wait_for_cache helper with deadline timeout and exponential backoff.
- Add unit test suite in tests/test_cache_facade.py.

Fixes chaoss#1218

Signed-off-by: Shiva Sharma <shivaumj@gmail.com>
@neo0007777

Copy link
Copy Markdown
Author

Hi @EngCaioFonseca, @cdolfi, @MoralCode - could you please review this PR when you have a moment?

Why this is important (in short):

  1. Prevents unhandled PostgreSQL IN () syntax errors and type crashes whenever repo selection is empty/cleared or still initializing.
  2. Fixes unclosed psycopg2 connections across cache_facade.py that contribute to database connection exhaustion (Ensure 8Knot doesnt leak connections when materialized views not present #1170).
  3. Introduces a centralized wait_for_cache helper with monotonic deadline timeouts and progressive backoff to prevent Celery callback workers from hanging in indefinite polling loops.
  4. Adds full unit test coverage in tests/test_cache_facade.py (all 8 tests passing).

Thank you!

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.

[BUG] Unhandled empty repolist causes SQL syntax error IN () in cache_facade and unbounded polling in visualization callbacks

1 participant