3

I'm working on request tracing in my node.js project (monorepo) and found out something called "cls-hooked" which uses async hooks. So I simply wrapped cls-hooked functionalities into adapter that I want to put into a shared library that I use in each micro service. The problem is while starting such service then:

node[35521]: ../src/async_wrap.cc:307:void node::SetupHooks(const FunctionCallbackInfo<v8::Value> &): Assertion `env->async_hooks_init_function().IsEmpty()' failed.
 1: 0x100b9f32f node::Abort() (.cold.1) [/usr/local/opt/node@12/bin/node]
 2: 0x100080fc5 node::FatalError(char const*, char const*) [/usr/local/opt/node@12/bin/node]
 3: 0x100080e5a node::AppendExceptionLine(node::Environment*, v8::Local<v8::Value>, v8::Local<v8::Message>, node::ErrorHandlingMode) [/usr/local/opt/node@12/bin/node]
 4: 0x100013952 node::SetupHooks(v8::FunctionCallbackInfo<v8::Value> const&) [/usr/local/opt/node@12/bin/node]
 5: 0x1001d1496 v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) [/usr/local/opt/node@12/bin/node]
 6: 0x1001d0b3a 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) [/usr/local/opt/node@12/bin/node]
 7: 0x1001d0370 v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) [/usr/local/opt/node@12/bin/node]
 8: 0x100749039 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_BuiltinExit [/usr/local/opt/node@12/bin/node]

when i put the adapter in each service's code - it works like a charm but i don't want to duplicate such code in every service. Can someone tell me is this error related to async hooks? Is it even possible to use async hooks functionalities, or cls-hooked functionalities itself from a shared library that is loaded from node_modules?

Farys
  • 33
  • 2
  • hit the same roadblock. did you find a fix for this? or an alternative? – Zameer Fouzan May 25 '21 at 12:04
  • In the end I added it as an external dependency to our shared lib, we use rollup as a bundler and u can achieved it by adding `external: [...builtins, 'cls-hooked']` in rollup config. Did u consider using AsyncLocalStorage core module (which seems to perform better) instead: https://nodejs.org/api/async_hooks.html#async_hooks_class_asynclocalstorage Some articles: https://itnext.io/one-node-js-cls-api-to-rule-them-all-1670ac66a9e8 https://medium.com/trabe/asynclocalstorage-for-easy-context-passing-in-node-js-e33c84679516 – Farys May 26 '21 at 15:32

0 Answers0