Repository navigation
fix: don't let close_session failure mask send_invoice outcome - #41
Merged
Merged
Conversation
send_invoice() closed the session in a bare finally block, so an error from close_session() (e.g. ConnectionResetError) replaced the successful SendInvoiceResponse. The invoice was already accepted by KSeF, but the caller saw an exception, treated it as a failed send and retried it, which KSeF then rejected as a duplicate. Close errors (requests.RequestException) are now logged as a warning and swallowed. The send result - or the original send exception - is always what propagates. KSeF closes abandoned sessions on its own.
The matrix python-version was never passed to setup-uv, so every job ran uv sync on the runner's system Python. ubuntu-latest now ships Python 3.14, for which the locked lxml 5.1.0 has no wheel, so the source build failed (no libxml2/libxslt headers) on every job. - pass matrix.python-version to setup-uv - uv.lock: lxml 5.1.0 -> 5.4.0 (wheels for 3.13), pydantic 1.10.6 -> 1.10.27 (1.10.6 fails to import on 3.9 with typing-extensions 4.15) - xades: use typing.Union instead of a PEP 604 string annotation, which raises TypeError on 3.9 when evaluated
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Client.send_invoice()closed the session in a barefinallyblock:If
close_session()raised, its exception replaced the successful return value. In production, the invoice was accepted by KSeF (reference number assigned in the session), then the close call failed with:The caller had no reference numbers, treated the invoice as failed, and retried it. KSeF then rejected the retry as a duplicate (
Duplikat faktury ... została już prawidłowo przesłana do systemu w sesji ...).The same
finallyalso meant a close failure would hide the real error when the send itself failed.Fix
close_session()insidesend_invoice()(requests.RequestException, which covers connection errors, timeouts and HTTP error responses) are logged as a warning with traceback and do not propagate.SendInvoiceResponseis always returned when the send succeeded. When the send failed, the original send exception propagates.close_session()itself is unchanged and still raises when called directly.Tests
test_send_invoice_returns_response_when_close_connection_fails– reproduces the production failure (connection reset on close). It fails onmasterwith the sameConnectionErrorand passes with the fix.test_send_invoice_returns_response_when_close_returns_http_error– a 500 on close still returns the response.test_send_invoice_raises_send_error_not_close_error– a 400 on send plus a connection reset on close raises the send'sHTTPError, and close is still attempted.Full unit suite: 44 passed. pre-commit (ruff, mypy, etc.) clean.
CI fix (second commit)
CI was failing on every branch, not just this PR: the
python-versionfrom the matrix was never passed tosetup-uv, so every job ran on the runner's system Python, now 3.14. The lockedlxml==5.1.0has no wheel for 3.14 and its source build failed.test.yml: passmatrix.python-versiontosetup-uv, so the matrix really tests 3.9–3.13uv.lock: lxml 5.1.0 → 5.4.0 (has 3.13 wheels); pydantic 1.10.6 → 1.10.27 (1.10.6 failed to import on 3.9 together with typing-extensions 4.15)auth/xades.py:typing.Unioninstead of the"A | B"string annotation, which raisedTypeErroron 3.9Only the lockfile changed; the dependency ranges in
pyproject.tomlare the same.