apdu4j icon indicating copy to clipboard operation
apdu4j copied to clipboard

Delay on CLI tool shutdown using Bare BIBO

Open gwachob opened this issue 2 years ago • 1 comments

I've been digging into performance issues with the globalplatform pro tool, specifically noticing that we get a significant (almost 3/4 to a whole second) delay after apdu commands and responses are done.

I am actually able to reproduce this using the apdu4j command line tool and it seems to be related to the use of CardBIBO (used by the globalplatformpro CLI as well) -- specifically, when the -B (bare bibo) option is used, we get the delay, but when its not used, we get no delay at the end of the operation (at least judging by the output to the terminal).

I am hoping by understanding this issue on apdu4j we can maybe improve the globalplatformpro CLI performance, but I admit they could be completely unrelated issues.

Here's a simple demonstration, built from HEAD (2268c830) - I don't really know how to intepret the output of the time command except that the elapsed time matches the last "total" number.

» time java -jar tool/target/apdu4j.jar -a 00A4040000  -d -v -B
# Using JNA2PCSC version 0.2
SCardConnect("Identiv uTrust 4700 F Dual Interface Reader(2)", T=*) -> T=1, 3BD595FF8191FE1FC300C700D32DB4
A>> T=1 (4+0000) 00A40400 00 
A<< (0018+2) (19ms) 6F108408A000000151000000A5049F6501FF 9000
SCardDisconnect("Identiv uTrust 4700 F Dual Interface Reader(2)", true) tx:5/rx:20 in 29ms
java -jar tool/target/apdu4j.jar -a 00A4040000 -d -v -B  0.83s user 0.16s system 70% cpu 1.404 total

time java -jar tool/target/apdu4j.jar -a 00A4040000  -d -v
# Using JNA2PCSC version 0.2
SCardConnect("Identiv uTrust 4700 F Dual Interface Reader(2)", T=*) -> T=1, 3BD595FF8191FE1FC300C700D32DB4
A>> T=1 (4+0000) 00A40400 00 
A<< (0018+2) (19ms) 6F108408A000000151000000A5049F6501FF 9000
java -jar tool/target/apdu4j.jar -a 00A4040000 -d -v  0.85s user 0.17s system 171% cpu 0.590 total

gwachob avatar Jun 01 '22 21:06 gwachob

Did some more experimentation and it seems like there's something related to the disconnect call in the CardBIBO - the two invocations seem to behave the same (with or without the -B) if the following patch is applied... (and I have also found that a similar patch in globalplatform pro's GPTool.java:119 also fixed the issue.

I realize that this patch is not appropriate in all cases, but it seems reasonable that at least in the globalplatformpro situation, maybe there should be CLI a option to use this behavior (esp for a use case like ours where we are calling the CLI a bunch). That should probably be discussed in the globalplatformpro project though.

diff --git a/pcsc/src/main/java/apdu4j/pcsc/CardBIBO.java b/pcsc/src/main/java/apdu4j/pcsc/CardBIBO.java
index 264751a..b48b22e 100644
--- a/pcsc/src/main/java/apdu4j/pcsc/CardBIBO.java
+++ b/pcsc/src/main/java/apdu4j/pcsc/CardBIBO.java
@@ -134,7 +134,7 @@ public class CardBIBO implements BIBO, AsynchronousBIBO {
     public void close() {
         closed = true;
         try {
-            card.disconnect(true);
+            card.disconnect(false);
         } catch (CardException e) {
             String err = SCard.getExceptionMessage(e);
             if (err.equals(SCard.SCARD_E_INVALID_HANDLE)) {

gwachob avatar Jun 02 '22 05:06 gwachob