Repository navigation
(Typed)ThreadSafeFunction.Release() crashes renderer process on reload (& abort?) #1109
Description
We are using a native addon with Electron (Both v14 and v16 show the same behaviour)
There is a class, which roughly looks like this:
class Foo : public Napi::ObjectWrap<Foo> {
public:
Foo(
const Napi::CallbackInfo& info
):
Napi::ObjectWrap<Foo>(info)
{
tsfn1_ = ...
tsfn2_ = ...
}
~Foo() {
tsfn1_.Release();
tsfn2_.Release();
...
}
private:
TSFNT1 tsfn1_;
TSFNT2 tsfn2_;
...
}
Everything works fine - Except when Electron reloads.
There are some situation when we need to reload the Electron application via getCurrentWebContents().reloadIgnoringCache() - Which fails spectacularly with a renderer crash.
This is an issue both for our users AND when in development when using hot reload.
I have tracked down the issue to the .Release() calls
If I remove the .Release() calls, everything seems to work fine, including reload.
I have tried to debug the issue further with a debug build of Electron, but at least v14 (have not tested v16) triggers another assertion when reloading which can't be fixed (I stripped the addon to not contain any exports and just the register call and it still caused a renderer crash on reload with Debug Electron)
My theory: Node destroys the TSFNs on Node's side before it frees / destroys the objects of the addon. I can't say whether this is just a wild theory or a real explanation, but I think a couple of Node devs here could know?
Our fix for now: Don't free the TSNFs in the destructor. The Foo object is long-living and usually only created once and destroyed once in the lifecycle of the addon.
FWIW, that's the stacktrace:
thread #1, name = 'electron', stop reason = signal SIGABRT
* frame #0: 0x00007f70aad7218b libc.so.6`__GI_raise(sig=<unavailable>) at raise.c:51:1
frame #1: 0x00007f70aad51859 libc.so.6`__GI_abort at abort.c:79:7
frame #2: 0x00005565c007d794 electron`uv_mutex_lock + 20
frame #3: 0x00005565c63f7266 electron`napi_release_threadsafe_function + 38
frame #4: 0x00007f709c498c9a addon.node`Napi::TypedThreadSafeFunction<std::nullptr_t, std::variant<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<SecretType, std::default_delete<SecretType> > >, &(Foo::SecretJSGlueFunction(Napi::Env, Napi::Function, std::nullptr_t*, std::variant<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<SecretType, std::default_delete<SecretType> > >*))>::Release(this=0x000013900794e100) at napi-inl.h:5163:10
frame #5: 0x00007f709c4905d8 addon.node`Foo::~Foo(this=0x000013900794e000) at Foo.cc:166:25
frame #6: 0x00007f709c490839 addon.node`Foo::~Foo(this=0x000013900794e000) at Foo.cc:164:48
frame #7: 0x00007f709c4a2632 addon.node`Napi::ObjectWrap<Foo>::FinalizeCallback(env=0x000013900078ae20, data=0x000013900794e000, (null)=0x0000000000000000) at napi-inl.h:4387:3
frame #8: 0x00005565c63f765f electron
frame #9: 0x00005565c63f8adf electron
frame #10: 0x00005565c63f74a6 electron
frame #11: 0x00005565c63f7ea5 electron
frame #12: 0x00005565c63f7f0e electron
frame #13: 0x00005565c63f8218 electron
frame #14: 0x00005565c0072729 electron`uv_run + 521
frame #15: 0x00005565c63d00e1 electron
frame #16: 0x00005565c63d0638 electron
frame #17: 0x00005565c6398264 electron`node::FreeEnvironment(node::Environment*) + 180
frame #18: 0x00005565c01d6698 electron
frame #19: 0x00005565c56662d5 electron
frame #20: 0x00005565c43085a5 electron
frame #21: 0x00005565c43072a6 electron
frame #22: 0x00005565c4684c0f electron
frame #23: 0x00005565c4ba1468 electron
frame #24: 0x00005565c4ba24f4 electron
frame #25: 0x00005565c4bb69e2 electron
frame #26: 0x00005565c4bb97bd electron
frame #27: 0x00005565c470b503 electron
frame #28: 0x00005565c5657761 electron
frame #29: 0x00005565c5671db3 electron
frame #30: 0x00005565c5671bfb electron
frame #31: 0x00005565c56561cc electron
frame #32: 0x00005565c5ae4805 electron
frame #33: 0x00005565c0f493c9 electron
frame #34: 0x00005565c5ae4d2b electron
frame #35: 0x00005565c2fa29e8 electron
frame #36: 0x00005565c2fa771d electron
frame #37: 0x00005565c2fa3f4a electron
frame #38: 0x00005565c31c0b29 electron
frame #39: 0x00005565c2fa505f electron
frame #40: 0x00005565c2c6e2f5 electron
frame #41: 0x00005565c2c84316 electron
frame #42: 0x00005565c2c854af electron
frame #43: 0x00005565c2c3901e electron
frame #44: 0x00005565c2c85914 electron
frame #45: 0x00005565c2c575f2 electron
frame #46: 0x00005565c61e682b electron
frame #47: 0x00005565c104fe53 electron
frame #48: 0x00005565c104d77f electron
frame #49: 0x00005565c104e169 electron
frame #50: 0x00005565c008335c electron
frame #51: 0x00007f70aad530b3 libc.so.6`__libc_start_main(main=0x00005565c0083150, argc=15, argv=0x00007fff22fedd28, init=<unavailable>, fini=<unavailable>, rtld_fini=<unavailable>, stack_end=0x00007fff22fedd18) at libc-start.c:308:16
frame #52: 0x00005565bfd18daa electron`_start + 42
Electron versions: v14 & v16
Compiler: Clang 10+
OS: definitely Linux, but seems to happen on MacOS and Windows
Arch: AMD64, but seems to happen on ARM64 too
Any ideas?
Activity
[-](Typed)ThreadSafeFunction.Release() segfaults renderer process on reload [/-][+](Typed)ThreadSafeFunction.Release() crashes renderer process on reload [/+][-](Typed)ThreadSafeFunction.Release() crashes renderer process on reload [/-][+](Typed)ThreadSafeFunction.Release() crashes renderer process on reload (& abort?)[/+]gabrielschulhof commentedon Dec 17, 2021
@robinchrist if you remove the
Release(), and since it now works, do you get memory leaks as you reload electron many times?mmomtchev commentedon Dec 27, 2021
@gabrielschulhof I am debugging what seems to be the same issue and I did
asanit and here is what happens:TSF::Release()before deleting the object that contains the TSFdetails::ThreadSafeFinalize::FinalizeWrapperWithContextat some later momentIf I remove the call to
Release()- yes, then it works, but my Node process is left hanging at the end since there are open file handlesCurrently the only way to make this work is to always have a finalizer and defer the deleting of the containing object. It is quite cumbersome, isn't it possible to skip
FinalizeWrapperWithContextwhen there is no finalizer?github-actions commentedon Mar 28, 2022
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.
robinchrist commentedon Mar 28, 2022
@gabrielschulhof What would be the best way to test this?
BB-fat commentedon Mar 31, 2022
I have the same problem on mac, but I can't trigger the bug stably.
I wrote something like this. See the full version here.
Here is my crash log.
github-actions commentedon Jun 30, 2022
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.
KevinEady commentedon Jul 1, 2022
Hi @robinchrist / @mmomtchev / @BB-fat ,
We discussed this in the 1 July Node API meeting. Do you have a repository that reproduces this issue? Since this is using Electron, it is a little cumbersome to try to create a repro on our own.
Thanks, Kevin
mmomtchev commentedon Jul 1, 2022
@KevinEady I don't have a repro, but this is not limited to Electron and it is not a bug but a design oversight.
From memory (this is the reason I don't use the C++
ThreadSafeFunctioninexprtk.js- but anapi_threadsafe_function):an object containing a
ThreadSafeFunctioncannot be destroyed. CallingReleasewill trigger an async callback on theThreadSafeFunctionobject. You can't even use a dynamic object and delete it in the finalizer, since there is a wrapper that runs after your finalizer.KevinEady commentedon Jul 1, 2022
Hi @mmomtchev ,
That logic does follow. However from what I'm seeing in the code for the finalize wrappers, these just call the finalizer with the data and context as decided in the
TSFN::Newcall. The node-addon-api finalizer wrappers are static that take as its data thisdetails::ThreadSafeFinalizetype, which encapsulates the user-provided finalizer and data passed that are passed intoNew(). They do not access theThreadSafeFunctions members themselves.Are you using a finalizer callback or data that is held by the C++ object you are
Release()ing from the destructor? If so, this makes sense to me that it will crash, as this memory is no longer valid when the finalizer wrapper gets called asynchronously after release. The TSFN finalizer callback and data must be alive throughout the existence of the TSFN.Unless I am misunderstanding something...?
2 remaining items
KevinEady commentedon Jul 1, 2022
Hi @robinchrist ,
In my threadsafe-function
AsyncIteratorexample, I callRelease()at the end of thestd::thread's entry point.robinchrist commentedon Jul 1, 2022
Yes, but that's not really a solution. The TSFN is held for a longer time and the objects which contain the TSFN are created and destroyed on the fly.
There must be a way to properly release a TSFN that is the member of an object inside the object's destructor?
github-actions commentedon Sep 30, 2022
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.
KevinEady commentedon Oct 7, 2022
We discussed this in the 7 Oct Node API meeting.
Both of the stacktraces posted above have a call to
node::FreeEnvironment. We will need to look into node core to verify the above assumption. We will need to look at the process of how/when entities are cleaned up during environment destruction.mhdawson commentedon Oct 21, 2022
@robinchrist is there any chance you could run under valgrind? That might give us more info on specifically what is leading to the crash. There are some instructions in https://github.com/nodejs/node/blob/main/doc/contributing/investigating-native-memory-leaks.md
gregcotten commentedon Dec 14, 2022
Chiming in here to say I have this issue!
legendecas commentedon Dec 18, 2022
I've submitted an explicit document on the order of invocation of
napi_finalizecallbacks and the cleanup hooks at nodejs/node#45903. I'll compose an example of how to release nested resources properly at https://github.com/nodejs/node-addon-examples.KevinEady commentedon Feb 24, 2023
We discussed this issue in the 24 Feb Node API team meeting.
There is a backing PR (nodejs/node#46692) in core to verify the order as described in nodejs/node#45903. Once the test PR has been merged, we can merge the documentation PR.
gabrielschulhof commentedon Apr 10, 2023
I think we need to thoroughly examine all the places where we NAPI_FATAL_IF_FAILED and decide if we need to add a NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS section. For example, at
node-addon-api/napi-inl.h
Line 2940 in 5adb896
napi_define_properties()fails.BB-fat commentedon May 25, 2023
It seems feasible to call
TSFN.Release()withinObjectWrap::Finalize.github-actions commentedon Aug 24, 2023
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.