• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LudovicRousseau / pyscard-devel / 14228676560 / 12
65%
master: 65%

Build:
DEFAULT BRANCH: master
Ran 02 Apr 2025 08:15PM UTC
Files 67
Run time 13s
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

02 Apr 2025 08:02PM UTC coverage: 43.912% (-0.5%) from 44.395%
14228676560.12

push

github

LudovicRousseau
Add an explicit .close() method to CardConnection()

Thanks to Mike Kazantsev for the bug report
"PCSCCardConnection.__del__ method raises exceptions that cannot be easily handled in python code. #223"
Closes: https://github.com/LudovicRousseau/pyscard/issues/223

Describe the bug

PCSCCardConnection.__del__ method raises exceptions that cannot be
easily handled in python code.

To Reproduce

I have a code that does something along the lines of:

req = smartcard.CardRequest.CardRequest(...)
svc = req.waitforcard()
svc.connection.connect()
svc.connection.transmit(cmd)
svc.connection.disconnect()
sleep(...)
exit()

Sometimes this raises following exception:

Exception ignored in: <function PCSCCardConnection.__del__ at 0x7fad81c78fe0>
Traceback (most recent call last):
  File "/usr/lib/python3.13/site-packages/smartcard/pcsc/PCSCCardConnection.py", line 90, in __del__
    raise CardConnectionException(
smartcard.Exceptions.CardConnectionException: Failed to release context: Service not available.: Service not available. (0x8010001D)

As far as I can tell, it gets raised during python's internal garbage
collection, so cannot easily be handled by code, as GC is supposed to
not be directly controlled by python code (aside from some hacks in "gc"
module and ordering duarantees iirc).

Expected behavior

Context can be closed manually within the usual try: ... except: ...
statements, so that any exceptions raised when closing it can be handled
properly.

Additional context

Workaround can be using del svc or such statements, but there's no
guarantee it'd trigger GC, and pretty sure they won't call __del__
method within try-except context anyway, resulting in same uncatchable
exception, just at a different time.

Running SCardReleaseContext(svc.connection.component.hcontext) looks
like going too deep into internals, and hence a bad way to do it, and
does not solve the issue, as it guarantees that __del__ method will now
fail with "Invalid handle." error.
But ... (continued)

94 of 436 branches covered (21.56%)

Branch coverage included in aggregate %.

1673 of 3588 relevant lines covered (46.63%)

0.47 hits per line

Source Files on job 14228676560.12
  • Tree
  • List 67
  • Changed 56
  • Source Changed 52
  • Coverage Changed 6
Coverage ∆ File Lines Relevant Covered Missed Hits/Line Branch Hits Branch Misses
  • Back to Build 14228676560
  • 5d50702b on github
  • Prev Job for on master (#14132186882.1)
  • Next Job for on master (#14251842065.16)
  • Delete
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc