Skip to content

Conversation

@BagritsevichStepan
Copy link
Contributor

@BagritsevichStepan BagritsevichStepan commented Jun 16, 2025

fixes #5215

The problem was that the StringFamily::Set command could change the key's type. If the previous type was HSET or JSON, the key was indexed. When StringFamily::Set is called on such a key, it changes the type from HSET/JSON to STRING, but we forgot to remove the key from the indexes.

This PR adds a call to RemoveDoc inside StringFamily::Set to properly clean up the old index entry.

A snippet that reproduces the issue has been added to the tests, along with explanatory comments:

127.0.0.1:6379> ft.create index on hash schema field TEXT
OK
127.0.0.1:6379> hset k1 field value
(integer) 1
127.0.0.1:6379> set k1 anothervalue
OK
127.0.0.1:6379> rename k1 anotherkey
OK
127.0.0.1:6379> hset k1 field value
Error: Server closed the connection
(1.29s)

fixes dragonflydb#5215

Signed-off-by: Stepan Bagritsevich <stefan@dragonflydb.io>
@vyavdoshenko
Copy link
Contributor

@BagritsevichStepan
Could you please add a more descriptive description of the root cause and how it was fixed?

@BagritsevichStepan
Copy link
Contributor Author

BagritsevichStepan commented Jun 16, 2025

@BagritsevichStepan Could you please add a more descriptive description of the root cause and how it was fixed?

Done

Copy link
Contributor

@vyavdoshenko vyavdoshenko left a comment

Choose a reason for hiding this comment

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

LGTM

@romange
Could you please take a look at this PR as well?

I see the potential issue. In my opinion, this issue can be bigger than this specific issue. We have to track the search index in all possible places, briefly speaking, all mutable operations. I am afraid that the same issue can cause the same or similar crash during the change of a different pair of types.
Personally, I don't know how to generalize the approach and avoid similar issues in the future. Maybe I am paranoid, correct me if I am wrong.

@BagritsevichStepan
Copy link
Contributor Author

@vyavdoshenko I spoke about it today and we already have an issue for this #5014 . I have a solution

@BagritsevichStepan BagritsevichStepan removed the request for review from BorysTheDev June 16, 2025 10:30
db_slice.SetMCFlag(op_args_.db_cntx.db_index, it->first.AsRef(), params.memcache_flags);

// We need to remove the key from search indices, because we are overwriting it to OBJ_STRING
shard->search_indices()->RemoveDoc(key, db_cntx, prime_value);
Copy link
Collaborator

Choose a reason for hiding this comment

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

i do not like this unconditional approach. please check before if prime_value.ObjType() is indexable (i.e. either hset or json). You can introduce a one-liner utility function for better readability (add it to family_utils.h).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know, we have this problem in several places (check for example rename command). Let's do it in another PR, I will fix other commands as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok, please do not forget

@romange
Copy link
Collaborator

romange commented Jun 16, 2025

most of our write commands are not blind writes so the issue is not wide to begin with.

@BagritsevichStepan
Copy link
Contributor Author

I know for example zunionstore command, that also changes the key type

@BagritsevichStepan
Copy link
Contributor Author

And there is the same bug in the OpAdd in set_family.cc if overwrite is true

Signed-off-by: Stepan Bagritsevich <stefan@dragonflydb.io>
@BagritsevichStepan BagritsevichStepan enabled auto-merge (squash) June 16, 2025 12:29
@BagritsevichStepan BagritsevichStepan merged commit aee7c78 into dragonflydb:main Jun 16, 2025
10 checks passed
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.

Search index is not flushed during SET command

3 participants