[fix][client] prevent garbage collection of token-supplier#412
[fix][client] prevent garbage collection of token-supplier#412shibd merged 3 commits intoapache:masterfrom
Conversation
|
@RobertIndie @BewareMyPower Sorry I haven't used C++ much before, am I right to assume that's what I need to do? |
|
Yeah, compared to a successful workflow before (https://github.com/apache/pulsar-client-node/actions/runs/13716725396/job/38363013887): It seems that there are two more open handles that keep Jest from exiting: |
src/Client.cc
Outdated
| if (obj.Has(CFG_AUTH_PROP) && obj.Get(CFG_AUTH_PROP).IsObject()) { | ||
| Authentication *auth = Authentication::Unwrap(obj.Get(CFG_AUTH_PROP).ToObject()); | ||
| this->authRef_ = Napi::Persistent(obj.Get(CFG_AUTH_PROP).As<Napi::Object>()); | ||
| this->authRef_.SuppressDestruct(); |
There was a problem hiding this comment.
SuppressDestruct() suppresses the destruction of authRef_, which is held by the Client instance. Could you try removing this call?
This method is used here:
pulsar-client-node/src/Client.cc
Line 88 in b79ead0
However, constructor is a static variable, whose lifetime is the same with the whole program. But here authRef_'s lifetime is the same with the Client. Suppressing the destruction of authRef_ could cause memory leak.
There was a problem hiding this comment.
I didn't have time looking deeper for now. But I assume that when the Client destructs, the authRef_ will be destroyed as well unless you call SuppressDestruct().
The original issue happens just because auth is a local variable that is referenced by the client config's authentication field. However, since it's now a field of Client, whose lifetime should be longer than its config. We can now prevent the GC of auth.
There was a problem hiding this comment.
/cc @RobertIndie Please help double confirm if there is anything wrong with my analysis above
There was a problem hiding this comment.
If that's the case then perhaps removing that for the logger object will also stop that other open handle.
I am away this weekend, but will give this a go when I'm back.
There was a problem hiding this comment.
Thanks for your analysis. I think it's better to add a test to verify it.
There was a problem hiding this comment.
Sorry I am on mobile just seen that you have done it and tests passed!
RobertIndie
left a comment
There was a problem hiding this comment.
Could you help add a test to verify this change?
When using the new async token‐supplier API (
() => Promise<string>) introduced in #395, the JSAuthenticationobject and its backingThreadSafeFunctioncan be garbage‐collected if no live JS references remain. After the initial token expires, Pulsar’s broker will send a new auth challenge and the native code crashes when it tries to invoke a finalized TSFN.By persisting a reference to the authentication object in the client it prevents the V8 garbage collector from dropping the token-supplier.