Skip to content

[Bug] postprocessing/antivirus ack events before the work is durable: transient NATS errors orphan uploads in "processing" until a manual resume #4085

Open

Description

@suse-coder

Describe the bug

Uploads can permanently fall out of the postprocessing pipeline. The upload session stays processing=true (status e.g. started postprocessing step: virusscan), ClamAV is idle, and the upload never finishes on its own. opencloud storage-users uploads sessions --processing=true --resume finishes them immediately.

Root cause: in several places, postprocessing and antivirus ack a JetStream message, or hold the follow-up work only in memory, before the next state is durably stored or published. A transient NATS error (e.g. the default 5s request timeout) or a process restart at that moment loses the only event that drives the upload. Nothing detects the orphan afterwards: there is no step deadline and no reconciler.

The clearest case: when postprocessing cannot read the upload's record from its KV store, it returns an error but still acks the incoming PostprocessingStepFinished (the antivirus result). The scan result is discarded, and the upload stays in virusscan forever. Store write errors and publish errors are already handled correctly (ackEvent = false → redelivery); the read path is not.

Steps to reproduce

  1. Run OpenCloud v9.2.0 with POSTPROCESSING_STEPS=virusscan and the ClamAV scanner. The default postprocessing store is nats-js-kv on the same NATS as the events.
  2. Upload a large burst of small files (e.g. a source tree with thousands of .java files) so that NATS requests start timing out.
  3. Observe:
    • opencloud storage-users uploads sessions --processing=true keeps listing uploads that never finish;
    • ClamAV is idle (no active scans) while those uploads are shown as started postprocessing step: virusscan;
    • nothing changes over time; pod restarts do not finish them.
  4. Run opencloud storage-users uploads sessions --processing=true --resume. All listed uploads are scanned and finish, i.e. the pipeline itself is healthy; only the driving event was lost.

Deterministic reproduction of the main code path (unit test level): give postprocessing a store whose Read returns a non-ErrNotFound error, deliver a PostprocessingStepFinished, and observe that the event is acked and nothing is published.

Expected behavior

Transient NATS/KV errors and restarts delay uploads but never lose them:

  • a message is acked only after the next state is stored and the successor event is published;
  • otherwise it is not acked (or Naked), so JetStream redelivers it;
  • uploads stuck in one step longer than a timeout are picked up again automatically.

Actual behavior

  • Uploads stay processing=true indefinitely and are only recovered by a manual --resume.
  • ClamAV is idle because no event for these uploads exists anymore (the status text is set by storage-users, not by antivirus, see below).
  • The losses cluster during upload bursts, because the same NATS carries all events (published synchronously) and the postprocessing KV, and every event is delivered to every consumer group.

Setup

  • OpenCloud v9.2.0 (tag 8b4139136232a5feac39c24be9a7a03d46c67bfc); the same code is on main as of 2026-11-20.
  • reva v3.0.1, go-micro natsjs v1.2.2, nats.go v1.55.0, nats-server v2.15.0, store opencloud-eu/go-micro-plugins/v4/store/[email protected].
  • Deployment:
Details

POSTPROCESSING_STEPS=virusscan
POSTPROCESSING_STORE=nats-js-kv
ANTIVIRUS_SCANNER_TYPE=clamav
ANTIVIRUS_WORKERS=10

Additional context

All line numbers below are from v9.2.0; on main they are identical or shifted by up to 3 lines.

1. postprocessing acks the event when the store read fails (main bug)

services/postprocessing/pkg/service/service.go

