Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FATAL ERROR: Error::New napi_get_last_error_info #132

Closed
nrathi opened this issue Oct 13, 2023 · 3 comments
Closed

FATAL ERROR: Error::New napi_get_last_error_info #132

nrathi opened this issue Oct 13, 2023 · 3 comments

Comments

@nrathi
Copy link

nrathi commented Oct 13, 2023

Is the HSNW index automatically garbage collected, or is it recommended that we "free" the memory when we're done? I was wondering if something like resizeIndex would work?

I ask because I intermittently get this error:

FATAL ERROR: Error::New napi_get_last_error_info
 1: 0x10253549c node::Abort() [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
 2: 0x102535594 node::OOMErrorHandler(char const*, bool) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
 3: 0x1025354b4 node::OnFatalError(char const*, char const*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
 4: 0x102504678 napi_open_callback_scope [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
 5: 0x10f959cb8 Napi::Error::Error(napi_env__*, napi_value__*) [/Users/neha/my_app/node_modules/.pnpm/hnswlib-node@2.0.0/node_modules/hnswlib-node/build/Release/addon.node]
 6: 0x10f95950c Napi::Error::New(napi_env__*) [/Users/neha/my_app/node_modules/.pnpm/hnswlib-node@2.0.0/node_modules/hnswlib-node/build/Release/addon.node]
 7: 0x10f969108 Napi::Value::ToBoolean() const [/Users/neha/my_app/node_modules/.pnpm/hnswlib-node@2.0.0/node_modules/hnswlib-node/build/Release/addon.node]
 8: 0x10f969094 CustomFilterFunctor::operator()(unsigned long) [/Users/neha/my_app/node_modules/.pnpm/hnswlib-node@2.0.0/node_modules/hnswlib-node/build/Release/addon.node]
 9: 0x10f976e98 std::__1::priority_queue<std::__1::pair<float, unsigned int>, std::__1::vector<std::__1::pair<float, unsigned int>, std::__1::allocator<std::__1::pair<float, unsigned int> > >, hnswlib::HierarchicalNSW<float>::CompareByFirst> hnswlib::HierarchicalNSW<float>::searchBaseLayerST<false, true>(unsigned int, void const*, unsigned long, hnswlib::BaseFilterFunctor*) const [/Users/neha/my_app/node_modules/.pnpm/hnswlib-node@2.0.0/node_modules/hnswlib-node/build/Release/addon.node]
10: 0x10f97077c hnswlib::HierarchicalNSW<float>::searchKnn(void const*, unsigned long, hnswlib::BaseFilterFunctor*) const [/Users/neha/my_app/node_modules/.pnpm/hnswlib-node@2.0.0/node_modules/hnswlib-node/build/Release/addon.node]
11: 0x10f96d554 HierarchicalNSW::searchKnn(Napi::CallbackInfo const&) [/Users/neha/my_app/node_modules/.pnpm/hnswlib-node@2.0.0/node_modules/hnswlib-node/build/Release/addon.node]
12: 0x10f96fafc Napi::InstanceWrap<HierarchicalNSW>::InstanceMethodCallbackWrapper(napi_env__*, napi_callback_info__*)::'lambda'()::operator()() const [/Users/neha/my_app/node_modules/.pnpm/hnswlib-node@2.0.0/node_modules/hnswlib-node/build/Release/addon.node]
13: 0x10f96f8a8 Napi::InstanceWrap<HierarchicalNSW>::InstanceMethodCallbackWrapper(napi_env__*, napi_callback_info__*) [/Users/neha/my_app/node_modules/.pnpm/hnswlib-node@2.0.0/node_modules/hnswlib-node/build/Release/addon.node]
14: 0x1024f6990 v8impl::(anonymous namespace)::FunctionCallbackWrapper::Invoke(v8::FunctionCallbackInfo<v8::Value> const&) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
15: 0x1026feb8c v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
16: 0x1026fe688 v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
17: 0x1026fdeb4 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
18: 0x102eed18c Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_BuiltinExit [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
19: 0x102e78198 Builtins_InterpreterEntryTrampoline [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
20: 0x102eac0d0 Builtins_GeneratorPrototypeNext [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
21: 0xf3b6af7f4 
22: 0x102f36b88 Builtins_PromiseConstructor [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
23: 0x102e75914 Builtins_JSBuiltinsConstructStub [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
24: 0xf3b6afb30 
25: 0x102e78198 Builtins_InterpreterEntryTrampoline [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
26: 0x102e78198 Builtins_InterpreterEntryTrampoline [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
27: 0x102eac0d0 Builtins_GeneratorPrototypeNext [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
28: 0xf3b6af7f4 
29: 0x102f36b88 Builtins_PromiseConstructor [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
30: 0x102e75914 Builtins_JSBuiltinsConstructStub [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
31: 0xf3b6afb30 
32: 0x102e78198 Builtins_InterpreterEntryTrampoline [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
33: 0x102e78198 Builtins_InterpreterEntryTrampoline [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
34: 0x102eac0d0 Builtins_GeneratorPrototypeNext [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
35: 0xf3b71df38 
36: 0x102f38738 Builtins_PromiseFulfillReactionJob [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
37: 0x102e9bc4c Builtins_RunMicrotasks [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
38: 0x102e763a4 Builtins_JSRunMicrotasksEntry [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
39: 0x1027ba99c v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
40: 0x1027bae8c v8::internal::(anonymous namespace)::InvokeWithTryCatch(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
41: 0x1027bb068 v8::internal::Execution::TryRunMicrotasks(v8::internal::Isolate*, v8::internal::MicrotaskQueue*, v8::internal::MaybeHandle<v8::internal::Object>*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
42: 0x1027e17b4 v8::internal::MicrotaskQueue::RunMicrotasks(v8::internal::Isolate*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
43: 0x1027e204c v8::internal::MicrotaskQueue::PerformCheckpoint(v8::Isolate*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
44: 0x102e79a34 Builtins_CallApiCallback [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
45: 0xf3b75534c 
46: 0x102e764d0 Builtins_JSEntryTrampoline [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
47: 0x102e76164 Builtins_JSEntry [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
48: 0x1027ba9cc v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
49: 0x1027b9f00 v8::internal::Execution::Call(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, int, v8::internal::Handle<v8::internal::Object>*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
50: 0x1026aa294 v8::Function::Call(v8::Local<v8::Context>, v8::Local<v8::Value>, int, v8::Local<v8::Value>*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
51: 0x102484d18 node::InternalCallbackScope::Close() [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
52: 0x102484fe8 node::InternalMakeCallback(node::Environment*, v8::Local<v8::Object>, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*, node::async_context) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
53: 0x102499c14 node::AsyncWrap::MakeCallback(v8::Local<v8::Function>, int, v8::Local<v8::Value>*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
54: 0x1025eb77c node::StreamBase::CallJSOnreadMethod(long, v8::Local<v8::ArrayBuffer>, unsigned long, node::StreamBase::StreamBaseJSChecks) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
55: 0x1025ece34 node::EmitToJSStreamListener::OnStreamRead(long, uv_buf_t const&) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
56: 0x10265d760 node::crypto::TLSWrap::ClearOut() [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
57: 0x10265f45c node::crypto::TLSWrap::OnStreamRead(long, uv_buf_t const&) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
58: 0x1025f0e10 node::LibuvStreamWrap::OnUvRead(long, uv_buf_t const*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
59: 0x1025f15ac node::LibuvStreamWrap::ReadStart()::$_1::__invoke(uv_stream_s*, long, uv_buf_t const*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
60: 0x102e61fd8 uv__stream_io [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
61: 0x102e6a0f0 uv__io_poll [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
62: 0x102e57e1c uv_run [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
63: 0x102485704 node::SpinEventLoop(node::Environment*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
64: 0x1025d89ec node::worker::Worker::Run() [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
65: 0x1025db7a4 node::worker::Worker::StartThread(v8::FunctionCallbackInfo<v8::Value> const&)::$_3::__invoke(void*) [/Users/neha/.nvm/versions/node/v18.15.0/bin/node]
66: 0x19891e06c _pthread_start [/usr/lib/system/libsystem_pthread.dylib]
67: 0x198918e2c thread_start [/usr/lib/system/libsystem_pthread.dylib]

It's worth noting that I use the filterFunction argument quite frequently to see if the index is in some pre-defined set of indices.

Thanks!

@yoshoku
Copy link
Owner

yoshoku commented Oct 15, 2023

This error seems to occur if a variable inside the filter function was deleted. I reproduced this error with the code below:

const hnswlib = require('hnswlib-node');

const index = new hnswlib.HierarchicalNSW('l2', 3);
index.initIndex({ maxElements: 4 });

index.addPoint([1, 2, 3], 1)
index.addPoint([4, 5, 6], 2)
index.addPoint([1, 2, 3], 3)
index.addPoint([7, 8, 9], 4)

arr = [1, 2]

const filterFnc = (label) => arr.includes(label);

delete arr;

console.table(index.searchKnn([1, 2, 3], 2, filterFnc));
$ node issue_132.js
FATAL ERROR: Error::New napi_get_last_error_info
 1: 0x104dd69f8 node::Abort() [/Users/foo/.anyenv/envs/nodenv/versions/20.3.1/bin/node]
 2: 0x104dd6ae4 node::OOMErrorHandler(char const*, v8::OOMDetails const&) [/Users/foo/.anyenv/envs/nodenv/versions/20.3.1/bin/node]
 3: 0x104da5af4 napi_open_callback_scope [/Users/foo/.anyenv/envs/nodenv/versions/20.3.1/bin/node]
 4: 0x11a71dd98 Napi::Error::Error(napi_env__*, napi_value__*) [/Users/foo/test_hnswlib-node/node_modules/hnswlib-node/build/Release/addon.node]
 5: 0x11a71d5ec Napi::Error::New(napi_env__*) [/Users/foo/test_hnswlib-node/node_modules/hnswlib-node/build/Release/addon.node]
 6: 0x11a72d140 Napi::Value::ToBoolean() const [/Users/foo/test_hnswlib-node/node_modules/hnswlib-node/build/Release/addon.node]
 7: 0x11a72d0cc CustomFilterFunctor::operator()(unsigned long) [/Users/foo/test_hnswlib-node/node_modules/hnswlib-node/build/Release/addon.node]
...

It is recommended to check if any variables in the filter function have been freed.

@nrathi
Copy link
Author

nrathi commented Oct 15, 2023

Thanks for that repro!

I definitely don't delete any variables directly, I let everything get garbage collected. Furthermore, I experience the issue as soon as I restart my development server (after hours and hours of previous starts/stops). The issue persists for some time (maybe on the order of minutes?), and then resolves itself.

@nrathi nrathi changed the title Freeing memory FATAL ERROR: Error::New napi_get_last_error_info Nov 9, 2023
@nrathi
Copy link
Author

nrathi commented Nov 10, 2023

Update: I figured out the problem, and it was exactly along the lines of what you said.

I was creating a Dictionary of Sets

const availableIndices = mapValues(groupBy(array, item => item.group), item => item.index)

Then, I was iterating through some other items and trying to use this in the filter function:

for (const item of items) {
  const searchResult = hsnwIndex.searchKnn(
    embedding,
    NUM_NEIGHBORS,
    (index) => availableIndices[item.group].has(index),
  );
}

But if the group hadn't been seen before, then this would throw the error because availableIndices[item.group] is undefined.

Thanks for your help.

@nrathi nrathi closed this as completed Nov 10, 2023
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

No branches or pull requests

2 participants