Skip to content

[MOD-13616] Use new RETURN-STRICT timeout mechanism in FT.SEARCH coordinator - #8236

Merged
lerman25 merged 15 commits into
masterfrom
omerl-search-coord-timeout-return
Feb 4, 2026
Merged

lerman25 merged 15 commits into
masterfrom
omerl-search-coord-timeout-return

Conversation

@lerman25

@lerman25 lerman25 commented Feb 1, 2026 •

Copy link
Copy Markdown
Collaborator

Partial Results Timeout for FT.SEARCH Coordinator

Summary

Implements partial results timeout support for the FT.SEARCH coordinator when ON_TIMEOUT policy is set to return-strict. Previously, the coordinator only supported the fail timeout policy. Now, when a timeout occurs, the coordinator returns partial results from shards that have already responded, along with a timeout warning.

Changes

Timeout Handling (src/module.c, src/module.h)

  • Added searchRequestCtx struct to hold parsed search request parameters on the main thread before blocking
  • Split request parsing: main thread parses request before blocking, eliminating coordinator thread parsing races
  • Implemented timeout callback that returns partial results when ON_TIMEOUT=return-strict
  • Added atomic timedOut and reducing flags for thread-safe coordination between timeout callback and coordinator thread
  • Timeout can occur at three stages: before fanout, during fanout, or during reducing - all handled gracefully

MapReduce Context (src/coord/rmr/rmr.c, src/coord/rmr/rmr.h)

  • Added MRCtx_SetTimedOut() and MRCtx_IsTimedOut() for atomic timeout flag
  • Added MRCtx_SetReducing() for claiming exclusive reducing state
  • Added mutex and condition variable for synchronizing timeout and reducer threads

Tests (tests/pytests/test_blocked_client_timeout.py)

  • Added comprehensive tests for partial results timeout scenarios:
    • Timeout before fanout (no replies received) → returns 0 results with warning
    • Timeout after partial replies → returns results from responding shards with warning
    • Tests for both FT.SEARCH and FT.PROFILE commands

Timeout Behavior

Scenario Result
Timeout before any shard replies 0 results + timeout warning
Timeout after some shards replied Partial results from responding shards + timeout warning
Timeout during reducing Waits for reducer to complete, returns partial results
Main Thread                    Coordinator Worker Thread       IO Thread (fanout)
     │                                    │                           │
     ├─ Parse request (rscParseRequest)   │                           │
     ├─ Create MRCtx with req as privdata │                           │
     ├─ Block client with timeout callback│                           │
     ├─ Set MRCtx as blocked client privdata                          │
     ├─ Queue work to coordinator pool ───┼──────────────────────────►│
     │                                    │                           │
     │                                    ├─ Check MRCtx_IsTimedOut() │
     │                                    ├─ Prepare command          │
     │                                    ├─ MR_Fanout() ─────────────┼──►│
     │                                    │                           │   │
     │                                    │                           │   ├─ Send to shards
     │                                    │                           │   ├─ Collect replies
     │                                    │                           │   │   (fanoutCallback)
     │                                    │                           │   │
     │  [TIMEOUT FIRES]                   │                           │   │
     ├─ DistSearchTimeoutPartialClient()  │                           │   │
     │   ├─ MRCtx_SetTimedOut()           │                           │   │
     │   ├─ MRCtx_TryClaimReducing()      │                           │   │
     │   │   ├─ If claimed: run reducer   │                           │   │
     │   │   └─ If not: wait for reducer  │                           │   │
     │   ├─ Reply with partial results    │                           │   │
     │   └─ Return (free_privdata called) │                           │   │
     │                                    │                           │   │
     │                                    │   [Normal completion]     │   │
     │                                    │   ├─ searchResultReducer()│◄──┤
     │                                    │   │   ├─ TryClaimReducing()
     │                                    │   │   │   (may fail if timeout claimed)
     │                                    │   │   ├─ Process replies
     │                                    │   │   ├─ SignalReducerComplete()
     │                                    │   │   └─ UnblockClient()

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Release Notes

  • This PR requires release notes
  • This PR does not require release notes

If a release note is required (bug fix / new feature / enhancement), describe the user impact of this PR in the title.


Note

Medium Risk
Touches coordinator blocked-client timeout handling, fanout reply collection, and reducer/unblock synchronization using atomics/conds, which is concurrency-sensitive and could affect query completion paths under load/timeouts.

Overview
Adds partial-results timeout support for coordinator FT.SEARCH when ON_TIMEOUT=return-strict, replying with whatever shard results have arrived plus a timeout warning instead of failing.

To support this, distributed search now parses/allocates searchRequestCtx and MRCtx on the main thread before blocking, adds timeout/reducing state + synchronization to MRCtx, discards late fanout replies after timeout, and updates the reducer/bailout paths to avoid double-reduce/double-unblock races.

Also refactors FT.SEARCH argument parsing to a single-pass ArgsCursor/ACArgSpec flow (and builds requiredFields there), extends empty-reply handling to emit timeout warnings, adds debug-only coordinator reduce pause commands (ENABLE_ASSERT), and expands Python tests to cover return-strict partial results, no-reply timeouts, and timeout-during-reduce scenarios (plus warning shape change to arrays).

Written by Cursor Bugbot for commit e5b01ca. This will update automatically on new commits. Configure here.

@lerman25
lerman25 force-pushed the omerl-search-coord-timeout-return branch from 87d3223 to e22d557 Compare February 1, 2026 13:45
@github-actions github-actions Bot added the size:M label Feb 1, 2026
@github-actions github-actions Bot added the size:L label Feb 1, 2026
@fcostaoliveira

fcostaoliveira commented Feb 1, 2026 •

Copy link
Copy Markdown
Contributor

Automated performance analysis summary

This comment was automatically generated given there is performance data available.

In summary:

  • Detected a total of 30 stable tests between versions.
  • Detected a total of 9 highly unstable benchmarks (9 baseline).
  • Detected a total of 3 improvements above the improvement water line.
  • Detected a total of 1 regressions bellow the regression water line 8.0%.

You can check a comparison in detail via the grafana link

Performance Improvements - Comparison between master and omerl-search-coord-timeout-return.

Time Period from 30 days ago. (environment used: oss-standalone)

Test Case Baseline master (median obs. +- std.dev) Comparison omerl-search-coord-timeout-return (median obs. +- std.dev) % change (higher-better) Note
search-numeric 2241 +- 3.8% (7 datapoints) 3666 63.6% IMPROVEMENT
search-ftsb-1M-enwiki_abstract-hashes-fulltext-simple-1word-query-non-sortable 168 +- 6.7% (7 datapoints) 182 8.4% IMPROVEMENT
search-numeric-sortby-optimize 26 +- 7.6% (7 datapoints) 28 8.3% IMPROVEMENT

Performance Regressions and Issues - Comparison between master and omerl-search-coord-timeout-return.

Time Period from 30 days ago. (environment used: oss-standalone)

Test Case Baseline master (median obs. +- std.dev) Comparison omerl-search-coord-timeout-return (median obs. +- std.dev) % change (higher-better) Note
search-ftsb-10K-enwiki_abstract-hashes-fulltext-aggregate-sortby-limit-0-100 941 +- 1.6% (5 datapoints) 790 -16.0% REGRESSION
search-numeric-sortby-desc-optimize 33 +- 10.7% UNSTABLE (5 datapoints) 31 -4.6% UNSTABLE (baseline high variance); server: FT.SEARCH p50 decreased 14.1% (baseline CV=8.8%); client: client latency stable; neither server nor client side confirms regression
search-filtering-tag-numeric 248 +- 16.5% UNSTABLE (5 datapoints) 245 -0.8% UNSTABLE (baseline high variance); server: FT.AGGREGATE p50 increased 18.7% (baseline CV=0.0%); client: client latency stable; only server side confirms regression (client side stable) - insufficient evidence
ftsb-1M-enwiki_abstract-hashes-fulltext-simple-1word-query 893 +- 18.1% UNSTABLE (5 datapoints) 896 0.3% UNSTABLE (baseline high variance); server: FT.SEARCH p50 increased 17.9% (baseline CV=5.1%); client: client latency stable; only server side confirms regression (client side stable) - insufficient evidence
search-numeric-sortby 3459 +- 20.1% UNSTABLE (7 datapoints) 3526 1.9% UNSTABLE (baseline high variance); server: p50 latency stable; client: client latency stable; neither server nor client side confirms regression
ftsb-1M-enwiki_abstract-hashes-fulltext-2word-union-query-non-sortable 994 +- 13.0% UNSTABLE (5 datapoints) 1046 5.3% UNSTABLE (baseline high variance); server: p50 latency stable; client: client latency stable; neither server nor client side confirms regression
ftsb-1M-enwiki_abstract-hashes-fulltext-2word-union-query 2828 +- 11.6% UNSTABLE (5 datapoints) 3060 8.2% UNSTABLE (baseline high variance); server: p50 latency stable; client: client latency stable; neither server nor client side confirms regression
ftsb-1M-enwiki_abstract-hashes-fulltext-2word-intersection-query-non-sortable 35 +- 45.0% UNSTABLE (5 datapoints) 39 11.5% UNSTABLE (baseline high variance); server: FT.SEARCH p50 decreased 8.5% (baseline CV=9.9%); client: OverallQuantiles.allCommands.q50 decreased 13.7% (baseline CV=10.4%); neither server nor client side confirms regression
ftsb-1M-enwiki_abstract-hashes-fulltext-2word-intersection-query 361 +- 26.4% UNSTABLE (5 datapoints) 403 11.7% UNSTABLE (baseline high variance); server: p50 latency stable; client: client latency stable; neither server nor client side confirms regression
search-numeric-sortby-desc 2239 +- 26.3% UNSTABLE (5 datapoints) 3534 57.9% UNSTABLE (baseline high variance); server: FT.SEARCH p50 decreased 36.2% (baseline CV=27.6%); client: Latency decreased 30.4% (baseline CV=27.4%); neither server nor client side confirms regression
Tests with No Significant Changes (30 tests)

Tests with No Significant Changes

