Skip to content

Commit

Permalink
Ensure injected globals are defined on spawning platforms
Browse files Browse the repository at this point in the history
This ensures dunders like __env__ are defined in states running in
parallel on spawning platforms.

The __running__ dict can contain salt.utils.process.Process instances,
which are still picklable.
  • Loading branch information
lkubb committed Oct 24, 2024
1 parent f3a49ca commit 3cf69dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/66996.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensured global dunders like __env__ are defined in state module that are run in parallel on spawning platforms
12 changes: 8 additions & 4 deletions salt/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2151,12 +2151,15 @@ def requisite_in(self, high):
return req_in_high, errors

@classmethod
def _call_parallel_target(cls, instance, init_kwargs, name, cdata, low):
def _call_parallel_target(
cls, instance, init_kwargs, name, cdata, low, inject_globals
):
"""
The target function to call that will create the parallel thread/process
"""
if instance is None:
instance = cls(**init_kwargs)
instance.states.inject_globals = inject_globals
# we need to re-record start/end duration here because it is impossible to
# correctly calculate further down the chain
utc_start_time = datetime.datetime.utcnow()
Expand Down Expand Up @@ -2261,7 +2264,7 @@ def _call_parallel_target(cls, instance, init_kwargs, name, cdata, low):
with salt.utils.files.fopen(tfile, "wb+") as fp_:
fp_.write(msgpack_serialize(ret))

def call_parallel(self, cdata, low):
def call_parallel(self, cdata, low, inject_globals):
"""
Call the state defined in the given cdata in parallel
"""
Expand All @@ -2278,10 +2281,11 @@ def call_parallel(self, cdata, low):
instance = None
else:
instance = self
inject_globals = None

proc = salt.utils.process.Process(
target=self._call_parallel_target,
args=(instance, self._init_kwargs, name, cdata, low),
args=(instance, self._init_kwargs, name, cdata, low, inject_globals),
name=f"ParallelState({name})",
)
proc.start()
Expand Down Expand Up @@ -2428,7 +2432,7 @@ def call(self, low, chunks=None, running=None, retries=1):
)
elif not low.get("__prereq__") and low.get("parallel"):
# run the state call in parallel, but only if not in a prereq
ret = self.call_parallel(cdata, low)
ret = self.call_parallel(cdata, low, inject_globals)
else:
self.format_slots(cdata)
with salt.utils.files.set_umask(low.get("__umask__")):
Expand Down

0 comments on commit 3cf69dc

Please sign in to comment.