183  ackEvent := true
184  defer func() {
185      if ackEvent {
186          if err := e.Ack(); err != nil { ... }
...
208  case events.PostprocessingStepFinished:
...
219      pp, err = pps.getPP(pps.store, ev.UploadID)
220      if err != nil {
221          pps.log.Error().Str("uploadID", ev.UploadID).Err(err).Msg("cannot get upload")
222          return fmt.Errorf("%w: cannot get upload", ErrEvent)   // ackEvent still true -> acked, result lost
223      }

The same pattern exists at :258-262 (StartPostprocessingStep / delay) and :271-275 (UploadReady).

By contrast, the code already does this correctly for writes and publishes:

303  if err := storePP(pps.store, pp); err != nil {
304      ackEvent = false
...
311  if err := pps.publishWithRetry(ctx, &e, next); err != nil {
312      // The successor event never made it onto the bus. Don't ack the source event so
313      // jetstream redelivers it and the upload can continue instead of being stuck in
314      // the store forever.
315      ackEvent = false

Why a read can fail transiently:

  • getPP (:371-380) maps only store.ErrNotFound to ErrNotFound and returns every other store error as is (log: reading from store: upload store read error).
  • The store plugin's getRecord (nats-js-kv/nats.go:457-462) maps nats.ErrKeyNotFound to store.ErrNotFound and wraps every other bucket.Get error as Failed to get object from bucket.
  • bucket.Get is a JetStream request ([email protected]/kv.go:642-662, GetLastMsg) with the default request wait defaultRequestWait = 5 * time.Second (js.go:303); reva's pkg/store/store.go:143-147 sets no other JetStream options.
  • By default the store and the events use the same NATS endpoint (services/postprocessing/pkg/config/defaults/defaultconfig.go:31, 43).

2. postprocessing retries and delays live only in memory

service.go:226-246: on outcome: retry the incoming event is acked, and the retry is a goroutine:

230  go func() {
231      time.Sleep(backoff)
...
242      err := events.Publish(ctx, pps.pub, retryEvent)
243      if err != nil {
244          pps.log.Error()...Msg("cannot publish RestartPostprocessing event")   // only logged
245      }
246  }()
  • NextStep (postprocessing.go:59-64) returns a PostprocessingRetry event, which is published immediately, but nothing consumes PostprocessingRetry in OpenCloud or reva v3.0.1. The goroutine above is therefore the only thing that ever re-sends the step.
  • The retry is lost if the process restarts during the backoff, or if that single publish fails (no retry, not acked-later, only logged).
  • Backoff: RetryBackoffDuration 5s × 2^(failures−1), MaxRetries 14 (defaultconfig.go:37-38, postprocessing.go:95-100), so a single wait reaches 5s × 2^13 ≈ 11.4 h.
  • The delay step has the same pattern: Delay (postprocessing.go:79-85) sleeps in a goroutine, and the publish error is only logged (service.go:263-267).

3. antivirus acks before scanning, and exits on a publish error

  • services/antivirus/pkg/service/service.go:135: events.Consume(natsStream, "antivirus", events.StartPostprocessingStep{}) (reva typed consumer). There is no Ack() anywhere in services/antivirus.

  • The typed consumer goes through go-micro natsjs, js.QueueSubscribe(topic, group, handleMsg, ...) ([email protected]/nats.go:242-244), without nats.ManualAck().

  • nats.go then auto-acks after the callback returns ([email protected]/js.go:1941-1945):

    // Auto acknowledge unless manual ack is set or policy is set to AckNonePolicy
    if cb != nil && !o.mack && o.cfg.AckPolicy != AckNonePolicy {
        ocb := cb
        cb = func(m *Msg) { ocb(m); m.Ack() }
    }

    The callback returns as soon as the event has been handed to reva's channel (natsjs nats.go:193-198), i.e. before the download and scan.

  • If publishing the result fails, antivirus calls log.Fatal (service.go:253-255, also :156, :160 in Run). The process exits, and every event already handed over (up to ANTIVIRUS_WORKERS + 1) is lost. In an all-in-one deployment where antivirus runs in the same process, this also restarts postprocessing and loses all in-memory retries from (2).

(Postprocessing itself was changed in #3347 to retry publishing instead of exiting; antivirus was not.)

4. Why this clusters during upload bursts

  • Every event goes to one subject: reva events.Publish → s.Publish(MainQueueName, ...) (reva pkg/events/events.go:179), and natsjs publishes on event.Topic (natsjs nats.go:135-145).
  • Publishing is synchronous: reva sets natsjs.SynchronousPublish(true) (reva pkg/events/stream/nats.go:67), so every publish waits for the PubAck.
  • No consumer sets a subject filter (FilterSubject appears nowhere in the natsjs plugin or in raw), and each service uses its own durable group. Every event is therefore delivered to every group: in v9.2.0 that is userlog, storage-users, sse-<uuid>, sharing, postprocessing-pull, policies, notifications, graph, evhistory, clientlog, auth-guest, audit, antivirus, activitylog, search-pull, plus reva-internal storage-users upload, dcfs-revisions and jsoncs3sharemanager (depending on enabled services). Most groups discard the event, but NATS still delivers and tracks each copy.
  • The postprocessing KV is served by the same NATS.

A burst of small uploads therefore produces many timeouts at once, and paths (1)–(3) create many orphaned uploads at the same time.

5. No self-healing: orphans are never picked up again

  • The upload state exists twice: the storage-users session (processing=true + status text) and the postprocessing KV record (Status.CurrentStep). No component checks that an event for the upload still exists, and there is no step deadline or reconciler.
  • The visible status text is misleading: started postprocessing step: virusscan is set by storage-users' own consumer when it sees the StartPostprocessingStep (reva pkg/upload/postprocessing.go:101-112), independently of whether antivirus received it or its result arrived.
  • Recovery is manual: uploads sessions --resume (services/storage-users/pkg/command/uploads.go:213) → ResumePostprocessing → resumePP (service.go:440-461) re-publishes pp.CurrentStep(), or publishes RestartPostprocessing if the record is gone.

Suggested fixes

  1. Do not ack on transient store read errors (all three getPP call sites):

    pp, err = pps.getPP(pps.store, ev.UploadID)
    if err != nil {
        if !errors.Is(err, ErrNotFound) {
            ackEvent = false // transient store error: let JetStream redeliver
        }
        pps.log.Error().Str("uploadID", ev.UploadID).Err(err).Msg("cannot get upload")
        return fmt.Errorf("%w: cannot get upload", ErrEvent)
    }

    Keep acking on ErrNotFound, so events without an upload (e.g. postprocessing logs "cannot get upload" for every non-empty file the posix driver assimilates #3606) are not redelivered forever.

  2. Make retries and delays durable: keep the retry/delay deadline in the postprocessing store (or use JetStream NakWithDelay on the source event) instead of time.Sleep goroutines, and do not drop a failed retry publish.

  3. antivirus: ack after the result is published: migrate it to raw.Stream like postprocessing (raw.Event has Ack/Nak/InProgress), ack after publishing PostprocessingStepFinished, Nak on retryable errors, and replace log.Fatal on a publish error with a retry (as fix(postprocessing): retry publishing events instead of killing the server #3347 did for postprocessing). AckWait must exceed ANTIVIRUS_CLAMAV_SCAN_TIMEOUT (default 5m).

  4. Add a reconciler: periodically re-publish the current step for uploads that have been in one step longer than its timeout (an automated postprocessing resume).

  5. (Load, optional) Use subject-filtered consumers so each service only receives the event types it handles.

Activity

  1. moved this from Qualification to Done in OpenCloud Team Boardon Nov 20, 2026
  2. changed the title [-][Bug] antivirus never acks main-queue events: JetStream stalls at max_ack_pending (1000), clamd goes idle and uploads stay in postprocessing[/-] [+][Bug] postprocessing acks PostprocessingStepFinished when the upload store read fails, so the antivirus result is lost and uploads stay in processing forever[/+] on Nov 20, 2026
  3. changed the title [-][Bug] postprocessing acks PostprocessingStepFinished when the upload store read fails, so the antivirus result is lost and uploads stay in processing forever[/-] [+][Bug] postprocessing/antivirus ack events before the work is durable: transient NATS errors orphan uploads in "processing" until a manual resume[/+] on Nov 20, 2026
  4. RyoKusnadi commented on Nov 20, 2026

    @RyoKusnadi
    Contributor

    Hi @suse-coder @aduffeck, can I do it? Only fix 1 from the issue, the rest stays open.

    How I plan to do it:

    1. In services/postprocessing/pkg/service/service.go, processEvent acks by default (ackEvent := true). The store-write and publish failures already set ackEvent = false, but the three getPP error branches (PostprocessingStepFinished, StartPostprocessingStep, UploadReady) don't, so a transient store error still acks.
    2. In those three branches, set ackEvent = false when the error isn't ErrNotFound, so the event gets redelivered.
    3. Keep acking on ErrNotFound, so events for uploads the service doesn't know aren't redelivered forever.
    4. Tests in service_test.go with a store whose Read fails with a non-not-found error: the event isn't acked and nothing is published, for all three event types.
    5. Must not change: ErrNotFound still acks, and a normal read still acks.

    Durable retries/delays, the antivirus ack and a reconciler (items 2-5) I'd leave for separate PRs if you want them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions