From 42c09d81c77f01eb1516bfd2d6a4a7582201ee25 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 09:44:22 +0000 Subject: [PATCH 1/2] Wait for running jobs before the app shuts down A catch-up run started when the server starts runs in the background and records its result when it ends. When the app stopped first, as tests do as soon as they finish, that write hit a closed app and crashed the test run now and then. Each job now tracks its runs, and the app waits for them on terminate; runs that would start after that are skipped. --- backend/jobs/jobs.go | 45 +++++++++++++++++++++++++++++++++++---- backend/jobs/jobs_test.go | 36 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/backend/jobs/jobs.go b/backend/jobs/jobs.go index 65ec39c..c955457 100644 --- a/backend/jobs/jobs.go +++ b/backend/jobs/jobs.go @@ -39,16 +39,53 @@ func MustAdd(app core.App, name, cronExpr string, fn Func) { if err != nil { panic(fmt.Sprintf("job %q: %v", name, err)) } - app.Cron().MustAdd(name, cronExpr, func() { Run(app, name, fn) }) + runs := &background{} + app.Cron().MustAdd(name, cronExpr, func() { + runs.do(func() { Run(app, name, fn) }) + }) app.OnServe().BindFunc(func(se *core.ServeEvent) error { - // checked before serving, so it never outlives the app; only a run goes - // to the background, so a slow job doesn't hold up the start + // checked before serving; only the run goes to the background, so a + // slow job doesn't hold up the start if due(se.App, name, schedule, time.Now()) { - go Run(se.App, name, fn) + go runs.do(func() { Run(se.App, name, fn) }) } return se.Next() }) + + // the app waits for a running job before it closes its database, so a run + // never writes to a closed app + app.OnTerminate().BindFunc(func(e *core.TerminateEvent) error { + runs.stop() + return e.Next() + }) +} + +// background tracks one job's runs so the app can wait for them when it stops. +// Once stopped, new runs are skipped. +type background struct { + mu sync.Mutex + wg sync.WaitGroup + stopped bool +} + +func (b *background) do(f func()) { + b.mu.Lock() + if b.stopped { + b.mu.Unlock() + return + } + b.wg.Add(1) + b.mu.Unlock() + defer b.wg.Done() + f() +} + +func (b *background) stop() { + b.mu.Lock() + b.stopped = true + b.mu.Unlock() + b.wg.Wait() } // Run runs a job now and records the result. A run is skipped while the diff --git a/backend/jobs/jobs_test.go b/backend/jobs/jobs_test.go index 074fe91..344ffe8 100644 --- a/backend/jobs/jobs_test.go +++ b/backend/jobs/jobs_test.go @@ -191,3 +191,39 @@ func TestSignInIsRateLimited(t *testing.T) { t.Errorf("rate limits = %+v", r) } } + +func TestShutdownWaitsForARunningJob(t *testing.T) { + a := newApp(t) + col, _ := a.FindCollectionByNameOrId(Collection) + rec := core.NewRecord(col) + rec.Set("name", "slow") + rec.Set("last_run", time.Now().Add(-time.Hour)) + if err := a.Save(rec); err != nil { + t.Fatal(err) + } + started, release := make(chan struct{}), make(chan struct{}) + MustAdd(a, "slow", "* * * * *", func(core.App) error { + close(started) + <-release + return nil + }) + if err := a.OnServe().Trigger(&core.ServeEvent{App: a}, func(*core.ServeEvent) error { return nil }); err != nil { + t.Fatal(err) + } + <-started + + stopped := make(chan time.Time) + go a.OnTerminate().Trigger(&core.TerminateEvent{App: a}, func(*core.TerminateEvent) error { + stopped <- lastRun(t, a, "slow").GetDateTime("last_run").Time() + return nil + }) + select { + case <-stopped: + t.Fatal("the app stopped while the job was still running") + case <-time.After(100 * time.Millisecond): + } + close(release) + if got := <-stopped; time.Since(got) > time.Minute { + t.Errorf("last_run = %v, want the run that just finished", got) + } +} From 3caf37e5b24b0159b477fd4090efd9dd3c578b97 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 09:45:14 +0000 Subject: [PATCH 2/2] Take Alpine and Debian security fixes in each image build The released image still has zlib 1.3.2-r1 (CVE-2026-4738), so the Security workflow's image scan fails. Upgrade the runtime stage's packages and build that stage without the cache when publishing, as plasto and the other apps do. The scan passes once the next release is published. --- .github/workflows/ci.yml | 4 ++++ Dockerfile | 6 ++++-- workers/Dockerfile | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index de3a9a6..5a5db32 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -95,6 +95,8 @@ jobs: tags: shoppa:smoke build-args: VERSION=${{ env.VERSION }} cache-from: type=gha,scope=app + # Run the runtime stage fresh, so each release takes Alpine's latest security fixes. + no-cache-filters: runtime cache-to: ${{ github.event_name == 'pull_request' && 'type=gha,mode=max,scope=app' || '' }} - name: Build the worker for the smoke test uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7 @@ -103,6 +105,8 @@ jobs: load: true tags: shoppa-worker:smoke cache-from: type=gha,scope=worker + # Run the runtime stage fresh, so each release takes Debian's latest security fixes. + no-cache-filters: runtime cache-to: ${{ github.event_name == 'pull_request' && 'type=gha,mode=max,scope=worker' || '' }} - name: Smoke test run: | diff --git a/Dockerfile b/Dockerfile index 56f774f..cf21a93 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,8 +20,10 @@ RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GOARM=${TARGETVARIANT#v} \ go build -trimpath -ldflags="-s -w -X github.com/selfhostit/shoppa/app.Version=$VERSION" -o /shoppa . # --- runtime --- -FROM alpine:3.24 -RUN apk add --no-cache ca-certificates tzdata \ +FROM alpine:3.24 AS runtime +# Take Alpine's security fixes now rather than waiting for the base image to be rebuilt. +RUN apk upgrade --no-cache \ + && apk add --no-cache ca-certificates tzdata \ && adduser -D -u 1000 app \ && mkdir -p /app/pb_data && chown app:app /app/pb_data WORKDIR /app diff --git a/workers/Dockerfile b/workers/Dockerfile index 6d0c62f..9cfdeec 100644 --- a/workers/Dockerfile +++ b/workers/Dockerfile @@ -12,8 +12,12 @@ RUN uv export --locked --no-dev --no-emit-project -o requirements.txt \ && uv build --wheel -o dist # --- runtime --- -FROM python:3.15-slim +FROM python:3.15-slim AS runtime ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1 +# Take Debian's security fixes now rather than waiting for the base image to be rebuilt. +RUN apt-get update \ + && apt-get upgrade -y --no-install-recommends \ + && rm -rf /var/lib/apt/lists/* COPY --from=build /src/requirements.txt /src/dist/ /tmp/build/ RUN pip install --no-cache-dir -r /tmp/build/requirements.txt \ && pip install --no-cache-dir --no-deps /tmp/build/*.whl \