Skip to content

fix(api): fall back to primary DB in supabaseGetScrapeById to fix /interact 404 race condition#3282

Open
firecrawl-spring[bot] wants to merge 1 commit intomainfrom
fix/interact-scrape-read-replica-race
Open

fix(api): fall back to primary DB in supabaseGetScrapeById to fix /interact 404 race condition#3282
firecrawl-spring[bot] wants to merge 1 commit intomainfrom
fix/interact-scrape-read-replica-race

Conversation

@firecrawl-spring
Copy link
Copy Markdown
Contributor

@firecrawl-spring firecrawl-spring Bot commented Apr 3, 2026

Summary

  • When v2 /scrape returns quickly (especially cache hits), the fire-and-forget logScrape insert may not yet be visible on the Supabase read replica when /interact is called immediately after
  • This causes /interact to return 404 "Job not found" because supabaseGetScrapeById reads from supabase_rr_service (read replica)
  • Adds a primary DB fallback: when the read replica returns no data, retry the query against supabase_service (primary), which resolves the race condition with zero latency impact on the normal path

Root Cause

  1. scrape.ts sets skipNuq: true for v2 API calls
  2. scrape-worker.ts creates logScrapePromise but does NOT await it when skipNuq is true — only attaches a .catch() handler
  3. The HTTP response is sent to the client before the primary DB insert completes
  4. /interact reads from supabase_rr_service (read replica) via supabaseGetScrapeById
  5. The record hasn't replicated yet → 404

Fix

In supabaseGetScrapeById, when the read replica returns null, fall back to querying the primary database. This:

  • Adds zero latency on the normal path (read replica hit)
  • Only queries primary when record is missing — exactly the race condition window
  • Is a single-function, 5-line change with no side effects

Related Pylon Ticket

https://app.usepylon.com/issues?issueNumber=25927


Summary by cubic

Fixes a race that caused /interact 404s right after /scrape. supabaseGetScrapeById now falls back to the primary DB when the read replica returns no data.

  • Bug Fixes
    • Query read replica first; if empty, retry against primary supabase_service.
    • Resolves 404 “Job not found” from replication lag after fire-and-forget logScrape.
    • No added latency on the normal path; primary is only used on replica miss.

Written for commit 86bed05. Summary will update on new commits.

…teract 404 race condition

When v2 /scrape completes (especially cache hits), the response is sent
before the fire-and-forget logScrape insert is visible on the read
replica. If /interact is called immediately, it reads from the replica
and returns 404 "Job not found".

Add a primary DB fallback in supabaseGetScrapeById: when the read
replica returns no data, retry against the primary. This adds zero
latency on the normal path (replica hit) and only queries the primary
when the record is missing, which is exactly the race condition window.

Co-Authored-By: micahstairs <micah@sideguide.dev>
@firecrawl-spring firecrawl-spring Bot requested a review from micahstairs April 3, 2026 16:03
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

@blacksmith-sh
Copy link
Copy Markdown
Contributor

blacksmith-sh Bot commented Apr 3, 2026

Found 1 test failure on Blacksmith runners:

Failure

Test View Logs
src/tests/snips/v2/scrape-skip-tls.test.ts/
V2 Scrape skipTlsVerification Default should default skipTlsVerification to true in v2
API
View Logs

Fix in Cursor

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants