Skip to content
Latchkey

How to Retry Only on Infrastructure Errors in CI

Retrying every failure hides real bugs, so scope retries to transient error types and let assertion failures fail immediately.

Most runners can limit retries to matching errors: pytest-rerunfailures --only-rerun, rspec-retry exceptions_to_retry, or a custom predicate. Match connection resets and timeouts, never AssertionError.

Steps

  • List the transient error types worth retrying (timeouts, resets).
  • Configure the runner to retry only those.
  • Ensure assertion failures are excluded so bugs surface fast.

Match transient errors only

Terminal
# pytest: retry only connection-style errors
pytest --reruns 2 --only-rerun 'ConnectionError|ReadTimeout'

RSpec exception allowlist

spec/spec_helper.rb
RSpec.configure do |c|
  c.default_retry_count = 2
  c.exceptions_to_retry = [Net::ReadTimeout, Errno::ECONNRESET]
end

Gotchas

  • Never retry AssertionError; a flaky assertion is a bug the retry will bury.
  • Keep the transient list short and specific so it does not silently grow to cover real failures.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →