Skip to content

Fix Cursor Purge - [MOD-9408]#6005

Merged
GuyAv46 merged 5 commits intomasterfrom
guyav-fix_cursor_api
Apr 28, 2025
Merged

Fix Cursor Purge - [MOD-9408]#6005
GuyAv46 merged 5 commits intomasterfrom
guyav-fix_cursor_api

Conversation

@GuyAv46
Copy link
Collaborator

@GuyAv46 GuyAv46 commented Apr 23, 2025

Describe the changes in the pull request

Fixing the cursors API so one can safely call FT.CURSOR DEL on some cursor currently being read by another connection on another thread.

Instead of attempting to delete the requested cursor, if we see that it is currently being used, we mark it for deletion. When the using thread finishes with the cursor, it will delete the cursor even if it was not depleted.

The issue can only happen using WORKERS or cluster mode (Coordinator).

Which additional issues does this PR fix

  1. MOD-9432
  2. MOD-9433
  3. MOD-9434
  4. MOD-9435

Main objects this PR modified

  1. Cursor API internals

Mark if applicable

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

@codecov
Copy link

codecov bot commented Apr 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.44%. Comparing base (fe7ab04) to head (557b1ee).
Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6005      +/-   ##
==========================================
+ Coverage   87.24%   87.44%   +0.20%     
==========================================
  Files         197      217      +20     
  Lines       36012    39367    +3355     
  Branches        0     2634    +2634     
==========================================
+ Hits        31420    34426    +3006     
- Misses       4592     4932     +340     
- Partials        0        9       +9     

☔ 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.

@GuyAv46 GuyAv46 marked this pull request as ready for review April 24, 2025 06:48
@GuyAv46 GuyAv46 requested a review from Copilot April 24, 2025 07:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the cursor purge behavior by marking in-use cursors for deletion instead of deleting them immediately, ensuring safe concurrent cursor operations. Key changes include:

  • Updating the cursor API to mark non-idle cursors for deletion.
  • Adding new tests for both basic and ownership scenarios in the cursor API.
  • Refactoring the cursor deletion and pause logic in the source code for clearer behavior and C++ compatibility.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
tests/cpptests/test_cpp_cursors.cpp Added tests to validate basic API behavior and ownership semantics.
src/cursor.h Added C++ extern block and improved code comments regarding lock usage.
src/cursor.c Refactored deletion/pause logic to support marking cursors for deletion safely.
Comments suppressed due to low confidence (1)

src/cursor.c:286

  • The new assignment to cur->pos omits the '- 1' adjustment present in the previous version, which may result in an off-by-one error when indexing into the idle array. Please verify that the ARRAY_ADD_AS macro returns the expected index so that cur->pos correctly references the newly added element.
cur->pos = ARRAY_GETSIZE_AS(&cl->idle, Cursor **);

@GuyAv46 GuyAv46 changed the title Fix Cursor Purge - [MOD-] Fix Cursor Purge - [MOD-9408] Apr 24, 2025
@GuyAv46 GuyAv46 requested a review from lerman25 April 27, 2025 06:17
@alonre24 alonre24 removed the request for review from lerman25 April 27, 2025 11:07
Copy link
Collaborator

@alonre24 alonre24 left a comment

Choose a reason for hiding this comment

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

Good catch and a clean fix
Let's add a short stress test in this opportunity

}

