Skip to content

Conversation

evan-onyx
Copy link
Contributor

@evan-onyx evan-onyx commented Aug 22, 2025

Description

fixes issue in multitenant where heartbeats weren't being sent

How Has This Been Tested?

n/a, pretty important fix but 0 impact if it doesn't work

Backporting (check the box to trigger backport action)

Note: You have to check that the action passes, otherwise resolve the conflicts manually and tag the patches.

  • This PR should be backported (make sure to check that the backport attempt succeeds)
  • [Optional] Override Linear Check

Summary by cubic

Propagates the multi-tenant context to the heartbeat thread so heartbeats are sent reliably during indexing in multi-tenant runs. Fixes missed heartbeats caused by contextvars not being passed to the background thread.

  • Bug Fixes
    • Run heartbeat_loop inside contextvars.copy_context().run(...) to preserve tenant context.
    • Remove extra debug logging from the loop.

@evan-onyx evan-onyx requested a review from a team as a code owner August 22, 2025 01:34
Copy link

vercel bot commented Aug 22, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
internal-search Ready Ready Preview Comment Aug 22, 2025 1:35am

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Summary

This PR fixes a critical multi-tenant context propagation issue in the heartbeat functionality. The problem was that background heartbeat threads were not inheriting context variables from their parent thread, specifically the CURRENT_TENANT_ID_CONTEXTVAR which is essential for proper tenant identification in multi-tenant deployments.

The core fix involves using Python's standard context variable propagation pattern for threads. Instead of directly passing the heartbeat_loop function to threading.Thread, the code now:

  1. Captures the current context using contextvars.copy_context()
  2. Uses context.run() to execute the heartbeat function within that captured context

This ensures that when get_session_with_current_tenant() is called within the heartbeat thread (line 21), the get_current_tenant_id() function can successfully retrieve the tenant ID from CURRENT_TENANT_ID_CONTEXTVAR. Without this fix, heartbeat threads would either fail with a RuntimeError in multi-tenant mode or potentially access the wrong tenant's data.

The change also removes a debug log statement that was likely adding unnecessary noise to the logs. This fix is crucial for proper multi-tenant isolation and ensures that background processes maintain the correct tenant context.

Confidence score: 4/5

  • This PR addresses a well-defined issue with a standard Python solution for context variable propagation
  • The fix follows established patterns for maintaining context across threads and aligns with the codebase's multi-tenant architecture
  • Pay close attention to the threading context propagation logic and ensure it works correctly across different deployment scenarios

1 file reviewed, no comments

Edit Code Review Bot Settings | Greptile

Copy link
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

@evan-onyx evan-onyx merged commit 04a607a into main Aug 25, 2025
27 of 34 checks passed
@evan-onyx evan-onyx deleted the fix/heartbeat-context-vars branch August 25, 2025 20:35
AnkitTukatek pushed a commit to TukaTek/onyx that referenced this pull request Sep 23, 2025
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.

2 participants