river icon indicating copy to clipboard operation
river copied to clipboard

Add timeout to standard pilot fetch

Open peter941221 opened this issue 6 months ago • 1 comments

Summary

Fix StandardPilot.JobGetAvailable so a stalled fetch does not hang a producer indefinitely.

Problem

producer.dispatchWork intentionally strips cancellation from the work context before fetching jobs so an in-flight fetch is allowed to complete during shutdown:

  • producer.go:744-766

That is reasonable, but StandardPilot.JobGetAvailable forwarded directly to exec.JobGetAvailable with no timeout at all:

  • rivershared/riverpilot/standard_pilot.go:18-22

This meant a stalled driver call could block a standard-pilot producer forever. The pro pilot already applies per-attempt fetch timeouts, so the standard pilot was the outlier.

Change

Add a 10-second timeout inside StandardPilot.JobGetAvailable before calling the driver.

This keeps the existing shutdown semantics intact:

  • fetches still ignore parent cancellation from dispatchWork
  • but they are now bounded, so a wedged DB call eventually returns instead of freezing the producer forever

The timeout is local to the standard pilot so there is no driver SQL change and no producer state-machine change.

Testing

  • added rivershared/riverpilot/standard_pilot_test.go
  • covered MaxToLock <= 0 no-op behavior
  • covered a hung JobGetAvailable call timing out with context.DeadlineExceeded
  • covered parent cancellation still winning when the incoming context is already canceled

Verification

Locally verified with:

  • GOPROXY=https://goproxy.cn,direct GOSUMDB=off go test ./rivershared/riverpilot -count=1

Closes #1026.

peter941221 avatar May 25 '26 13:05 peter941221

Another more robust option is to do fetches within a transaction that sets a statement_timeout at the DB level. This is far more robust because it's not prone to accidentally stranding jobs, although the extra round trips may impact throughput. As long as the Go side waits for db_timeout + margin it'd be super unlikely to strand jobs. We basically do this on the Pro side already due to extra logic running within fetches. Thoughts @brandur?

bgentry avatar May 28 '26 15:05 bgentry

@bgentry WFM. It'd probably be worth re-running the benchmark on the branch to verify no major degradation in performance, but given the fetch queries are relatively few compared to everything else, hopefully there wouldn't be.

You were previously against use of statement_timeout with a context cancellation though weren't you? You're saying there'd still be both here right?

brandur avatar May 30 '26 17:05 brandur

@bgentry @brandur I put it on StandardPilot first because I wasn’t sure yet whether ProPilot should inherit the same timeout semantics. If both pilots are really meant to have the same JobGetAvailable cancellation policy, then I agree dispatchWork is probably the cleaner home for it since it makes the timeout automatic instead of something each pilot has to remember. My hesitation was exactly the case you called out: if ProPilot wants a meaningfully longer timeout, pushing it up immediately may be the wrong abstraction. I can take a closer look at the pro path and either move this up to dispatchWork or keep it split if the semantics are intentionally different.

peter941221 avatar May 30 '26 22:05 peter941221

@peter941221 Yep, makes sense. Thanks.

brandur avatar May 31 '26 03:05 brandur

  1. I’m going to keep this PR scoped to the StandardPilot timeout.

  2. I don’t want to move the timeout up to dispatchWork or switch this branch to a transaction + statement_timeout design until I’ve checked whether the Pro fetch path is meant to keep different timeout semantics.

  3. If we do want one policy across both pilots, I’d rather handle that as a separate follow-up with a benchmark around the extra round trips.

peter941221 avatar May 31 '26 04:05 peter941221

Thx. Gonna pull this in and make a few tweaks on top.

brandur avatar May 31 '26 19:05 brandur

Actually, Peter would you mind "signing" our CLA over at https://github.com/riverqueue/rivercla as well? Thx!

brandur avatar Jun 01 '26 05:06 brandur

Signed the individual CLA here: https://github.com/riverqueue/rivercla/pull/26

peter941221 avatar Jun 01 '26 06:06 peter941221