Skip to content

Commit 69e8941

Browse files
committed
Add compatibility with StackContext
1 parent b4f300a commit 69e8941

File tree

5 files changed

+84
-54
lines changed

5 files changed

+84
-54
lines changed

salt/client/mixins.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
import salt.utils.user
3030
import salt.utils.versions
3131

32+
from salt import USE_VENDORED_TORNADO
33+
if USE_VENDORED_TORNADO:
34+
from salt.ext.tornado.stack_context import StackContext
35+
else:
36+
from contextlib import nullcontext as StackContext
37+
3238
log = logging.getLogger(__name__)
3339

3440
CLIENT_INTERNAL_KEYWORDS = frozenset(
@@ -378,26 +384,27 @@ def low(self, fun, low, print_event=True, full_return=False):
378384
data["fun_args"] = list(args) + ([kwargs] if kwargs else [])
379385
func_globals["__jid_event__"].fire_event(data, "new")
380386

381-
func = self.functions[fun]
382-
try:
383-
data["return"] = func(*args, **kwargs)
384-
except TypeError as exc:
385-
data[
386-
"return"
387-
] = "\nPassed invalid arguments: {}\n\nUsage:\n{}".format(
388-
exc, func.__doc__
389-
)
390-
try:
391-
data["success"] = self.context.get("retcode", 0) == 0
392-
393-
except AttributeError:
394-
# Assume a True result if no context attribute
395-
data["success"] = True
396-
if isinstance(data["return"], dict) and "data" in data["return"]:
397-
# some functions can return boolean values
398-
data["success"] = salt.utils.state.check_result(
399-
data["return"]["data"]
400-
)
387+
with StackContext(self.functions.context_dict.clone):
388+
func = self.functions[fun]
389+
try:
390+
data["return"] = func(*args, **kwargs)
391+
except TypeError as exc:
392+
data[
393+
"return"
394+
] = "\nPassed invalid arguments: {}\n\nUsage:\n{}".format(
395+
exc, func.__doc__
396+
)
397+
try:
398+
data["success"] = self.context.get("retcode", 0) == 0
399+
400+
except AttributeError:
401+
# Assume a True result if no context attribute
402+
data["success"] = True
403+
if isinstance(data["return"], dict) and "data" in data["return"]:
404+
# some functions can return boolean values
405+
data["success"] = salt.utils.state.check_result(
406+
data["return"]["data"]
407+
)
401408

402409
except (Exception, SystemExit) as ex: # pylint: disable=broad-except
403410
if isinstance(ex, salt.exceptions.NotImplemented):

salt/metaproxy/deltaproxy.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
from salt.utils.event import tagify
5757
from salt.utils.process import SignalHandlingProcess, default_signals
5858

59+
from salt import USE_VENDORED_TORNADO
60+
if USE_VENDORED_TORNADO:
61+
from salt.ext.tornado.stack_context import StackContext
62+
else:
63+
from contextlib import nullcontext as StackContext
64+
5965
log = logging.getLogger(__name__)
6066

6167

@@ -562,10 +568,11 @@ def target(cls, minion_instance, opts, data, connected):
562568
uid = salt.utils.user.get_uid(user=opts.get("user", None))
563569
minion_instance.proc_dir = salt.minion.get_proc_dir(opts["cachedir"], uid=uid)
564570

565-
if isinstance(data["fun"], tuple) or isinstance(data["fun"], list):
566-
ProxyMinion._thread_multi_return(minion_instance, opts, data)
567-
else:
568-
ProxyMinion._thread_return(minion_instance, opts, data)
571+
with StackContext(minion_instance.ctx):
572+
if isinstance(data["fun"], tuple) or isinstance(data["fun"], list):
573+
ProxyMinion._thread_multi_return(minion_instance, opts, data)
574+
else:
575+
ProxyMinion._thread_return(minion_instance, opts, data)
569576

570577

571578
def thread_return(cls, minion_instance, opts, data):

salt/metaproxy/proxy.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
from salt.utils.event import tagify
5353
from salt.utils.process import SignalHandlingProcess, default_signals
5454

55+
from salt import USE_VENDORED_TORNADO
56+
if USE_VENDORED_TORNADO:
57+
from salt.ext.tornado.stack_context import StackContext
58+
else:
59+
from contextlib import nullcontext as StackContext
60+
5561
log = logging.getLogger(__name__)
5662

5763

@@ -380,10 +386,11 @@ def target(cls, minion_instance, opts, data, connected):
380386
opts["cachedir"], uid=uid
381387
)
382388

