Skip to content

[C API] Add cancellation callback to search parameters - #484

Open
yuejiaointel wants to merge 5 commits into
dev/yuejiao/vamana-cancel-partial-resultsfrom
dev/yuejiao/c-api-cancel-callback
Open

yuejiaointel wants to merge 5 commits into
dev/yuejiao/vamana-cancel-partial-resultsfrom
dev/yuejiao/c-api-cancel-callback

Conversation

@yuejiaointel

@yuejiaointel yuejiaointel commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #400.

Stacked on #483, which makes a cancelled Vamana search return the results found so far. The diff here contains only the C API change; the base will move to main once #483 merges.

What

  • svs_search_params_set_cancel_callback(params, fn, userdata, out_err) sets an svs_is_cancelled_fn callback on the search parameters (NULL removes it).
  • New error code SVS_ERROR_CANCELLED = 9.
  • svs_index_search_topk() polls the callback once at call start, once per visited node, and per query and per batch for filtered search. The callback may be called concurrently from the search threads.
  • If the callback already returns true when the call starts, nothing is searched: 0 results and SVS_ERROR_CANCELLED.
  • When the callback returns true, the call returns false with SVS_ERROR_CANCELLED, and out_results holds, per query, the neighbors found so far (possibly fewer than k; queries that had not started when the cancel fired may hold a few candidates). Invalid-neighbor markers left by the core are dropped before the results are exported.
  • Filtered search keeps the hits already accepted when cancelled.
  • Without a callback the core's default predicate is used, so behavior and results are unchanged.

Tests

New test case C API Search Cancel Callback ([cancel]) in bindings/c/tests/c_api_index.cpp, using a callback that fires after N polls, on a static index (also filtered) and on a dynamic index with deletions and non-contiguous external IDs:

  • cancel before start: SVS_ERROR_CANCELLED, 0 results
  • cancel mid-search: SVS_ERROR_CANCELLED, some results, only live external IDs, no invalid-neighbor marker
  • never-firing callback and NULL callback: identical to a search without a callback

Argument checks for the setter are in c_api_search_params.cpp. The test uses random data because generate_test_data() produces duplicate vectors that starve filtered search.

Before merging

  • Needs Return partial results when a Vamana search is cancelled #483 merged, plus the matching LVQ/LeanVec change in a released SVS build, and SVS_URL bumped to that build. Until then the "C API (with static library)" job builds against v0.5.0, which does not return partial results on cancel.
  • The "C API (with static library)" job is currently red at compile time in files this PR does not touch (bindings/c/src/dispatcher_dynamic_vamana_copy.hpp:94: VamanaStateLoader is not a template; dispatcher_dynamic_vamana.cpp:92: no matching DynamicVamana::assemble overload), so it is not caused by this PR. The partial-results behaviour there can only be checked after the SVS_URL bump.
  • The overhead benchmark from [C API] Add cancellation-poll callback to svs_search_params #400 (null callback vs baseline, < 1% p50 at k=10) has not been run yet.

🤖 Generated with Claude Code

Add svs_search_params_set_cancel_callback() and SVS_ERROR_CANCELLED. The
callback is polled during svs_index_search_topk() (per visited node, before
each query, and per filtered batch). When it returns true the search stops,
returns false with SVS_ERROR_CANCELLED, and out_results holds the neighbors
found so far for each query. Without a callback, behavior is unchanged.
…l-results' into yuejiao/c-api-cancel-callback-v2
@yuejiaointel
yuejiaointel marked this pull request as ready for review November 25, 2026 23:17

This branch has not been deployed

No deployments
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