Skip to content

Bug: Deadlock in concurrent update()s #396

Closed as not planned
Closed as not planned

Description

@chun0nick

Describe the bug

Deadlock came up during testing, appears to be an issue in index.hpp. We're using the Rust bindings for v2.8.16 so through index_dense.hpp. This testing involved deleting a bunch of vectors and then, with multiple threads, calling add(), which then called update() on the deleted nodes.

In update we acquire the lock on the deleted node here. We then call connect_node_across_levels_ which invokes search_for_one_ and search_to_insert_, while locking and locking (respectively) every candidate node it looks at.

This update lock on the deleted node seems to be the problem. If another thread performing an update selects that deleted node as a "next" candidate, we have to wait until the other thread is completely done updating, as we don't drop the update lock until the end of the call. What we've seen somewhat consistently (with 16 threads updating) is a deadlock, where each of the threads needs the candidate lock that another thread updating already holds, and that thread in turn needs the candidate lock a different thread holds.

This may be the issue described in #354?

Steps to reproduce

Create an index, add a reasonable number of vectors. Delete them - then concurrently call add (update) with a bunch of threads.

Expected behavior

The concurrent updates should not conflict with each other.

USearch version

v2.8.16

Operating System

Amazon Linux 2

Hardware architecture

x86

Which interface are you using?

Other bindings

Contact Details

[email protected]

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    bug
    on Apr 12, 2024
  2. ashvardanian commented on Apr 12, 2024

    @ashvardanian
    Member

    Good catch! Looking into it! Feel free to submit a test-case or patches via PR while I do 🤗

  3. ashvardanian commented on Apr 15, 2024

    @ashvardanian
    Member

    Replicated it, will make part of the primary testing suite in C++. Thanks for finding and reporting the issue, @chun0nick! Would you be open to be mentioned as a contributor for this patch? 🤗

  4. chun0nick commented on Apr 15, 2024

    @chun0nick
    Author

    Awesomely fast response! And sorry - was planning to give you a test case today. Sure, would be happy to be mentioned as a contributor.

  5. sef43 commented on Apr 23, 2024

    @sef43

    I had what looked to be a deadlock happen when using the Python api (as part of usearch_molecules indexing step) when using 32 threads. Could this bug be the cause of that?

  6. ashvardanian commented on Apr 23, 2024

    @ashvardanian
    Member

    Yes, @sef43, the issue is coming from the core in update-heavy workloads. Haven't finished patching yet, was busy the last few days with new bindings for UForm and new algos in StringZilla.

  7. chun0nick commented on Jul 8, 2024

    @chun0nick
    Author

    Hi, was curious if there was movement on this issue - I've taken a few cracks at a fix but have quickly found myself touching quite a bit of code that I wasn't super confident with. Thanks!

  8. ashvardanian commented on Jul 8, 2024

    @ashvardanian
    Member

    I've spent some time on this recently as well. Concurrent updates are quite tricky, especially if you want to avoid inefficient memory usage. I found no clean way of keeping a 2-layer solution, separating the logic between index_gt and index_dense_gt.

    A better solution might be to have a more advanced compare-exchange-like operation in index_gt.

    After my weekend patches, every other core C++ test on the main-dev branch passes, except for these concurrent updates and weird SEGFAULT in Python bindings, so its a core priority 🤗

  9. vitarb commented on Feb 13, 2025

    @vitarb

    @ashvardanian it's been a while, I wonder if it fell out of your radar, were you able to get this fixed? Is there any branch that contains partial fixes?

  10. ashvardanian commented on Feb 14, 2025

    @ashvardanian
    Member

    Hi @vitarb! I would heavily discourage concurrent removals and updates in the current v2. I'd love to learn more about your usecase and if you'd like to be participating in the v3 preview. We can continue over email as well - [email protected] 🤗

  11. ashvardanian commented on Nov 14, 2026

    @ashvardanian
    Member

    @chun0nick, thanks for the report, and @sef43 and @vitarb for following up! 3.0 drops the v2 update() along with its locking, and removal returns in #846 as an exclusive call that never races with inserts or searches, so replacing an entry becomes a removal followed by an insert. I'm closing this in its favor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions