From 1f2ea1b83a32d67612156fb92f371ddddcd00c2b Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 16 Nov 2026 20:43:37 +0000 Subject: [PATCH] fix: honor zero and empty values for fetch backoff and idle cleanup (#1418) `fetch_interval_max: 0` is documented to disable polling backoff but became 5s. Zeros for `workdir_cleanup_age` and `idle_cleanup_interval` were ignored when they came from a YAML merge key or alias, so cleanup still ran, while an empty or null value turned cleanup off. These defaults are now seeded before the file is read, as `cache` already does, so a written 0 means off and an empty value keeps the default. _Written by Claude (Opus 5.5)._ Reviewed-on: https://gitea.com/gitea/runner/pulls/1418 Reviewed-by: bircni Co-authored-by: silverwind --- internal/pkg/config/config.go | 45 ++++++------------------------ internal/pkg/config/config_test.go | 8 +++++- 2 files changed, 16 insertions(+), 37 deletions(-) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 79121801..f8e2119f 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -271,8 +271,11 @@ type Config struct { // If file is not empty, it will be used to load the configuration. func LoadDefault(file string) (*Config, error) { // Seeded before the file is read, so a written 0 can mean off. - cfg := &Config{Cache: DefaultCache(), Log: Log{Job: LogJob{Retention: 7 * 24 * time.Hour, MaxSize: 1024 * 1024 * 1024}}} - var definedRunnerKeys map[string]bool + cfg := &Config{ + Runner: Runner{FetchIntervalMax: 5 * time.Second, WorkdirCleanupAge: 24 * time.Hour, IdleCleanupInterval: 10 * time.Minute}, + Cache: DefaultCache(), + Log: Log{Job: LogJob{Retention: 7 * 24 * time.Hour, MaxSize: 1024 * 1024 * 1024}}, + } if file != "" { content, err := os.ReadFile(file) if err != nil { @@ -289,7 +292,6 @@ func LoadDefault(file string) (*Config, error) { return nil, fmt.Errorf("parse config file %q: %w", file, err) } warnUnknownKeys(file, &document) - definedRunnerKeys = definedRunnerConfigKeys(&document) } if cfg.Runner.EnvFile != "" { @@ -410,15 +412,6 @@ func LoadDefault(file string) (*Config, error) { if cfg.Runner.FetchInterval <= 0 { cfg.Runner.FetchInterval = 2 * time.Second } - if cfg.Runner.FetchIntervalMax <= 0 { - cfg.Runner.FetchIntervalMax = 5 * time.Second - } - if cfg.Runner.WorkdirCleanupAge == 0 && !definedRunnerKeys["workdir_cleanup_age"] { - cfg.Runner.WorkdirCleanupAge = 24 * time.Hour - } - if cfg.Runner.IdleCleanupInterval == 0 && !definedRunnerKeys["idle_cleanup_interval"] { - cfg.Runner.IdleCleanupInterval = 10 * time.Minute - } if cfg.Runner.LogReportInterval <= 0 { cfg.Runner.LogReportInterval = 5 * time.Second } @@ -459,8 +452,10 @@ func LoadDefault(file string) (*Config, error) { cfg.Runner.FetchTimeout = RequestTimeout } if cfg.Runner.FetchIntervalMax < cfg.Runner.FetchInterval { - logging.Warnf("fetch_interval_max (%v) is less than fetch_interval (%v), setting fetch_interval_max to fetch_interval", - cfg.Runner.FetchIntervalMax, cfg.Runner.FetchInterval) + if cfg.Runner.FetchIntervalMax != 0 { + logging.Warnf("fetch_interval_max (%v) is less than fetch_interval (%v), setting fetch_interval_max to fetch_interval", + cfg.Runner.FetchIntervalMax, cfg.Runner.FetchInterval) + } cfg.Runner.FetchIntervalMax = cfg.Runner.FetchInterval } if cfg.Runner.LogReportMaxLatency >= cfg.Runner.LogReportInterval { @@ -550,28 +545,6 @@ func warnUnknownKeys(file string, document *yaml.Node) { } } -func definedRunnerConfigKeys(root *yaml.Node) map[string]bool { - defined := map[string]bool{} - if len(root.Content) == 0 { - return defined - } - - doc := root.Content[0] - for i := 0; i+1 < len(doc.Content); i += 2 { - key := doc.Content[i] - value := doc.Content[i+1] - if key.Value != "runner" || value.Kind != yaml.MappingNode { - continue - } - for j := 0; j+1 < len(value.Content); j += 2 { - defined[value.Content[j].Value] = true - } - break - } - - return defined -} - func validateExtraHeaders(headers map[string]string) error { seen := make(map[string]string, len(headers)) for name, value := range headers { diff --git a/internal/pkg/config/config_test.go b/internal/pkg/config/config_test.go index a0777076..8a03b07f 100644 --- a/internal/pkg/config/config_test.go +++ b/internal/pkg/config/config_test.go @@ -255,6 +255,7 @@ metrics: } func TestLoadDefault_WorkdirCleanup(t *testing.T) { + t.Setenv("EMPTY", "") for _, tc := range []struct { name, yaml string age, interval time.Duration @@ -263,6 +264,8 @@ func TestLoadDefault_WorkdirCleanup(t *testing.T) { {"configured interval", "\nrunner:\n idle_cleanup_interval: 45m\n", 24 * time.Hour, 45 * time.Minute}, {"disabled", "\nrunner:\n workdir_cleanup_age: 0s\n idle_cleanup_interval: 0s\n", 0, 0}, {"negative", "\nrunner:\n workdir_cleanup_age: -1s\n idle_cleanup_interval: -1s\n", -time.Second, -time.Second}, + {"disabled through a merge key", "\nx-base: &base\n workdir_cleanup_age: 0s\n idle_cleanup_interval: 0s\nrunner:\n <<: *base\n", 0, 0}, + {"empty keeps the default", "\nrunner:\n workdir_cleanup_age: ${EMPTY}\n idle_cleanup_interval:\n", 24 * time.Hour, 10 * time.Minute}, } { t.Run(tc.name, func(t *testing.T) { cfg, err := LoadDefault(write(t, tc.yaml)) @@ -441,11 +444,14 @@ func TestLoadDefault_ShippedConfigsChangeNothing(t *testing.T) { assert.Empty(t, hook.AllRecords()) } -func TestLoadDefault_ClampsFetchTimeout(t *testing.T) { +func TestLoadDefault_ClampsFetchTimeoutAndZeroFetchIntervalMaxDisablesBackoff(t *testing.T) { cfg, err := LoadDefault(write(t, ` runner: fetch_timeout: 120s + fetch_interval: 1s + fetch_interval_max: 0s `)) require.NoError(t, err) assert.Equal(t, RequestTimeout, cfg.Runner.FetchTimeout) + assert.Equal(t, time.Second, cfg.Runner.FetchIntervalMax) }