Skip to content

Commit 9686558

Browse files
committed
fix: Clear any non-UTF encodable environment variables in pilots
1 parent 86f86a1 commit 9686558

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

src/DIRAC/WorkloadManagementSystem/Utilities/PilotWrapper.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,26 @@
6868
# just logging the environment as first thing
6969
logger.debug('===========================================================')
7070
logger.debug('Environment of execution host\\n')
71-
for key, val in getattr(os, "environb", os.environ).items():
72-
# Clean the environment of non-utf-8 characters
73-
try:
74-
key = key.decode("utf-8")
75-
val = val.decode("utf-8")
76-
except UnicodeDecodeError as e:
77-
logger.error("Dropping %%s=%%s due to: %%s", key, val, e)
78-
del os.environ[key]
79-
continue
80-
logger.debug(key + '=' + val)
71+
# Clear any non-UTF encodable environment variables as they cause the pilot to fail
72+
# Use os.environb if it exists (gives raw bytes), else fall back to os.environ.
73+
environb = getattr(os, "environb", os.environ)
74+
for key, val in environb.items():
75+
try:
76+
# Decode if these are bytes; if they're already text, leave them as-is.
77+
if isinstance(key, bytes):
78+
key_decoded = key.decode("utf-8")
79+
else:
80+
key_decoded = key
81+
if isinstance(val, bytes):
82+
val_decoded = val.decode("utf-8")
83+
else:
84+
val_decoded = val
85+
except UnicodeDecodeError as e:
86+
logger.error("Dropping %%s=%%s due to decode error: %%s", key, val, e)
87+
del environb[key]
88+
continue
89+
90+
logger.debug(key_decoded + '=' + val_decoded)
8191
logger.debug('===========================================================\\n')
8292
8393
# putting ourselves in the right directory

0 commit comments

Comments
 (0)