I have encountered the following error with GnuPG 2.2.34 when I was gpg --edit-card and factory-reset
sending command SELECT AID failed: Bad secret key
After printing some log I found a race condition
2022-02-13 11:56:52 scdaemon[39654] DBG: enter: apdu_get_status: slot=0 hang=0 2022-02-13 11:56:52 scdaemon[39654] trylock_slot 2022-02-13 11:56:52 scdaemon[39654] DBG: chan_7 <- APDU 00A4040006D27600012401 2022-02-13 11:56:52 scdaemon[39654] trylock_slot 2022-02-13 11:56:52 scdaemon[39654] apdu_send_direct failed: Bad secret key 2022-02-13 11:56:52 scdaemon[39654] DBG: chan_7 -> ERR 65543 Bad secret key <Unsspecified source> 2022-02-13 11:56:52 scdaemon[39654] DBG: chan_7 <- UNLOCK 2022-02-13 11:56:52 scdaemon[39654] DBG: chan_7 -> OK 2022-02-13 11:56:52 scdaemon[39654] unlock_slot 2022-02-13 11:56:52 scdaemon[39654] DBG: leave: apdu_get_status => sw=0x0 statuss=7
where the second trylock_slot failed and returned SW_HOST_BUSY.
However, this works on 2.3.4 as there is a fix f808012