On Thu, 14 Apr 2022 22:01:18 GMT, Xue-Lei Andrew Fan <xue...@openjdk.org> wrote:
>> src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/wrapper/PKCS11.java >> line 168: >> >>> 166: // Calls disconnect() to cleanup the native part of the >>> wrapper. >>> 167: Cleaner.create().register(this, >>> 168: () -> PKCS11.disconnect(pNativeData)); >> >> If I'm not mistaken each new call to Cleaner.create() will create a new >> cleaner and a new deamon thread for it, so it's not recommended to create >> one cleaner per object. >> Also it might be worth double checking that the lambda created here doesn't >> capture `this`: IIRC there were some subtle cases where a lambda could >> unexpectedly capture `this`. >> >> Also probably the cleaner itself can't be GC'ed while its thread is running >> but you might be relying on undocumented behavior. It would be more prudent >> to stick it in a static variable to retain a strong reference. > > Good points. I may change to use a static method instead in the next commit. The new Cleaner code for PasswordCallbackHandler in P11KeyStore.java also creates a new Cleaner instance per object. Possible capture of 'this' is one reason I find `Runnable`s written out as static classes easier reason about than lambdas. ------------- PR: https://git.openjdk.java.net/jdk/pull/8248