Skip to content

Commit

Permalink
Filter unpicklable objects from the context dict when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
lkubb authored and dwoz committed Oct 25, 2024
1 parent a275498 commit fd7b0dc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/66999.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Filtered unpicklable objects from the context dict when invoking states in parallel on spawning platforms to avoid a crash
26 changes: 25 additions & 1 deletion salt/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import inspect
import logging
import os
import pickle
import random
import re
import site
Expand Down Expand Up @@ -2288,7 +2289,30 @@ def call_parallel(self, cdata, low, inject_globals):
args=(instance, self._init_kwargs, name, cdata, low, inject_globals),
name=f"ParallelState({name})",
)
proc.start()
try:
proc.start()
except TypeError as err:
# Some modules use the context to cache unpicklable objects like
# database connections or loader instances.
# Ensure we don't crash because of that on spawning platforms.
if "cannot pickle" not in str(err):
raise
clean_context = {}
for var, val in self._init_kwargs["context"].items():
try:
pickle.dumps(val)
except TypeError:
pass
else:
clean_context[var] = val
init_kwargs = self._init_kwargs.copy()
init_kwargs["context"] = clean_context
proc = salt.utils.process.Process(
target=self._call_parallel_target,
args=(instance, init_kwargs, name, cdata, low, inject_globals),
name=f"ParallelState({name})",
)
proc.start()
ret = {
"name": name,
"result": None,
Expand Down

0 comments on commit fd7b0dc

Please sign in to comment.