fix(scanner): stop trivy scans from updating the database

Each trivy scan updated the vulnerability database when the database was
stale. Six concurrent rescans then downloaded it at the same time. A
download took about 11 seconds alone and 38 seconds under contention.
Five of six trivy processes crashed with SIGBUS, because they read a
database that another process replaced. The other scans hit their timeout.

Scans now run with --skip-db-update. A refresh loop updates the database
every 6 hours (trivy.db_update_interval). Scans hold a read lock and an
update holds the write lock, so an update never replaces the database
under a running scan. A scan that finds no database downloads it once
before it scans.

The grype refresh loop moves to the dbrefresh package, which both scanners
use.
This commit is contained in:
2026-12-01 03:20:49 +00:00
parent e4077066ef
commit f367ce618e
8 changed files with 250 additions and 44 deletions
+45
View File
@@ -0,0 +1,45 @@
// Package dbrefresh runs a scanner's vulnerability database update on a fixed interval.
package dbrefresh
import (
"context"
"time"
"github.com/rs/zerolog/log"
)
// DefaultInterval applies when the configured interval is 0.
const DefaultInterval = 6 * time.Hour
// Run calls update every interval until stop closes. immediate runs the first update at once;
// otherwise it waits one interval.
func Run(stop <-chan struct{}, interval time.Duration, immediate bool, scanner string, update func(context.Context) error) {
if interval <= 0 {
interval = DefaultInterval
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
<-stop
cancel()
}()
once := func() {
if err := update(ctx); err != nil {
log.Error().Err(err).Str("scanner", scanner).Msg("Scheduled database update failed")
}
}
if immediate {
once()
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
once()
case <-ctx.Done():
return
}
}
}