Skip to content

Commit a07fbf6

Browse files
committed
fix async sudo askpass cleanup during disconnect
1 parent 03d2c38 commit a07fbf6

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/pyinfra/api/host.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,12 @@ def disconnect(self) -> None:
462462
async def disconnect_async(self) -> None:
463463
self._check_state()
464464

465+
await remove_any_sudo_askpass_file_async(self)
466+
465467
disconnect_func = getattr(self.connector, "disconnect", None)
466468
if disconnect_func:
467469
await disconnect_func()
468470

469-
await remove_any_sudo_askpass_file_async(self)
470-
471471
self.state.trigger_callbacks("host_disconnect", self)
472472
self.connected = False
473473
if hasattr(self.state, "active_hosts"):

tests/test_api/test_api_host.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from unittest import TestCase
22

3+
import asyncio
4+
from types import SimpleNamespace
5+
from unittest.mock import patch
6+
7+
from pyinfra.api import Config, State
38
from pyinfra.api.host import HostData
49

10+
from ..util import make_inventory
11+
512

613
class TestHostData(TestCase):
714
def test_host_data(self):
@@ -34,3 +41,25 @@ def test_host_data_missing(self):
3441

3542
assert context.exception.args[0] == "Host `somehost` has no data `not-a-key`"
3643
assert data.get("not-a-key") is None
44+
45+
46+
class TestHostDisconnect(TestCase):
47+
def test_disconnect_removes_askpass_before_connector_close(self):
48+
state = State(make_inventory(), Config())
49+
host = state.inventory.get_host("somehost")
50+
host.connector_data["sudo_askpass_path"] = "/tmp/askpass"
51+
52+
calls: list[str] = []
53+
54+
async def fake_remove(_host):
55+
calls.append("remove")
56+
57+
async def fake_disconnect():
58+
calls.append("disconnect")
59+
60+
host.connector = SimpleNamespace(disconnect=fake_disconnect)
61+
62+
with patch("pyinfra.api.host.remove_any_sudo_askpass_file_async", fake_remove):
63+
asyncio.run(host.disconnect_async())
64+
65+
assert calls == ["remove", "disconnect"]

0 commit comments

Comments
 (0)