383-
if isinstance(data["fun"], tuple) or isinstance(data["fun"], list):
384-
ProxyMinion._thread_multi_return(minion_instance, opts, data)
385-
else:
386-
ProxyMinion._thread_return(minion_instance, opts, data)
389+
with StackContext(minion_instance.ctx):
390+
if isinstance(data["fun"], tuple) or isinstance(data["fun"], list):
391+
ProxyMinion._thread_multi_return(minion_instance, opts, data)
392+
else:
393+
ProxyMinion._thread_return(minion_instance, opts, data)
387394

388395

389396
def thread_return(cls, minion_instance, opts, data):

salt/minion.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@
101101
except ImportError:
102102
HAS_WIN_FUNCTIONS = False
103103

104+
from salt import USE_VENDORED_TORNADO
105+
if USE_VENDORED_TORNADO:
106+
from salt.ext.tornado.stack_context import ExceptionStackContext
107+
else:
108+
from contextlib import nullcontext as ExceptionStackContext
104109

105110
log = logging.getLogger(__name__)
106111

@@ -1675,9 +1680,10 @@ def handle_timeout(*_):
16751680

16761681
timeout_handler = handle_timeout
16771682

1678-
# pylint: disable=unexpected-keyword-arg
1679-
self._send_req_async(load, timeout)
1680-
# pylint: enable=unexpected-keyword-arg
1683+
with ExceptionStackContext(timeout_handler):
1684+
# pylint: disable=unexpected-keyword-arg
1685+
self._send_req_async(load, timeout)
1686+
# pylint: enable=unexpected-keyword-arg
16811687
return True
16821688

16831689
@tornado.gen.coroutine
@@ -2251,11 +2257,12 @@ def timeout_handler(*_):
22512257
timeout_handler()
22522258
return ""
22532259
else:
2254-
# pylint: disable=unexpected-keyword-arg
2255-
ret_val = self._send_req_async(
2256-
load, timeout=timeout
2257-
)
2258-
# pylint: enable=unexpected-keyword-arg
2260+
with ExceptionStackContext(timeout_handler):
2261+
# pylint: disable=unexpected-keyword-arg
2262+
ret_val = self._send_req_async(
2263+
load, timeout=timeout
2264+
)
2265+
# pylint: enable=unexpected-keyword-arg
22592266

22602267
log.trace("ret_val = %s", ret_val) # pylint: disable=no-member
22612268
return ret_val
@@ -2341,11 +2348,12 @@ def timeout_handler(*_):
23412348
timeout_handler()
23422349
return ""
23432350
else:
2344-
# pylint: disable=unexpected-keyword-arg
2345-
ret_val = self._send_req_async(
2346-
load, timeout=timeout
2347-
)
2348-
# pylint: enable=unexpected-keyword-arg
2351+
with ExceptionStackContext(timeout_handler):
2352+
# pylint: disable=unexpected-keyword-arg
2353+
ret_val = self._send_req_async(
2354+
load, timeout=timeout
2355+
)
2356+
# pylint: enable=unexpected-keyword-arg
23492357

23502358
log.trace("ret_val = %s", ret_val) # pylint: disable=no-member
23512359
return ret_val
@@ -3270,18 +3278,19 @@ def timeout_handler(*args):
32703278
log.warning("Unable to forward pub data: %s", args[1])
32713279
return True
32723280

3273-
self.local.pub_async(
3274-
data["tgt"],
3275-
data["fun"],
3276-
data["arg"],
3277-
data["tgt_type"],
3278-
data["ret"],
3279-
data["jid"],
3280-
data["to"],
3281-
io_loop=self.io_loop,
3282-
callback=lambda _: None,
3283-
**kwargs
3284-
)
3281+
with ExceptionStackContext(timeout_handler):
3282+
self.local.pub_async(
3283+
data["tgt"],
3284+
data["fun"],
3285+
data["arg"],
3286+
data["tgt_type"],
3287+
data["ret"],
3288+
data["jid"],
3289+
data["to"],
3290+
io_loop=self.io_loop,
3291+
callback=lambda _: None,
3292+
**kwargs
3293+
)
32853294

32863295
def _send_req_sync(self, load, timeout):
32873296
if self.opts["minion_sign_messages"]:

salt/utils/thin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import salt
2626
import salt.exceptions
27-
import tornado as tornado
27+
import tornado
2828
import salt.utils.files
2929
import salt.utils.hashutils
3030
import salt.utils.json

0 commit comments

Comments
 (0)