Skip to content

fix(deep-crawl): ContextVar crash in streaming deep crawl (#1917)#1922

Open
ntohidi wants to merge 1 commit intodevelopfrom
fix/deep-crawl-streaming-contextvar-1917
Open

fix(deep-crawl): ContextVar crash in streaming deep crawl (#1917)#1922
ntohidi wants to merge 1 commit intodevelopfrom
fix/deep-crawl-streaming-contextvar-1917

Conversation

@ntohidi
Copy link
Copy Markdown
Collaborator

@ntohidi ntohidi commented Apr 16, 2026

Summary

  • Fixes [Bug]: Deep crawling crashes from time to time #1917: DeepCrawlDecorator used ContextVar.reset(token) in the async generator's finally block. When Starlette's StreamingResponse consumes the generator in a different asyncio Task (different Context), reset() raises ValueError: was created in a different Context.
  • Fix: Replaced reset(token) with set(False) on both the streaming and batch paths. Safe because deep_crawl_active is never nested — the guard on line 21 prevents re-entry.
  • Added 9 tests covering cross-context safety, state management, error handling, and concurrent requests.

Test plan

  • Minimal reproduction confirms the bug (ContextVar token in different task)
  • Docker /crawl/stream endpoint: 3/3 requests crash before fix, 0/3 after
  • 9 new tests pass (tests/deep_crawling/test_deep_crawl_contextvar.py)
  • All 57 existing deep crawl tests still pass

#1917)

ContextVar.reset(token) requires the same Context that created the token.
When Starlette's StreamingResponse consumes the async generator in a
different Task, the Context changes and reset() raises ValueError.

Replaced with set(False) which works across context boundaries. Safe
because deep_crawl_active is never nested — the guard on line 21
prevents re-entry.
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.

1 participant