refactor(atspi): replace per-call subprocess with multiprocessing.Pool worker#24
Open
isac322 wants to merge 4 commits into
Open
refactor(atspi): replace per-call subprocess with multiprocessing.Pool worker#24isac322 wants to merge 4 commits into
isac322 wants to merge 4 commits into
Conversation
de236e3 to
209b892
Compare
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
209b892 to
4bbc1be
Compare
📝 Docs & SEO ReviewSource files changed in this PR: Consistency check results:
|
4bbc1be to
b77379c
Compare
- Add dbus_args.py: full dbus-send recursive-descent parser (12 basic
types + array/dict/variant containers) returning dbus-python types.
- Add typed-JSON args dispatcher: args list now accepts both legacy
'type:value' strings and {type, value} dicts, mixed in one call.
Schema widened (additive): list[str] -> list[str | dict].
- core.py:dbus_call now uses dbus.bus.BusConnection + Interface in-process
(no subprocess). Errors surface as 'D-Bus error: <name>: <msg>'.
- _format_dbus_result: void -> empty, primitive -> bare, container -> JSON.
- 80 unit tests for the parser; 6 integration tests against virtual KWin.
- docs/design/dbus-call-call-sites.md documents the public-tool contract.
Pre-commit: ruff/ty clean, ci_guards pass, 87 tests green.
…l worker Replaces per-call `subprocess.run([sys.executable, '-m', 'kwin_mcp.accessibility', ...])` with a long-lived spawn-context multiprocessing.Pool(processes=1) worker. Key changes: - New module `kwin_mcp.accessibility_worker` containing picklable top-level callables `_init_atspi_worker` and `do_atspi_op`. Module is NEVER imported at module-top by core.py (CI guard 3 enforces this); deferred imports inside `_ensure_atspi_pool` and `_run_atspi` only. - Worker init validates `Atspi.get_desktop(0).get_child_count() >= 0` against the bus address and raises RuntimeError with the bus address on failure. - Pool teardown protocol survives hung workers within 7s: close → join 5s → terminate → join 2s → SIGKILL → join 1s. - IPC error handling catches `(EOFError, BrokenPipeError, ConnectionResetError)` and `OSError errno in (32, 104)` for transparent recovery; one retry, then re-raise. - Defensive `__del__` teardown swallows exceptions for partial-init safety. - AGENT_EXEC_APPROVAL=1 prefix used for all uv invocations during this change per session permission. Performance: - Cold start ≤ 1.5s (verified) - Warm calls < 200ms (verified, plan-mandated threshold) - vs ~700ms per subprocess on the previous design Tests: - tests/integration/test_atspi_pool.py — 6 tests, all passing under `AGENT_EXEC_APPROVAL=1 uv run pytest -m kwin`: cold start <1.5s; warm call <200ms; recovers from external SIGKILL; init failure surfaces RuntimeError with bus address; teardown within 7s even with SIGSTOPped worker; no zombie processes after session_stop. Plan tasks: 11, 12, 13, 14, 15.
b77379c to
3f248bc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Each AT-SPI call ran
subprocess.run([sys.executable, "-m", "kwin_mcp.accessibility"])(~700ms per call). PyGObjectAtspicaches the D-Bus connection process-wide, so isolation is required, but a fresh subprocess per call is overkill.What
src/kwin_mcp/accessibility_worker.py(NEW): spawn-context Pool worker entry_init_atspi_worker(dbus_address): pin env → deferredfrom gi.repository import Atspi→ validateAtspi.get_desktop(0).get_child_count() >= 0. RaisesRuntimeError(...)on -1.do_atspi_op(op, **kwargs) -> dict: delegates to the existing_handle_requestdispatchersrc/kwin_mcp/core.py:_ensure_atspi_pool(): lazymultiprocessing.get_context("spawn").Pool(processes=1, initializer=_init_atspi_worker)(deferred import inside method)_run_atspi(op, **kwargs):apply_async().get(timeout=30)+ IPC death catch (EOFError/BrokenPipeError/ConnectionResetError/OSError errno∈{32,104}) + 1 retry_teardown_atspi_pool(): close → join 5s → terminate → join 2s → SIGKILL → join 1s.multiprocessing.pool.Pool.joinlacks atimeoutkwarg, so athreading.Threadwrapper is used.__del__cleanup + explicit invocation insession_stoptests/integration/test_atspi_pool.py: 6 lifecycle tests (cold start ≤1.5s, warm <200ms, external SIGKILL recovery, init failure, terminate within 7s with hung worker, no zombies)Performance
Verify
uv run pytest -m kwin tests/integration/test_atspi_pool.py # → 6 passedSeries: Stacked on top of PR 2. Base =
overhaul/pr2-dbus.