Skip to content

crt/crt_db error handling - #3124

Merged
liquidsec merged 6 commits into
devfrom
crt-resilience
Jun 16, 2026
Merged

crt/crt_db error handling#3124
liquidsec merged 6 commits into
devfrom
crt-resilience

Conversation

@liquidsec

Copy link
Copy Markdown
Collaborator

Supersedes #3077 — bundles the fix @TrebledJ proposed there with an adjacent fix in crt_db that came out of the same audit.

crt: don't short-circuit 404 as "no data"

The default _api_response_is_success in BaseModule treats HTTP 404 as success because most APIs use 404 to mean "no records for this query." crt.sh's API returns 404 (and 503) transiently when overloaded — it's a real failure, not an empty-set signal. Today those responses pass through _api_response_is_success, skip the api_request retry loop, hit parse_results, and produce JSONDecodeError noise.

This overrides the success check in the crt module to drop the 404 special case:

def _api_response_is_success(self, r):
    return getattr(r, "is_success", False)

So 404 → counts as a failure → api_request retries with backoff → if still failing after api_failure_abort_threshold, the module enters error state (same path 503 already takes).

Credit to @TrebledJ for spotting this in #3077; this PR uses the corrected logic from the review there (#3077's diff inverted the membership check and unintentionally promoted 400/500/502/etc. to success — sending the abort threshold the wrong way).

crt_db: reconnect on dropped connections, bail on Postgres OOM

A real bbot scan log shows two flavors of repeating failure from crt_db:

Count Issue
asyncpg.InterfaceError: connection is closed 4483 The module opens one asyncpg connection on first request and reuses it for the rest of the scan. When the upstream (crt.sh's Postgres) drops the connection — restart, idle timeout, OOM — every subsequent db_conn.fetch() raises and the module is silently broken for the rest of the scan with no recovery path.
asyncpg.OutOfMemoryError 343 crt.sh's Postgres reports "out of shared memory." The module keeps hammering instead of backing off.

The fix:

  • Extract _connect() so the same parameters can be reused.
  • Before each query, check db_conn is None or db_conn.is_closed() and reopen.
  • Wrap fetch() so InterfaceError / ConnectionError clears self.db_conn (next call reconnects) and re-raises.
  • On OutOfMemoryError, call set_error_state(...) so the module backs off cleanly for the rest of the scan.

Added a regression test TestCRT_DB_Reconnect that drops the first connection mid-scan and asserts the module reconnects and continues producing results.

liquidsec added 2 commits May 22, 2026 09:06
- crt: override _api_response_is_success so 404 falls back to api_request's
  retry path instead of being treated as "no data" (crt.sh returns 404/503
  transiently when overloaded). Co-authored with @TrebledJ's #3077.
- crt_db: reconnect when the asyncpg connection is dropped mid-scan, and
  bail out via set_error_state on Postgres OutOfMemoryError instead of
  hammering the upstream.
@github-actions

github-actions Bot commented May 22, 2026

Copy link
Copy Markdown
Contributor

🚀 Performance Benchmark Report

⚠️ No current benchmark data available

This might be because:

  • Benchmarks failed to run
  • No benchmark tests found
  • Dependencies missing

@codecov

codecov Bot commented May 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.43750% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90%. Comparing base (e418368) to head (3f0df4d).
⚠️ Report is 167 commits behind head on dev.

Files with missing lines Patch % Lines
...est/test_step_2/module_tests/test_module_crt_db.py 98% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             dev   #3124     +/-   ##
=======================================
- Coverage     90%     90%     -0%     
=======================================
  Files        447     449      +2     
  Lines      40476   44195   +3719     
=======================================
+ Hits       36336   39521   +3185     
- Misses      4140    4674    +534     

☔ 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.

@liquidsec liquidsec changed the title crt/crt_db: stop treating transient failures as terminal crt/crt_db error handling May 22, 2026
@liquidsec liquidsec self-assigned this May 25, 2026
@liquidsec
liquidsec requested a review from ausmaster May 25, 2026 15:16
@liquidsec liquidsec mentioned this pull request Jun 15, 2026
28 tasks
@liquidsec
liquidsec merged commit c07e3e7 into dev Jun 16, 2026
17 of 18 checks passed
@liquidsec
liquidsec deleted the crt-resilience branch June 16, 2026 00:21
@ausmaster ausmaster added this to the BBOT 3.0 - blazed_elijah milestone Jun 26, 2026
@liquidsec liquidsec mentioned this pull request Jul 7, 2026
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