Fix KafkaError error strings raising/garbling on non-UTF-8 locales (#448) - #2331
Fix KafkaError error strings raising/garbling on non-UTF-8 locales (#448)#2331Sparsh (iSparshP) wants to merge 4 commits into
Conversation
|
🎉 All Contributor License Agreements have been signed. Ready to merge. |
|
Thanks for digging into this. Nice diagnosis — the strict-vs- One gap:
Same one-line substitution as lines 99/101: if (self->str) {
reason = cfl_PyUnistr_FromStringSafe(self->str);
} else {
reason = NULL;
}This makes the decode non-raising, which also moots the unchecked-result issue for |
Pranav Shah (prashah-confluent)
left a comment
There was a problem hiding this comment.
Thanks Sparsh (@iSparshP)
Have few comments which could ensure that all the places are covered and the coding style guidelines are honoured.
…onfluentinc#448) KafkaError_str() decoded error strings with PyUnicode_FromString(), which assumes strict UTF-8 and raises UnicodeDecodeError when librdkafka or the OS returns strings in the system locale encoding (e.g. on non-English Windows). Add cfl_PyUnistr_FromStringSafe(), which decodes via PyUnicode_DecodeUTF8(..., "replace") so invalid bytes degrade to U+FFFD instead of raising, and use it for both the custom and librdkafka-derived error strings. Also route KafkaError_reduce() through the same helper. __reduce__ is what pickle (and copy.deepcopy) call, so the same invalid bytes previously crashed whenever a KafkaError crossed a process boundary; without this confluentinc#448 was not fully closed. Add a non-ASCII round-trip test covering the valid-UTF-8 path.
66c1a09 to
1f100c9
Compare
|
Thanks for the thorough review Pranav Shah (@prashah-confluent), all three addressed in the amended commit:
|
|
Heads up on the CLA: the signing link from @confluent-cla-assistant ( I ruled out my setup, reproduced it in a clean Incognito window with all extensions disabled, and on a second browser, so it looks like an Ironclad-side / workflow issue rather than anything local. Re-running the check via an empty commit didn't help since the link is a fixed workflow ID. Could a maintainer regenerate the CLA workflow or point me to an alternate way to sign? Happy to push an empty commit to re-run the check as soon as it's working. |
|
/sem-approve |
Pranav Shah (prashah-confluent)
left a comment
There was a problem hiding this comment.
Thanks Sparsh (@iSparshP) for the fixes, we will be taking care of the cla-status-check issue and will merge the PR after one final review.
|
Sparsh (@iSparshP) , the team has fixed the cla link. Can you give it a try? If required do an empty commit as well? |
Kaushik Raina (k-raina)
left a comment
There was a problem hiding this comment.
Thanks for PR! On comment
|
|
||
| if (self->str) { | ||
| reason = cfl_PyUnistr(_FromString(self->str)); | ||
| reason = cfl_PyUnistr_FromStringSafe(self->str); |
There was a problem hiding this comment.
Can we add test case for pickle/reduce()?
Fixes #448.
Problem
KafkaError.str()decodes the underlying C error string withPyUnicode_FromString(), which assumes strict UTF-8. When librdkafka or the OS returns an error string in the system locale encoding rather than UTF-8 — e.g. on a non-English Windows install — this raisesUnicodeDecodeErroror produces garbled output, as reported in #448.Fix
Add a small helper
cfl_PyUnistr_FromStringSafe()that decodes viaPyUnicode_DecodeUTF8(s, len, "replace"). Invalid bytes degrade to U+FFFD (�) instead of raising, so error reporting never blows up on a locale-encoded string. Both the custom-message andrd_kafka_err2str()paths inKafkaError_str()now use it.This is intentionally the minimal, non-raising fix: it guarantees a usable string in all locales rather than attempting full locale-encoding detection.
Test
Added
test_kafka_error_non_ascii_strcovering a non-ASCII (Cyrillic) error message. Fulltests/test_error.pypasses locally (7 passed) against a fresh build.