Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(roles): read a task kept in a map on app state as fire-and-forget (#3428)

Raghav Chamadiya22350efb8f1f
diff --git a/docs/layers/PERFORMANCE.md b/docs/layers/PERFORMANCE.md
index e68e0be8a..55dd69ec0 100644
--- a/docs/layers/PERFORMANCE.md
+++ b/docs/layers/PERFORMANCE.md
@@ -58,7 +58,7 @@ The default queue holds production opportunities in the `plan_ready` and
`advisory` states. They are ranked by cost first; state only breaks ties.
Only opportunities whose cost is measured are in it. A loop whose size nothing measured is `cost_proof = unproven`, never leads, and shows only with the `proof=unproven` filter.
Each opportunity also says what runs its loop (`execution_role`: request, event consumer, scheduled job, startup, CLI, UI, tooling, test or unknown), and loops run per request rank above the rest. Roles are read for Python and TypeScript/JavaScript; other languages show `unknown`. The `role` filter lists any one role, or `all`.
-Work a request hands to `asyncio.create_task` and never waits for is a background job, not part of the request. An opportunity takes its role and loop size from the same live call sites, so a call site switched off by a constant flag or already chunked never leads it.
+Work a request hands to `asyncio.create_task` and never waits for is a background job, not part of the request, even when the task is kept in a map on shared app state. An opportunity takes its role and loop size from the same live call sites, so a call site switched off by a constant flag or already chunked never leads it.
Everything else is counted by reason, not hidden:
diff --git a/packages/core/src/repowise/core/ingestion/python_tasks.py b/packages/core/src/repowise/core/ingestion/python_tasks.py
index 3283917f6..666e559da 100644
--- a/packages/core/src/repowise/core/ingestion/python_tasks.py
+++ b/packages/core/src/repowise/core/ingestion/python_tasks.py
@@ -4,7 +4,8 @@
it is not garbage collected, runs beside its caller: the caller does not pay
for it and does not repeat it. Anything less certain reads as the caller's own
work: a task awaited, gathered, returned or passed on; a container of tasks
-used for anything but adding and removing; a scheduler other than ``asyncio``
+used for anything but adding and removing (publishing it onto an attribute or
+testing it against ``None`` reads no task); a scheduler other than ``asyncio``
itself (a ``TaskGroup`` the caller waits for looks the same as one it does not).
Read within one function and matched by name, so the answer errs towards
@@ -112,13 +113,16 @@ def _holder(scheduler: Node, src: str) -> tuple[str, str | None]:
def _uses(scope: Node, name: str, src: str) -> Iterator[Node]:
- """Every use of *name* in *scope* except a plain rebinding of it."""
+ """Every use of *name* in *scope* except a plain rebinding of it, and a
+ field that merely shares its name (``state.tasks`` is not ``tasks``)."""
for node in _walk(scope):
if node.type not in ("identifier", "attribute") or node_text(node, src) != name:
continue
parent = node.parent
if parent is not None and parent.type == "assignment" and parent.child_by_field_name("left") == node:
continue
+ if parent is not None and parent.type == "attribute" and parent.child_by_field_name("attribute") == node:
+ continue
yield node
@@ -171,10 +175,30 @@ def _stored_into(node: Node) -> bool:
)
+def _published_or_tested(node: Node) -> bool:
+ """``state.tasks = node`` or ``node is None``: neither reads a task.
+
+ Publishing a local container onto an attribute is the same as keeping the
+ tasks in that attribute, which adding to it already is.
+ """
+ parent = node.parent
+ if parent is None:
+ return False
+ if parent.type == "assignment":
+ left = parent.child_by_field_name("left")
+ return parent.child_by_field_name("right") == node and left is not None and left.type == "attribute"
+ return (
+ parent.type == "comparison_operator"
+ and len(parent.children) == 3
+ and parent.children[1].type in ("is", "is not")
+ and parent.children[2].type == "none"
+ )
+
+
def _only_kept(scope: Node, container: str, src: str) -> bool:
"""Whether *container* is only added to and removed from in *scope*."""
return all(
- _method_on(node, _KEEPS, src) or _stored_into(node)
+ _method_on(node, _KEEPS, src) or _stored_into(node) or _published_or_tested(node)
for node in _uses(scope, container, src)
)
diff --git a/tests/unit/health/test_perf_spawned_and_mixed_groups.py b/tests/unit/health/test_perf_spawned_and_mixed_groups.py
index d59155de5..fdb4db721 100644
--- a/tests/unit/health/test_perf_spawned_and_mixed_groups.py
+++ b/tests/unit/health/test_perf_spawned_and_mixed_groups.py
@@ -107,6 +107,37 @@ class Runner:
async def step(x):
return x
+
+
+def launch(state, job_id):
+ task = asyncio.create_task(step(job_id))
+ jobs = getattr(state, "jobs", None)
+ if jobs is None:
+ jobs = {}
+ state.jobs = jobs
+ jobs[job_id] = task
+
+
+def launch_checked(job_id):
+ task = asyncio.create_task(step(job_id))
+ if _tasks is None:
+ return
+ _tasks.add(task)
+
+
+def launch_aliased(job_id):
+ task = asyncio.create_task(step(job_id))
+ jobs = set()
+ jobs.add(task)
+ pending = jobs
+ return pending
+
+
+def launch_and_hand_back(state, job_id):
+ task = asyncio.create_task(step(job_id))
+ jobs = {}
+ jobs[job_id] = task
+ state.drain(jobs)
"""
@@ -153,6 +184,13 @@ def test_only_a_task_nobody_waits_for_is_spawned(tmp_path: Path) -> None:
assert calls[("iterated", "step")] is False
assert calls[("returned", "step")] is False
assert calls[("joined", "step")] is False
+ # Published onto shared state and tested against None: no task is read.
+ assert calls[("launch", "step")] is True
+ assert calls[("launch_checked", "step")] is True
+ # Aliased to a plain name, the container may be read through the alias.
+ assert calls[("launch_aliased", "step")] is False
+ # Handed to a call that may wait for it: the caller's work.
+ assert calls[("launch_and_hand_back", "step")] is False
def test_a_spawned_call_site_never_resolves_as_a_call_its_caller_makes(tmp_path: Path) -> None: