#23992·harbor

Unscheduling one periodic job also removes other schedules that share its numeric ID

Author: Vad1moCreated Sep 23, 2026Updated Sep 28, 2026

When jobservice removes one periodic schedule, it also removes every other schedule that was registered with the same numeric ID. The removed schedules stay in the schedule table and the UI keeps showing them, but they never fire again. Tag retention, replication, GC and other scheduled policies stop running and nothing reports an error.

Expected behavior and actual behavior:

Expected: removing a schedule removes only that schedule.

Actual: every schedule that shares its numeric ID is removed from jobservice as well. The database still has them, so the UI shows them as scheduled, but they never run.

Jobservice keeps periodic policies in the sorted set {harbor_job_service_namespace}:period:policies. basicScheduler.Schedule() sets the score to time.Now().Unix() + rand.Int63n(10) (src/jobservice/period/basic_scheduler.go:91), so policies registered within a few seconds of each other often get the same score. UnSchedule() removes the policy with ZREMRANGEBYSCORE key score score (basic_scheduler.go:162), which deletes every member that has that score.

Many policies are registered at once whenever jobservice rebuilds its policy set. When the jobservice Redis database has lost its data or has been cleared, the startup sync in src/jobservice/sync/schedule.go registers every schedule from the database again, usually within one or two seconds. Any unschedule after that also takes out the schedules that share the score, for example when a user edits or deletes a scheduled policy.

The lost schedules are not restored until jobservice restarts, because the sync runs only at startup. UnSchedule() logs nothing above debug level, so the removal leaves no trace in the logs.

Steps to reproduce the problem:

Unit level, against a real Redis: register 20 policies with Schedule() in a loop, then call UnSchedule() for one policy whose score is shared with others. On main at 51e66ab76, the 20 policies got 10 distinct scores, and unscheduling one of them left 17 policies in the set instead of 19. The test is below.

On a running instance:

  1. Create about 20 scheduled policies with the same cron, for example a daily tag retention policy in 20 projects.
  2. In the jobservice Redis database, delete {harbor_job_service_namespace}:period:policies and restart jobservice. The log shows Sync: restore missing schedule lines with the same timestamp.
  3. Run ZRANGE '{harbor_job_service_namespace}:period:policies' 0 -1 WITHSCORES. Several members have the same score.
  4. Change the schedule of one of those policies in the UI.
  5. Run the ZRANGE again. Besides the edited policy's old entry, the other members that had the same score are gone. Those policies stop running, and the UI still shows them as scheduled.

To find affected schedules, compare the task.job_id of each schedule's SCHEDULER execution with the member IDs in period:policies. A schedule whose job ID is missing from the set does not run.

Versions:

  • harbor version: main at 51e66ab76. The score and removal code has not changed since the schedule sync was added in 2021 (6b5cd3a7a).
  • Deployment: Kubernetes, Redis without persistence.

Additional context:

We hit this in production on a v2.15 based build after a node rotation. The sync restored the schedules within one second. Later, one unschedule removed several tag retention and replication schedules along with the schedule it targeted. Nothing was logged when they were removed.

Test used for the unit-level reproduction (src/jobservice/period/repro_collision_test.go)
go
//go:build db

package period

import (
	"context"
	"fmt"
	"sync"
	"testing"

	"github.com/goharbor/harbor/src/jobservice/common/utils"
	"github.com/goharbor/harbor/src/jobservice/env"
	"github.com/goharbor/harbor/src/jobservice/job"
	"github.com/goharbor/harbor/src/jobservice/lcm"
	"github.com/goharbor/harbor/src/jobservice/tests"
)

func TestReproScoreCollision(t *testing.T) {
	ctx, cancel := context.WithCancel(context.WithValue(context.Background(), utils.NodeID, "repro"))
	defer cancel()
	ns := tests.GiveMeTestNamespace()
	pool := tests.GiveMeRedisPool()
	envCtx := &env.Context{SystemContext: ctx, WG: new(sync.WaitGroup)}
	ctl := lcm.NewController(envCtx, ns, pool, func(string, *job.StatusChange) error { return nil })
	s := NewScheduler(ctx, ns, pool, ctl)

	for i := 0; i < 20; i++ {
		p := &Policy{ID: fmt.Sprintf("policy-%02d", i), JobName: job.SampleJob, CronSpec: "0 0 0 * * *"}
		pid, err := s.Schedule(p)
		if err != nil {
			t.Fatal(err)
		}
		if _, err := ctl.New(&job.Stats{Info: &job.StatsInfo{JobID: p.ID, Status: job.ScheduledStatus.String(), JobName: p.JobName, JobKind: job.KindPeriodic, NumericPID: pid, CronSpec: p.CronSpec}}); err != nil {
			t.Fatal(err)
		}
	}

	conn := pool.Get()
	defer conn.Close()
	before, _ := Load(ns, conn)
	scores := map[int64][]string{}
	for _, p := range before {
		scores[p.NumericID] = append(scores[p.NumericID], p.ID)
	}
	t.Logf("registered=%d distinct scores=%d", len(before), len(scores))

	var victim string
	for _, ids := range scores {
		if len(ids) > 1 {
			victim = ids[0]
			t.Logf("unscheduling %s, shares its score with %v", victim, ids[1:])
			break
		}
	}
	if err := s.UnSchedule(victim); err != nil {
		t.Fatal(err)
	}
	after, _ := Load(ns, conn)
	t.Logf("after unscheduling 1 policy: registered=%d (expected %d)", len(after), len(before)-1)
	_ = tests.ClearAll(ns, conn)
}
REDIS_HOST=localhost go test -tags db -run TestReproScoreCollision -v ./jobservice/period/