unum-cloud / usearch

Fast Open-Source Search & Clustering engine × for Vectors & 🔜 Strings × in C++, C, Python, JavaScript, Rust, Java, Objective-C, Swift, C#, GoLang, and Wolfram 🔍
https://unum-cloud.github.io/usearch/
Apache License 2.0
2.15k stars 130 forks source link

Bug: cannot catch Runtime exception when index fails to load #256

Closed spicymatt closed 1 year ago

spicymatt commented 1 year ago

Describe the bug

We are using USearch from swift When a saved index cannot be loaded because, say, the file has been corrupted during save(), the lib crashes. We get a runtime exception that cannot be caught.

My question is as follows (sorry for being newbie in c++):

There is a mechanism in Swift to ensure that C exception can be handled via ObjC bridging...requires some easy code...but since these methods will call std:terminate when there is an error in loading the index, it won't help

Steps to reproduce

create an index and corrupt the file on disk try to load it it crashes the runtime

Expected behavior

the runtime should not crash exception should be sent to the caller of the library

USearch version

1.3.0

Operating System

macOS

Hardware architecture

Arm

Which interface are you using?

C++ implementation

Contact Details

mk@cyme.io

Is there an existing issue for this?

Code of Conduct

ashvardanian commented 1 year ago

Hey, @spicymatt! Thank you for reaching out! I wasn't aware of anyone already using the Swift embeddings! Cool!


As for your question, fread is a pure C function. They have no exceptions. The ABI does not support it. I would expect the following to happen in your case:

  1. Fail in fread is wrapped into a serialization_result_t and exported out.
  2. The load_from_stream propagates it up, say from here.
  3. Obj-C layer catches negative result, unwraps the error, and pushes an Obj-C exception up.

That being said, I can try replicating your setup. Will come back soon. Feel free to provide any more details 🤗

spicymatt commented 1 year ago

Hi, Thanks for getting back. I don't think that the is what happens...unless I am mistaken. See the attached stack trace. std:terminate is called.

Screenshot 2023-09-06 at 23 13 50

We are using USearch to replace FAISS on mobile (iOS). Thanks for your amazing work....we see a number of areas where we would want to see improvements (and maybe we can help)...especially in the addition of vectors (Faiss has a MT addition method to the cluster which is very speedy).

ashvardanian commented 1 year ago

I have replicated the error, @spicymatt. Thank you for sharing the details! Still thinking of the ways to solve it :)

ashvardanian commented 1 year ago

@spicymatt, I think I've found that nasty bug. You should be able to try it in minutes. Please let me know if it worked :)