On Mon, 15 Jun 2026 22:46:26 GMT, Viktor Klang <[email protected]> wrote:

>> src/java.smartcardio/share/classes/sun/security/smartcardio/CardImpl.java 
>> line 128:
>> 
>>> 126: 
>>> 127:         this.context = new Context(localCardId, State.OK);
>>> 128:         this.cleanable = CleanerFactory.cleaner().register(this, 
>>> this.context);
>> 
>> Move these two lines to be right after the SCardConnect(...) call? 
>> Otherwise, the cleaner may be skipped if some error occurred in between.
>
> That's a good point, there are several places where something like an OOME 
> might get thrown.

Thank's Valerie. I think that would be a good change.

For memory-visibility reasons, `Cleaner.register()` is generally recommended to 
be the last line of a constructor. This is because `register()` introduces a 
_happens-before_ edge, ensuring that the state in the context at the time of 
the `register()` call is guaranteed to be seen by the Cleaner thread.

For `CardImpl`, `Context.cardId` is final, though `Card.state` is mutable. 
However, it doesn't look like `state` can be changed by the constructor code 
following `SCardConnect()`.

So AFAICT, we can move the `register()` call earlier; no additional 
_happens-before_ (e.g. a reachabilityFence) should be needed.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/30683#discussion_r3417222387

Reply via email to