Test Case Baseline master (median obs. +- std.dev) Comparison omerl-search-coord-timeout-return (median obs. +- std.dev) % change (higher-better) Note
ftsb-10K-enwiki_abstract-hashes-fulltext-sortby 73 +- 1.4% (7 datapoints) 70.00 -4.2% potential REGRESSION
ftsb-10K-enwiki_abstract-hashes-term-prefix 6093 +- 6.7% (5 datapoints) 6325.00 3.8% potential IMPROVEMENT
ftsb-10K-enwiki_abstract-hashes-term-suffix 2205 +- 0.7% (5 datapoints) 2277.00 3.3% potential IMPROVEMENT
ftsb-10K-enwiki_abstract-hashes-term-suffix-withsuffixtrie 16697 +- 1.5% (5 datapoints) 16355.00 -2.1% No Change
ftsb-10K-enwiki_abstract-hashes-term-wildcard 9103 +- 3.0% (7 datapoints) 9374.00 3.0% No Change
ftsb-10K-enwiki_pages-hashes-fulltext-mixed_simple-1word-query_write_1_to_read_20.yml 1014 +- 1.8% (7 datapoints) 1066.00 5.1% potential IMPROVEMENT
ftsb-10K-enwiki_pages-hashes-load 63254 +- 3.3% (5 datapoints) 63642.00 0.6% No Change
ftsb-10K-multivalue-numeric-json 967 +- 2.4% (7 datapoints) 960.00 -0.7% No Change
ftsb-10K-singlevalue-numeric-json 469 +- 1.0% (7 datapoints) 477.00 1.7% No Change
ftsb-1K-enwiki_abstract-hashes-term-contains 1970 +- 0.8% (7 datapoints) 2017.00 2.4% No Change
ftsb-1M-enwiki_abstract-hashes-load 23067 +- 1.4% (7 datapoints) 22217.00 -3.7% potential REGRESSION
ftsb-1M-nyc_taxis-ftadd-load 28181 +- 2.0% (7 datapoints) 28211.00 0.1% No Change
ftsb-1M-nyc_taxis-hashes-load 30639 +- 2.2% (7 datapoints) 30688.00 0.2% No Change
search-aggregate-post-filter-simple.yml 17323 +- 1.6% (5 datapoints) 17267.00 -0.3% No Change
search-filtering-tag-numeric-filter-pipeline 10824 +- 1.3% (7 datapoints) 10904.00 0.7% No Change
search-ftsb-10K-enwiki_abstract-hashes-fulltext-search-sortby-limit-0-100 800 +- 1.5% (7 datapoints) 790.00 -1.2% No Change
search-ftsb-10K-enwiki_abstract-hashes-term-withoutsuffix-trie 14307 +- 1.3% (7 datapoints) 14385.00 0.5% No Change
search-ftsb-10K-enwiki_abstract-hashes-term-withsuffix-trie 14324 +- 1.2% (7 datapoints) 14068.00 -1.8% No Change
search-ftsb-1700K-docs-union-iterators-q3 8.0 +- 1.6% (7 datapoints) 8.00 -0.2% No Change
search-ftsb-1M-enwiki_abstract-hashes-fulltext-simple-1word-query-one-indexed-field 7289 +- 2.1% (7 datapoints) 7260.00 -0.4% No Change
search-ftsb-370K-docs-union-iterators-q4 8.3 +- 1.0% (7 datapoints) 8.20 -1.2% No Change
search-ftsb-5200K-docs-union-iterators-q1 0.83 +- 0.9% (7 datapoints) 0.82 -1.2% No Change
search-ftsb-5500K-docs-union-iterators-q2 1.2 +- 1.0% (7 datapoints) 1.20 0.0%
search-geo 223 +- 3.2% (7 datapoints) 228.00 2.5% No Change
search-high-cardinality-negation-term-baseline 39 +- 1.1% (5 datapoints) 36.00 -7.0% potential REGRESSION
search-high-cardinality-negation-term-comparison_union_all_other_terms 14 +- 1.5% (5 datapoints) 14.00 -1.8% No Change
search-numeric-optimize 8136 +- 1.8% (5 datapoints) 7993.00 -1.8% No Change
vecsim-arxiv-titles-384-angular-filters-m16-ef-128-fulltext-filter 609 +- 3.9% (7 datapoints) 630.00 3.4% potential IMPROVEMENT
vecsim-arxiv-titles-384-angular-filters-m16-ef-128-numeric-filter 150 +- 3.8% (7 datapoints) 157.00 4.6% potential IMPROVEMENT
vecsim-arxiv-titles-384-angular-filters-m16-ef-128-tag-filter 15991 +- 1.6% (7 datapoints) 15737.00 -1.6% No Change

@lerman25 lerman25 changed the title Omerl search coord timeout return [MOD-13616] Use new RETURN-STRICT timeout mechanism in FT.SEARCH coordinator Feb 1, 2026
@lerman25
lerman25 marked this pull request as ready for review February 1, 2026 16:03
Comment thread src/module.c Resolved
Comment thread src/module.c Resolved
Comment thread src/module.c Resolved
Comment thread src/coord/rmr/rmr.c Resolved
@codecov

codecov Bot commented Feb 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.54545% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.01%. Comparing base (c54f5c6) to head (e5b01ca).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
src/module.c 91.05% 17 Missing ⚠️
src/debug_commands.c 82.35% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8236      +/-   ##
==========================================
+ Coverage   82.94%   83.01%   +0.06%     
==========================================
  Files         389      389              
  Lines       57262    57371     +109     
  Branches    15567    15567              
