Code-review follow-ups for v3.5.2/v3.5.1, plus a test flake found while
re-running the suites:
- F1: a half-initialized equip is no longer reported as success. cmd2
swallows exceptions raised inside the equip command (and prints no
traceback by default), while PysimApp.equip() assigns card/rs before it
registers the command sets - so app.card alone once let the "CommandSet ...
is already installed" abort pass as done while /api/tree stayed broken.
The auto-equip attempt now captures the output with cmd2 debug on (a
swallowed error prints a traceback), requires the new profile's command-set
instances to be installed (_app_equip_complete), and the manual
/api/command equip branch applies the post-equip refresh only when that
check passes (and reports it in the output when it does not).
- F3: SCARD_E_SHARING_VIOLATION is recoverable (a rebuild cannot free another
process's claim) instead of transport-fatal.
- F4: SPI1 b2b1 = 11 (Digital Signature) is refused instead of building an
unsigned packet; help notes RC is CRC-32 only (KID CRC-16 not offered).
- F5: _clear_app_card_state removes the muted stdout again when the app object
had no stdout attribute.
- F7: the watchdog re-arm keeps its rate-limit window when the trigger is
busy/disabled instead of consuming it.
- F2: stale docstring in _auto_equip_attempt.
- tests: equip-state units, half-equip and captured-traceback failures,
unequip-on-failure, busy trigger, sharing violation, DS refusal.
- bonus: the MCC/MNC random-pick test could fail because a dict keyed by
(mcc, mnc) keeps one of two entries (the bundled list carries both a real
and an MVNO entry for 234/18 and 234/28); the picker was correct - the
assertion now checks the pair against the non-MVNO pairs.
549 frontend / 421 Python green; version 3.5.4; sw cache simple-v256.
Equip was broken after a card swap: auto-equip (and manual Equip) failed with
"Failed to transmit with protocol T0. Card was removed. (0x80100069)" until
the server was restarted.
Chain (v3.1.2 regression):
- _handle_card_disconnect() cleared server.card/scc but not pySim's
app.card/app.rs/app.lchan, so the removed card - and with it the old PC/SC
link and its exclusive card handle - stayed referenced; handlers using
app.rs (e.g. the PWA's /api/tree poll) kept transmitting over the dead card.
- those errors carry hresult=0x80100069 (SCARD_W_REMOVED_CARD), but
_is_pcsc_error() treated any PC/SC error as a dead service and set
_TRANSPORT_STALE; the next auto-equip then called _ensure_transport(),
which built a second PcscSimLink while the old one was still connected -
the new link inherited the removed card's handle and failed on its first
APDU. No retry existed, so every later equip repeated the failure.
Fixes:
- _is_transport_fatal(): only service/context hresults rebuild the transport
(E_NO_SERVICE, E_SERVICE_STOPPED, E_NO_READERS_AVAILABLE, E_INVALID_HANDLE,
...); card-level states (W_REMOVED_CARD, E_NO_SMARTCARD, W_RESET_CARD,
W_UNRESPONSIVE_CARD, W_UNPOWERED_CARD) reconnect on the existing link.
All 8 disconnect call sites pass the new verdict.
- _clear_app_card_state(): unequip through pySim's own equip(None, None)
before clearing app.card/app.rs/app.lchan. Nulling them alone would make
the next equip abort with "CommandSet ... is already installed"
(PysimApp.equip() unregisters the previous profile's command sets from
self.rs), which left /api/tree broken after a swap. A failed auto-equip
attempt unequips the half-initialized shell as well. Handlers now answer
"no card" instead of transmitting over the dead card, and the old link
becomes collectable.
- _ensure_transport(): disconnects the old link before building the new one
(restoring it if the factory fails) - the rebuild path is now safe for the
real pcscd-restart case.
- auto-equip: _auto_equip_attempt()/_auto_equip_attempts() retry up to 3
times, 1 s apart (fatal failures mark the transport so the retry rebuilds);
the watchdog re-arms it (_auto_equip_rearm) every 5 s while a card is
present and the session is down, with exponential backoff to 60 s for a
card that cannot be initialized at all.
- fastinit.init_card_fast(): also retries once after a PC/SC link error
(CardConnectionException/NoCardException), not only after SW mismatches.
Tests: transport-fatal classification, app-state clearing, shell unequip,
old-link release, auto-equip retry/backoff/re-arm, fastinit link retry.
547 frontend / 410 Python green; version 3.5.2; sw cache simple-v254.
After pcscd restarted (or the reader re-enumerated) the server stayed
permanently cardless:
- pyscard's presence-monitor thread stops itself on SCARD_E_NO_SERVICE
and pyscard never starts it again, so card insertions were no longer
noticed and auto-equip was dead;
- PCSCCardConnection.connect() reuses the context handle captured when
the reader was opened, so the old connection could not be revived and
every APDU kept failing with 'Service not available'.
- _start_card_watchdog(): a daemon that every 5 s checks whether
CardMonitor().rmthread.instance is alive and recreates the stopped
thread (CardMonitoringThread.instance = None + a fresh rmthread); the
new thread reports already-present cards as inserted, which triggers
auto-equip. start_card_monitor() starts the watchdog.
- _is_pcsc_error(): pyscard exceptions carry an hresult, card-level
errors do not; the disconnect paths (AUTO-STATUS, status poll, timer
expiration, net-sim, tree, event send, OTA send) pass the verdict to
_handle_card_disconnect(stale=...), which marks the transport stale.
- _ensure_transport(): builds a fresh transport via
server.transport_factory (installed by __main__ as
mod.init_reader(opts, **tracer_kwargs)) and installs it into
app.sl/server.sl; called by the auto-equip worker, the /api/command
equip branch and _esim_reinit.
- tests: watchdog tick, transport recreation (fresh/stale/failure/no
factory), PC/SC classification, stale disconnect flag.
- docs: AGENTS card-behavior section, README troubleshooting (EN/RU);
version 3.1.2, sw.js simple-v237.