Bug Report: UnicodeDecodeError when limit_bytes truncates UTF-8 mid-character
Les mainteneurs répondent en général sous 1 jour
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 68/100
Piste de recherche
Start with kubernetes/client/rest.py at the UTF-8 decode in request, then inspect the response handling and tests for preloaded content. Reproduce with a response ending in an incomplete UTF-8 sequence and confirm ordinary invalid UTF-8 still behaves as intended. Done means a byte-limited pod log no longer raises UnicodeDecodeError when truncation splits a character.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Summary
When using read_namespaced_pod_log() with _preload_content=True and limit_bytes parameter, the client raises UnicodeDecodeError if the byte limit cuts a multi-byte UTF-8 character in the middle.
Environment
- kubernetes-client version: 36.0.3 (also occurs in versions < 36.0.0)
- Python version: 3.12
- Operating System: Linux
Problem Description
The limit_bytes parameter works at the byte level and can cut a multi-byte UTF-8 character mid-sequence. When _preload_content=True (the default), the client attempts to decode the truncated byte stream as UTF-8 in rest.py:232, which raises UnicodeDecodeError.
Root Cause
In kubernetes/client/rest.py, line 232:
r.data = r.data.decode('utf8')
This assumes:
- The response is valid UTF-8
- The byte stream is complete
However, the Kubernetes API /api/v1/namespaces/{namespace}/pods/{name}/log endpoint:
- Returns logs as a byte stream with no encoding specified (content-type: text/plain)
- The
limitBytesparameter cuts at byte boundaries, not character boundaries - Can therefore return incomplete UTF-8 sequences when the limit is reached
Evidence
Stack Trace
File "/opt/app-root/lib/python3.12/site-packages/kubernetes/client/rest.py", line 232, in request
r.data = r.data.decode('utf8')
^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 10485759: unexpected end of data
Analysis
- Position 10485759: This is exactly 10MB - 1 byte (10485760 = 10 * 1024 * 1024)
- Byte 0xc2: Start of a 2-byte UTF-8 sequence (continuation byte missing)
- The error occurs precisely at the
limit_bytesboundary
Full Call Stack
File "/opt/app-root/lib/python3.12/site-packages/kubernetes/client/api/core_v1_api.py", line 24264, in read_namespaced_pod_log
return self.read_namespaced_pod_log_with_http_info(name, namespace, **kwargs)
File "/opt/app-root/lib/python3.12/site-packages/kubernetes/client/api/core_v1_api.py", line 24387, in read_namespaced_pod_log_with_http_info
return self.api_client.call_api(...)
File "/opt/app-root/lib/python3.12/site-packages/kubernetes/client/api_client.py", line 348, in call_api
return self.__call_api(resource_path, method, ...)
File "/opt/app-root/lib/python3.12/site-packages/kubernetes/client/api_client.py", line 180, in __call_api
response_data = self.request(...)
File "/opt/app-root/lib/python3.12/site-packages/kubernetes/client/api_client.py", line 373, in request
return self.rest_client.GET(url, ...)
File "/opt/app-root/lib/python3.12/site-packages/kubernetes/client/rest.py", line 244, in GET
return self.request("GET", url, ...)
File "/opt/app-root/lib/python3.12/site-packages/kubernetes/client/rest.py", line 232, in request
r.data = r.data.decode('utf8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 10485759: unexpected end of data
Reproduction
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
# This will fail if limit_bytes cuts a UTF-8 character mid-sequence
try:
log = v1.read_namespaced_pod_log(
name="pod-with-large-logs",
namespace="default",
limit_bytes=10 * 1024 * 1024, # 10 MB
_preload_content=True, # Default
)
except UnicodeDecodeError as e:
print(f"Error at position {e.start}: {e.reason}")
# Output: Error at position 10485759: 'utf-8' codec can't decode byte 0xc2
Proposed Solutions
Option 1: Handle Incomplete UTF-8 (Recommended)
Modify rest.py:232 to handle incomplete UTF-8 sequences:
# Current code:
r.data = r.data.decode('utf8')
# Proposed fix:
r.data = r.data.decode('utf8', errors='ignore') # Or 'replace'
Or better, strip incomplete sequences at the end:
def safe_decode_utf8(data):
"""Decode UTF-8, handling incomplete sequences at the end."""
try:
return data.decode('utf8')
except UnicodeDecodeError as e:
# If error is at the end, try truncating incomplete sequence
if e.start >= len(data) - 4: # UTF-8 max 4 bytes
return data[:e.start].decode('utf8', errors='strict')
raise
Impact
This affects any application using:
read_namespaced_pod_log()withlimit_bytes- Default
_preload_content=True - Pod logs containing non-ASCII characters (common in error messages, stack traces)
References
- Related issue: #2596 (different manifestation of string handling in v36.x)
- Discussion: https://gitlab.cee.redhat.com/ccx/ccx-ocp-core/-/merge_requests/1146
Expected Behavior
The client should handle incomplete UTF-8 sequences gracefully, OR
Actual Behavior
The client raises an unhandled UnicodeDecodeError that propagates to the application.
- Langage dominant
- Python
- Étoiles
- 7.7k
- Forks
- 3.5k
- Merge moyen
- 1 j 13 h
- PR mergées (30 j)
- 21
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de kubernetes-client/python
-
getheaders is deprecated in urllib3 >= 2.0.0Peut-être à nouveau libre Une pull request pour cette issue a été fermée sans être fusionnée. Ouvertekind/bug
Difficulté 1/5 Moins d'une heure Accessibilité débutants 68/100
kubernetes-client/python#2280 · 18 commentaires · 9 réactions ·
Les mainteneurs répondent en général sous 1 jour
-
kind/feature
Difficulté 4/5 3-5 jours Accessibilité débutants 55/100
kubernetes-client/python#2752 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Make async an extraPeut-être pris @jdork0 l’a pris il y a 5 jours. Ouvertekind/feature
Difficulté 2/5 1-3 heures Accessibilité débutants 55/100
kubernetes-client/python#2739 · 3 commentaires · 1 personne assignée ·
Les mainteneurs répondent en général sous 1 jour
-
Growing library sizeOuvertehelp wanted
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
kubernetes-client/python#2677 · 15 commentaires · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour
-
Add option to disable strict x509 verifcation in sync clientPeut-être pris @Vishwa0223 l’a pris il y a 64 jours. Ouvertehelp wanted kind/feature
kubernetes-client/python#2602 · 6 commentaires · 1 personne assignée ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de kubernetes-client/python
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 67/100
pydot/pydot#601 · 4 commentaires ·
Les mainteneurs répondent en général sous 2 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
letsencrypt/cp-cps#361 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
-
CI: startup_failure because callers grant fewer permissions than ledger-app-workflows@v1 declaresPeut-être pris @maykonmichel l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
LedgerHQ/app-boilerplate-rust#227 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
linuxmint/mintupdate#1107 ·