==========================================
+ Hits        47497    47627     +130     
+ Misses       9606     9585      -21     
  Partials      159      159              
Flag Coverage Δ
flow 84.20% <90.54%> (+0.19%) ⬆️
unit 50.45% <0.00%> (-0.10%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/module.c Resolved
Comment thread src/module.c Resolved
Comment thread src/module.c Resolved
Comment thread src/module.c Resolved
Comment thread src/coord/rmr/rmr.c Resolved

@GuyAv46 GuyAv46 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice and clear

Comment thread src/module.c Resolved
Comment thread src/module.c Resolved
Comment thread src/module.c Resolved
Comment thread src/module.c Resolved
Comment thread src/module.c Resolved
Comment thread src/debug_commands.c Resolved
Comment thread src/coord/rmr/rmr.c Resolved
Comment thread src/coord/rmr/rmr.c Resolved
Comment thread src/coord/rmr/rmr.c Resolved
Comment thread src/coord/rmr/rmr.c Outdated

// Check if timed out - discard reply and return early.
// Timeout callback handles the reply, no need to track completion here.
if (MRCtx_IsTimedOut(ctx)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When do we free the MRCtx? How do we access it after a timeout?

@lerman25 lerman25 Feb 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Timeout doesn't free MRCtx, only DistSearchFreePrivData callback does that, it happens after the background thread calls unblock client (whether timeout happened or not, if timeout happened, the unblock client is a no-op)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ahh right. And we call the unblock API only when all the chunks have been returned?
There may still be some issue here, as you return from this function without increasing the error replies count and calling the reducer (which is where we eventually unblock the client, even if a timeout has occurred)

Comment thread src/coord/rmr/rmr.c
// already set, it stays set; if not set, we set it but that's fine since
// we only call this after SetTimedOut or to check if someone else set it.
return atomic_flag_test_and_set(&ctx->timedOut);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

MRCtx_IsTimedOut incorrectly sets flag on read

High Severity

MRCtx_IsTimedOut uses atomic_flag_test_and_set which always sets the flag while returning the previous value. When fanoutCallback calls this function to check if timeout occurred, the first call sets the flag even though no timeout happened. All subsequent calls then return true, causing valid shard replies to be incorrectly discarded. This results in missing search results under normal operation.

Additional Locations (1)

Fix in Cursor Fix in Web

Comment thread src/coord/rmr/rmr.c Resolved

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Comment thread src/coord/rmr/rmr.c
// If we've received the last reply - unblock the client
if (ctx->numReplied + ctx->numErrored == ctx->numExpected) {
if (ctx->fn) {
if (!timedOut && ctx->fn) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use-after-free race in fanoutCallback after timeout

High Severity

After the timeout callback (DistSearchTimeoutPartialClient) completes on the main thread, DistSearchFreePrivData frees the MRCtx. However, the IO thread's fanoutCallback may still receive pending shard replies and attempt to access the freed MRCtx via the privdata pointer. This includes calling MRCtx_IsTimedOut(ctx), incrementing ctx->numErrored, and accessing ctx->bc. This is a use-after-free race condition that can cause crashes or memory corruption.

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@lerman25
lerman25 requested a review from GuyAv46 February 4, 2026 16:07
Comment thread src/coord/rmr/rmr.c
Comment on lines 199 to 202
bool MRCtx_TryClaimReducing(struct MRCtx *ctx) {
bool expected = false;
return atomic_compare_exchange_strong(&ctx->reducing, &expected, true);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider using atomic_flag for this logic, as it feels appropriate

Comment thread src/module.c Resolved
@lerman25
lerman25 added this pull request to the merge queue Feb 4, 2026
Merged via the queue into master with commit 34e1b02 Feb 4, 2026
81 checks passed
@lerman25
lerman25 deleted the omerl-search-coord-timeout-return branch February 4, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants