Skip to content

fix: don't let close_session failure mask send_invoice outcome - #41

Merged
samupl merged 2 commits into
masterfrom
fix/send-invoice-close-session-failure
Nov 24, 2026
Merged

samupl merged 2 commits into
masterfrom
fix/send-invoice-close-session-failure

Conversation

@samupl

@samupl samupl commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Problem

Client.send_invoice() closed the session in a bare finally block:

try:
    send_response = self.send_invoice_in_session(...)
    return send_response
finally:
    self.close_session(session_context)

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:

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))
  File ".../ksef/client.py", line 330, in send_invoice
    self.close_session(session_context)

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 finally also meant a close failure would hide the real error when the send itself failed.

Fix

  • Errors from close_session() inside send_invoice() (requests.RequestException, which covers connection errors, timeouts and HTTP error responses) are logged as a warning with traceback and do not propagate.
  • The successful SendInvoiceResponse is 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.
  • KSeF closes abandoned online sessions automatically, so a missed close is harmless.

Tests

  • test_send_invoice_returns_response_when_close_connection_fails – reproduces the production failure (connection reset on close). It fails on master with the same ConnectionError and 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's HTTPError, 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-version from the matrix was never passed to setup-uv, so every job ran on the runner's system Python, now 3.14. The locked lxml==5.1.0 has no wheel for 3.14 and its source build failed.

  • test.yml: pass matrix.python-version to setup-uv, so the matrix really tests 3.9–3.13
  • uv.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.Union instead of the "A | B" string annotation, which raised TypeError on 3.9

Only the lockfile changed; the dependency ranges in pyproject.toml are the same.

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
@samupl
samupl merged commit a23cbe2 into master Nov 24, 2026
8 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.

1 participant