int Cursor_Free(Cursor *cur) {
return Cursors_Purge(getCursorList(cur->is_coord), cur->id);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we unify Cursors_Purge and Cursor_Free? Actually, neither of them is really freeing the memory, right? Better name would be Cursor_Delete IMO

Copy link
Collaborator Author

@GuyAv46 GuyAv46 Apr 27, 2025

Choose a reason for hiding this comment

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

I didn't want to rename the function, so I don't have to change it in all the files that use it. It's not that bad in my opinion.

Cursors_Purge and Cursor_Free are NOT the same thing.

  • Cursors_Purge is a request to delete a cursor, freeing it if it exists and is idle.
  • Cursors_Free is for releasing an owned cursor. It always freeing memory, and cannot fail.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request fixes the cursor purge logic to safely handle deletion of cursors that are actively used by other threads by marking them for deletion rather than deleting immediately.

  • Updated the cursor deletion behavior in both pause and purge functions.
  • Enhanced header documentation and added tests to verify the new behavior.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
tests/cpptests/test_cpp_cursors.cpp Adds test cases to validate basic API calls and deletion-mark logic.
src/cursor.h Updates the API documentation and adds the delete_mark flag to Cursor.
src/cursor.c Modifies the free, pause, and purge functions to support deferred deletion.
Comments suppressed due to low confidence (1)

src/cursor.c:260

  • The previous implementation subtracted one from the idle array size to assign the cursor's position. Please verify that assigning cur->pos to the full size (without subtracting one) is intentional, so that the index stored correctly reflects the cursor's actual position in the idle list.
cur->pos = ARRAY_GETSIZE_AS(&cl->idle, Cursor **);

@GuyAv46 GuyAv46 added this pull request to the merge queue Apr 28, 2025
Merged via the queue into master with commit 4f6000a Apr 28, 2025
11 checks passed
@GuyAv46 GuyAv46 deleted the guyav-fix_cursor_api branch April 28, 2025 12:33
@redisearch-backport-pull-request
Copy link
Contributor

Backport failed for 2.8, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin 2.8
git worktree add -d .worktree/backport-6005-to-2.8 origin/2.8
cd .worktree/backport-6005-to-2.8
git switch --create backport-6005-to-2.8
git cherry-pick -x 4f6000a4311babfd6e2cb720143fa0103cd1ef88

@redisearch-backport-pull-request
Copy link
Contributor

Backport failed for 2.6, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin 2.6
git worktree add -d .worktree/backport-6005-to-2.6 origin/2.6
cd .worktree/backport-6005-to-2.6
git switch --create backport-6005-to-2.6
git cherry-pick -x 4f6000a4311babfd6e2cb720143fa0103cd1ef88

redisearch-backport-pull-request bot pushed a commit that referenced this pull request Apr 28, 2025
* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff

(cherry picked from commit 4f6000a)
@redisearch-backport-pull-request
Copy link
Contributor

Successfully created backport PR for 2.10:

redisearch-backport-pull-request bot pushed a commit that referenced this pull request Apr 28, 2025
* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff

(cherry picked from commit 4f6000a)
@redisearch-backport-pull-request
Copy link
Contributor

Successfully created backport PR for 8.0:

GuyAv46 added a commit that referenced this pull request Apr 28, 2025
* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff

(cherry picked from commit 4f6000a)
GuyAv46 added a commit that referenced this pull request Apr 28, 2025
* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff

(cherry picked from commit 4f6000a)
github-merge-queue bot pushed a commit that referenced this pull request Apr 28, 2025
Fix Cursor Purge - [MOD-9408] (#6005)

* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff

(cherry picked from commit 4f6000a)

Co-authored-by: GuyAv46 <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Apr 29, 2025
* Fix Cursor Purge - [MOD-9408] (#6005)

* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff

(cherry picked from commit 4f6000a)

* fix test for 2.10

---------

Co-authored-by: GuyAv46 <[email protected]>
Co-authored-by: GuyAv46 <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Apr 29, 2025
* Fix Cursor Purge - [MOD-9408] (#6005)

* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff

(cherry picked from commit 4f6000a)

* fix test and build for 2.6
github-merge-queue bot pushed a commit that referenced this pull request Apr 29, 2025
* Fix Cursor Purge - [MOD-9408] (#6005)

* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff

(cherry picked from commit 4f6000a)

* fix test for 2.8
nafraf pushed a commit that referenced this pull request May 13, 2025
* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff
JoanFM pushed a commit that referenced this pull request May 27, 2025
* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff
JoanFM pushed a commit that referenced this pull request May 27, 2025
* fix cursor purging mechanism

* add new unit test for cursors

* minor improvement

* address CR

* reduce diff
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.

4 participants