riverqueue / riverqueue/river

Job cancellation doesn't work in database/sql driver

Open
#630 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
5.7k
Forks
179
Avg merge
15h 43m
Merged PRs (30d)
13

Description

Hi!

I am using the example SortWorker job with small adjustments to test longer-running jobs with cancellation but it does not seem to work.

const SortJobKind = "sort"

type SortArgs struct {
  Strings []string `json:"strings"`
}

func (SortArgs) Kind() string {
  return SortJobKind
}

type SortWorker struct {
  river.WorkerDefaults[SortArgs]
}

func (w *SortWorker) Work(ctx context.Context, job *river.Job[SortArgs]) error {
  log.Printf("STARTING: %d", job.ID)
  select {
  case <-ctx.Done():
    log.Printf("CANCELLED: %d", job.ID)
    return ctx.Err()
  case <-time.After(20 * time.Second):
    log.Printf("WILL DO: %d", job.ID)
  }

  items := job.Args.Strings
  sort.Strings(items)
  log.Printf("RESULT: %+v\n", items)
  return nil
}

When the job appears in the User Interface (riverui), I click Cancel but the job keeps running. In the logs I eventually get from STARTING to WILL DO but never observe CANCELLED.

The only thing that indicates some attempt at cancellation is the metadata field on the job which states:

{
  "cancel_attempted_at": "2024-10-02T....."
}

Am I doing something wrong or is there a potential bug?

I am running v0.12.1 and using riverdatabasesql.New, though the underlying connection is pgx, wrapped with opencensus tracing (ocsql).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the SortWorker example, riverui's Cancel action, and riverdatabasesql.New using the described pgx and ocsql setup. Start by tracing cancellation from the cancel_attempted_at metadata to the Work context; done means the worker observes ctx.Done, logs CANCELLED, and stops before the 20-second delay.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.