Skip to content

Commit

Permalink
api: don't execute callbacks within a greenlet if we're already in one
Browse files Browse the repository at this point in the history
Prevents infinite recursion when the loacl context is lost when switching
between greenlets.
  • Loading branch information
Fizzadar committed Feb 4, 2025
1 parent 935ce07 commit 32c9312
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pyinfra/api/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import gevent
from typing_extensions import Unpack

from pyinfra.context import ctx_config, ctx_host
from pyinfra.context import LocalContextObject, ctx_config, ctx_host

from .arguments import ConnectorArguments

Expand Down Expand Up @@ -225,6 +225,12 @@ def execute(self, state: "State", host: "Host", connector_arguments: ConnectorAr
if "state" in argspec.args and "host" in argspec.args:
return self.function(state, host, *self.args, **self.kwargs)

# If we're already running inside a greenlet (ie a nested callback) just execute the func
# without any gevent.spawn which will break the local host object.
if isinstance(host, LocalContextObject):
self.function(*self.args, **self.kwargs)
return

def execute_function():
with ctx_config.use(state.config.copy()):
with ctx_host.use(host):
Expand Down

0 comments on commit 32c9312

Please sign in to comment.