Skip to content

fix: resolve NamedTemporaryFile permission errors on Windows in tests#485

Merged
cffls merged 2 commits intoPython-Cardano:mainfrom
Lampard7crypt:main
Mar 1, 2026
Merged

fix: resolve NamedTemporaryFile permission errors on Windows in tests#485
cffls merged 2 commits intoPython-Cardano:mainfrom
Lampard7crypt:main

Conversation

@Lampard7crypt
Copy link
Contributor

No description provided.

@cffls
Copy link
Collaborator

cffls commented Feb 28, 2026

#479 should be able to resolve issue #478 . Could you please explain if there is any other issue this PR tries to address?

@Lampard7crypt
Copy link
Contributor Author

#479 should be able to resolve issue #478 . Could you please explain if there is any other issue this PR tries to address?

  1. decode_arrayinfinite recursion (serialization.py)
    PyCardano overrides cbor2's array decoder to preserve indefinite-length CBOR arrays as IndefiniteFrozenList. Both branches of the override called self.decode_array() —which after patching major_decoders[4] re-entered the same function, causing infinite recursion. As a side effect, for arrays with 24+ items this double-called _decode_length on the stream, corrupting it and producing CBORDecodeEOF.

Fix: Save major_decoders[4] before patching, then delegate to that saved reference instead of calling self.decode_array(). This breaks the recursion loop entirely.

  1. Windows NamedTemporaryFile permission errors (7 tests)
    What: On Windows, NamedTemporaryFile() holds an exclusive file lock while open. When save() tried to open the same path for writing, it got PermissionError. Works fine on Linux/macOS where locking is advisory.

Fix: Use delete=False so the lock is released immediately, then manually delete the file with os.unlink() in a finally block.

@cffls
Copy link
Collaborator

cffls commented Mar 1, 2026

Fix: Save major_decoders[4] before patching, then delegate to that saved reference instead of calling self.decode_array(). This breaks the recursion loop entirely.

self.decode_array(subtype) resolves to CBORDecoder.decode_array, which is the original unpatched instance method on the class — it was never modified. Saving _original_decode_array = cbor2._decoder.major_decoders[4] before patching and calling it explicitly does the exact same thing, just with more indirection.

I am fine with the changes for the temp file usage though.

@Lampard7crypt Lampard7crypt changed the title fix: decode_array stream corruption for lists with 24+ items (closes #478, relates to #402) fix: resolve NamedTemporaryFile permission errors on Windows in tests Mar 1, 2026
@Lampard7crypt
Copy link
Contributor Author

Fix: Save major_decoders[4] before patching, then delegate to that saved reference instead of calling self.decode_array(). This breaks the recursion loop entirely.

self.decode_array(subtype) resolves to CBORDecoder.decode_array, which is the original unpatched instance method on the class — it was never modified. Saving _original_decode_array = cbor2._decoder.major_decoders[4] before patching and calling it explicitly does the exact same thing, just with more indirection.

I am fine with the changes for the temp file usage though.

Oh, thanks for the clarification, you're right that self.decode_array(subtype) resolves to the unpatched class method since we only patched the major_decoders dict and not the class itself, so saving the explicit reference is indeed redundant. I've reverted those changes. The NamedTemporaryFile fixes I've kept as-is.

@codecov
Copy link

codecov bot commented Mar 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.60%. Comparing base (1d2e825) to head (907cd8b).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #485      +/-   ##
==========================================
+ Coverage   90.57%   90.60%   +0.03%     
==========================================
  Files          34       34              
  Lines        5154     5154              
  Branches      781      781              
==========================================
+ Hits         4668     4670       +2     
+ Misses        306      305       -1     
+ Partials      180      179       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@cffls cffls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@cffls cffls merged commit b1788b0 into Python-Cardano:main Mar 1, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants