From 3b909086de236b2a4cf07eaf5002c5e6c1191ef8 Mon Sep 17 00:00:00 2001 From: Prajwal2246 <213174436+Prajwal2246@users.noreply.github.com> Date: Thu, 17 Sep 2026 11:30:21 +0530 Subject: [PATCH 1/3] Stop batch software upload after first package failure A failed package fails the whole batch, so skip processing (and downloading) the remaining payloads once one fails. --- .../50153-cancel-batch-download-on-failure | 1 + ee/server/service/software_installers.go | 10 ++++- ee/server/service/software_installers_test.go | 38 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 changes/50153-cancel-batch-download-on-failure diff --git a/changes/50153-cancel-batch-download-on-failure b/changes/50153-cancel-batch-download-on-failure new file mode 100644 index 00000000000..c8500b199b9 --- /dev/null +++ b/changes/50153-cancel-batch-download-on-failure @@ -0,0 +1 @@ +- Fixed GitOps software batch uploads continuing to download remaining packages after one package failed. diff --git a/ee/server/service/software_installers.go b/ee/server/service/software_installers.go index 0c0d66ab3f0..67cb82fb423 100644 --- a/ee/server/service/software_installers.go +++ b/ee/server/service/software_installers.go @@ -3395,7 +3395,7 @@ func (svc *Service) softwareBatchUpload( manualAgentInstall = team.Config.MDM.MacOSSetup.ManualAgentInstall.Value } - var g errgroup.Group + g, gctx := errgroup.WithContext(ctx) g.SetLimit(1) // TODO: consider whether we can increase this limit, see https://github.com/fleetdm/fleet/issues/22704#issuecomment-2397407837 // the reason for this struct with extra installers support is that: @@ -3438,6 +3438,14 @@ func (svc *Service) softwareBatchUpload( i, p := i, p g.Go(func() error { + // A single failure fails the whole batch, so don't process (and download) + // the remaining payloads. With the limit of 1 above, gctx is always + // cancelled before the next goroutine starts; in-flight work isn't + // interrupted if the limit is raised. + if gctx.Err() != nil { + return nil + } + // NOTE: cannot defer tfr.Close() here because the reader needs to be // available after the goroutine completes. Instead, all temp file // readers are collected in toBeClosedTFRs and will have their Close diff --git a/ee/server/service/software_installers_test.go b/ee/server/service/software_installers_test.go index 5e2f0e90255..2dca9e7f95b 100644 --- a/ee/server/service/software_installers_test.go +++ b/ee/server/service/software_installers_test.go @@ -3201,6 +3201,44 @@ func TestSoftwareBatchUploadFMAVersionCache(t *testing.T) { } } +func TestBatchSetSoftwareInstallersStopsAfterFirstFailure(t *testing.T) { + t.Parallel() + + kvs, getKey := inMemoryKeyValueStore() + + ds := new(mock.Store) + ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) { + return &fleet.AppConfig{}, nil + } + var processed atomic.Int32 + ds.GetTeamsWithInstallerByHashFunc = func(ctx context.Context, sha256, url string) (map[uint][]*fleet.ExistingSoftwareInstaller, error) { + processed.Add(1) + return nil, errors.New("boom") + } + + svc := newTestService(t, ds) + svc.keyValueStore = kvs + svc.logger = slog.New(slog.NewTextHandler(io.Discard, nil)) + + ctx := viewer.NewContext(t.Context(), viewer.Viewer{ + User: &fleet.User{GlobalRole: new(fleet.RoleAdmin)}, + }) + + payloads := []*fleet.SoftwareInstallerPayload{ + {URL: "https://example.com/a.pkg"}, + {URL: "https://example.com/b.pkg"}, + {URL: "https://example.com/c.pkg"}, + } + requestUUID, err := svc.BatchSetSoftwareInstallers(ctx, "", payloads, true) + require.NoError(t, err) + + require.Eventually(t, func() bool { + status := getKey(batchSoftwarePrefix + requestUUID) + return status != nil && strings.HasPrefix(*status, batchSetFailedPrefix) + }, 10*time.Second, 50*time.Millisecond, "batch never failed") + require.Equal(t, int32(1), processed.Load()) +} + func TestGetBatchSetSoftwareInstallersResultMissingDeletedKey(t *testing.T) { t.Parallel() From 454c90d6efec864d011503deb0b3d3ca8b332aad Mon Sep 17 00:00:00 2001 From: Prajwal2246 <213174436+Prajwal2246@users.noreply.github.com> Date: Mon, 5 Oct 2026 12:40:11 +0530 Subject: [PATCH 2/3] test: cover batch installer download failure --- ee/server/service/software_installers_test.go | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ee/server/service/software_installers_test.go b/ee/server/service/software_installers_test.go index 504a2e56c95..a65dfa61ceb 100644 --- a/ee/server/service/software_installers_test.go +++ b/ee/server/service/software_installers_test.go @@ -3202,18 +3202,30 @@ func TestSoftwareBatchUploadFMAVersionCache(t *testing.T) { } func TestBatchSetSoftwareInstallersStopsAfterFirstFailure(t *testing.T) { - t.Parallel() + dev_mode.SetOverride("FLEET_DEV_BATCH_RETRY_INTERVAL", "1ms", t) kvs, getKey := inMemoryKeyValueStore() + var downloads [3]atomic.Int32 + installerSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/a.pkg": + downloads[0].Add(1) + case "/b.pkg": + downloads[1].Add(1) + case "/c.pkg": + downloads[2].Add(1) + } + http.Error(w, "download failed", http.StatusInternalServerError) + })) + t.Cleanup(installerSrv.Close) + ds := new(mock.Store) ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) { return &fleet.AppConfig{}, nil } - var processed atomic.Int32 ds.GetTeamsWithInstallerByHashFunc = func(ctx context.Context, sha256, url string) (map[uint][]*fleet.ExistingSoftwareInstaller, error) { - processed.Add(1) - return nil, errors.New("boom") + return nil, nil } svc := newTestService(t, ds) @@ -3225,9 +3237,9 @@ func TestBatchSetSoftwareInstallersStopsAfterFirstFailure(t *testing.T) { }) payloads := []*fleet.SoftwareInstallerPayload{ - {URL: "https://example.com/a.pkg"}, - {URL: "https://example.com/b.pkg"}, - {URL: "https://example.com/c.pkg"}, + {URL: installerSrv.URL + "/a.pkg", AlwaysDownload: true}, + {URL: installerSrv.URL + "/b.pkg", AlwaysDownload: true}, + {URL: installerSrv.URL + "/c.pkg", AlwaysDownload: true}, } requestUUID, err := svc.BatchSetSoftwareInstallers(ctx, "", payloads, true) require.NoError(t, err) @@ -3236,7 +3248,9 @@ func TestBatchSetSoftwareInstallersStopsAfterFirstFailure(t *testing.T) { status := getKey(batchSoftwarePrefix + requestUUID) return status != nil && strings.HasPrefix(*status, batchSetFailedPrefix) }, 10*time.Second, 50*time.Millisecond, "batch never failed") - require.Equal(t, int32(1), processed.Load()) + require.Positive(t, downloads[0].Load()) + require.Zero(t, downloads[1].Load()) + require.Zero(t, downloads[2].Load()) } func TestGetBatchSetSoftwareInstallersResultMissingDeletedKey(t *testing.T) { From 52f74a7c2ec40761f55d19a09d4f949d5be573ef Mon Sep 17 00:00:00 2001 From: Prajwal2246 <213174436+Prajwal2246@users.noreply.github.com> Date: Thu, 8 Oct 2026 22:10:11 +0530 Subject: [PATCH 3/3] Return cancellation error for skipped batch uploads --- ee/server/service/software_installers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ee/server/service/software_installers.go b/ee/server/service/software_installers.go index d000e070900..be547d3084c 100644 --- a/ee/server/service/software_installers.go +++ b/ee/server/service/software_installers.go @@ -3490,8 +3490,8 @@ func (svc *Service) softwareBatchUpload( // the remaining payloads. With the limit of 1 above, gctx is always // cancelled before the next goroutine starts; in-flight work isn't // interrupted if the limit is raised. - if gctx.Err() != nil { - return nil + if err := gctx.Err(); err != nil { + return err } // NOTE: cannot defer tfr.Close() here because the reader needs to be