Skip to content

Commit 94cc7dc

Browse files
chrisburrweb-flow
authored andcommitted
sweep: DIRACGrid#8103 Clear any non-UTF encodable environment variables in pilots
1 parent 4167fc9 commit 94cc7dc

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/DIRAC/WorkloadManagementSystem/Utilities/PilotWrapper.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,26 @@ def formatTime(self, record, datefmt=None):
110110
# just logging the environment as first thing
111111
logger.debug('===========================================================')
112112
logger.debug('Environment of execution host\\n')
113-
for key, val in getattr(os, "environb", os.environ).items():
114-
# Clean the environment of non-utf-8 characters
115-
try:
116-
key = key.decode("utf-8")
117-
val = val.decode("utf-8")
118-
except UnicodeDecodeError as e:
119-
logger.error("Dropping %%s=%%s due to: %%s", key, val, e)
120-
del os.environ[key]
121-
continue
122-
logger.debug(key + '=' + val)
113+
# Clear any non-UTF encodable environment variables as they cause the pilot to fail
114+
# Use os.environb if it exists (gives raw bytes), else fall back to os.environ.
115+
environb = getattr(os, "environb", os.environ)
116+
for key, val in environb.items():
117+
try:
118+
# Decode if these are bytes; if they're already text, leave them as-is.
119+
if isinstance(key, bytes):
120+
key_decoded = key.decode("utf-8")
121+
else:
122+
key_decoded = key
123+
if isinstance(val, bytes):
124+
val_decoded = val.decode("utf-8")
125+
else:
126+
val_decoded = val
127+
except UnicodeDecodeError as e:
128+
logger.error("Dropping %%s=%%s due to decode error: %%s", key, val, e)
129+
del environb[key]
130+
continue
131+
132+
logger.debug(key_decoded + '=' + val_decoded)
123133
logger.debug('===========================================================\\n')
124134
125135
logger.debug(sys.version)

0 commit comments

Comments
 (0)