Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: don't execute callbacks within a greenlet if we're already in one #1284

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pyinfra/api/arguments_typed.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ class PyinfraOperation(Generic[P], Protocol):
def __call__(
self,
#
# op args
# needs to be first
#
*args: P.args,
#
# ConnectorArguments
#
# Auth
Expand Down Expand Up @@ -74,6 +69,10 @@ def __call__(
_run_once: bool = False,
_serial: bool = False,
#
# op args
#
*args: P.args,
#
# op kwargs
#
**kwargs: P.kwargs,
Expand Down
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
Loading