You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "DaveT1991 (via GitHub)" <gi...@apache.org> on 2026/04/10 04:46:17 UTC
[PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
DaveT1991 opened a new pull request, #64995:
URL: https://github.com/apache/airflow/pull/64995
## Summary
Avoid a startup race in the task supervisor when starting forked task subprocesses.
`WatchedSubprocess.start()` currently wraps the freshly forked child with `psutil.Process(pid)` immediately. On some systems this can raise `psutil.NoSuchProcess` even though the child is the direct supervised process, which aborts task startup before the task instance is marked running and later shows up as a queued/failed mismatch.
This change uses a lightweight handle backed by `os.waitpid`/`os.kill` for directly forked children instead of performing an immediate `psutil` lookup, and adds a regression test to cover that startup path.
Closes #64974
## Testing
- Added regression coverage in `task-sdk/tests/task_sdk/execution_time/test_supervisor.py`
- `git diff --check`
I could not run the Python test suite locally in this environment because `python` is not installed here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #64995:
URL: https://github.com/apache/airflow/pull/64995#discussion_r3067136785
##########
task-sdk/tests/task_sdk/execution_time/test_supervisor.py:
##########
@@ -540,6 +540,38 @@ def subprocess_main():
assert "Last chance exception handler" in captured.err
assert "RuntimeError: Fake syntax error" in captured.err
+ def test_start_retries_psutil_lookup_on_no_such_process(self, client_with_ti_start, monkeypatch):
+ real_process = psutil.Process
+ call_count = 0
+
+ def flaky_process(pid=None):
+ nonlocal call_count
+ if pid is not None:
+ call_count += 1
+ if call_count == 1:
+ raise psutil.NoSuchProcess(pid=pid, msg="process PID not found")
+ return real_process(pid)
+ return real_process()
+
+ monkeypatch.setattr("airflow.sdk.execution_time.supervisor.psutil.Process", flaky_process)
+ monkeypatch.setattr("airflow.sdk.execution_time.supervisor.time.sleep", lambda _: None)
Review Comment:
The monkeypatch targets here use a dotted-string that looks like a nested attribute path ("airflow.sdk.execution_time.supervisor.psutil.Process" / ".time.sleep"). With pytest's monkeypatch, the string form imports everything up to the last dot as a module, so this will try to import `airflow.sdk.execution_time.supervisor.psutil` and `airflow.sdk.execution_time.supervisor.time` (which don’t exist) and the test will error. Patch the attributes on the already-imported supervisor module objects instead (e.g. import `airflow.sdk.execution_time.supervisor` and set `supervisor.psutil.Process` / `supervisor.time.sleep`).
##########
task-sdk/src/airflow/sdk/execution_time/supervisor.py:
##########
@@ -441,6 +441,14 @@ def exit(n: int) -> NoReturn:
exit(125)
+def _psutil_process(pid: int) -> psutil.Process:
+ try:
+ return psutil.Process(pid)
+ except psutil.NoSuchProcess:
+ time.sleep(0.1)
+ return psutil.Process(pid)
Review Comment:
`time.sleep(0.1)` is a new hard-coded retry delay. Given this module already centralizes timing knobs as constants (e.g. `MIN_HEARTBEAT_INTERVAL`, `SOCKET_CLEANUP_TIMEOUT`), consider extracting this delay (and possibly retry count/timeout) into a named constant or config-backed value so it’s easier to tune and reason about if the race needs longer retries on some platforms.
##########
task-sdk/src/airflow/sdk/execution_time/supervisor.py:
##########
@@ -441,6 +441,14 @@ def exit(n: int) -> NoReturn:
exit(125)
+def _psutil_process(pid: int) -> psutil.Process:
+ try:
+ return psutil.Process(pid)
+ except psutil.NoSuchProcess:
+ time.sleep(0.1)
+ return psutil.Process(pid)
Review Comment:
The PR description says this change replaces the immediate psutil lookup with an `os.waitpid`/`os.kill`-backed lightweight handle, but the implementation here still wraps the child PID in `psutil.Process` (with a retry). Either update the PR description to match the actual approach, or implement the described non-psutil handle so reviewers/users aren’t misled about the behavior and dependencies.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "DaveT1991 (via GitHub)" <gi...@apache.org>.
DaveT1991 commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4224056954
Thanks for the feedback @ashb. I've updated the approach based on your suggestion — instead of replacing psutil, the new version simply retries psutil.Process(pid) after a 100ms sleep when NoSuchProcess is raised, which preserves pid-reuse detection.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4222897781
> I'm going to need more evidence that this is actually a race. We used psutil because it's safer than just killing a pid.
Yes. I saw similar issues raised and similar races happening at scalenin reported - by a quick search, we likely need to look closer and look at similar issues being solved elsewhere.
But the issue is real - we have seen at least few reports from our users that in Airflow 3 there are running tasks that are reported as failed - but they are actually running in background - which would definitely match the pattern described here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4300898753
@DaveT1991 This PR has been converted to **draft** because it does not yet meet our [Pull Request quality criteria](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-quality-criteria).
**Issues found:**
- :x: **Merge conflicts**: this branch has conflicts with `main` and cannot be merged as-is. Rebase: `git fetch upstream main && git rebase upstream/main`, resolve, force-push.
- :x: **Unresolved review comments** (3 threads): please walk through each unresolved review thread. **Even if a suggestion looks incorrect or irrelevant** — and some of them will be, especially any comments left by automated reviewers like GitHub Copilot — it is still the author's responsibility to respond: apply the fix, reply in-thread with a brief explanation of why the suggestion does not apply, or resolve the thread if the feedback is no longer relevant. **Once you believe a thread is resolved — whether by pushing a fix or by explaining why the suggestion doesn't apply — please mark it as resolved yourself by clicking the 'Resolve conversation' button at the bottom of each thread.** Reviewers don't auto-close their own threads, so an addressed-but-unresolved thread reads as 'still waiting on the author' and keeps the PR from moving forward.
**What to do next:**
- Address the items above.
- Make sure static checks pass locally: `prek run --from-ref main --stage pre-commit`.
- Mark the PR as "Ready for review" when you are done.
Converting a PR to draft is **not** a rejection — it is an invitation to bring the PR up to the project's standards so that maintainer review time is spent productively. There is no rush — take your time and work at your own pace. We appreciate your contribution and are happy to wait for updates. If you have questions, feel free to ask on the [Airflow Slack](https://s.apache.org/airflow-slack).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4222914707
And yes - I agree that solution likely **could** be better if we find that this kind of race is really the root cause.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4223685077
> However I'm still skeptical this is actually what's happening. I can find no other reports anywhere saying a pid can exist without `/proc/pid/stat` existing. If this kind of race was possible we can't be the first to hit it.
I am also looking for those.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4383012325
@DaveT1991 This draft PR has been inactive for 12 days since the last triage comment and no response from the author. Closing to keep the queue clean.
You are welcome to reopen this PR when you resume work, or to open a new one addressing the issues previously raised. There is no rush — take your time.
---
_Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this [two-stage triage process](https://github.com/apache/airflow/blob/main/contributing-docs/25_maintainer_pr_triage.md#why-the-first-pass-is-automated) so that our maintainers' limited time is spent where it matters most: the conversation with you._
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4222710993
Nice. @kaxil @ashb @amoghrajesh -> that looks indeed plausible and the solution looks good as well. WDYT? That would explain a flurry of random task instance status not matching actually running tasks - there were quite a few reports about that one.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4227462176
If you resolve comments from copilot, either add a comment if it is invalid or create a commit fixing it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk closed pull request #64995: Avoid psutil lookup race when starting supervised task subprocesses
URL: https://github.com/apache/airflow/pull/64995
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4222991242
https://github.com/giampaolo/psutil/blob/v7.1.0/psutil/_pslinux.py#L1645-L1651:
```python
self._raise_if_zombie()
# /proc/PID directory may still exist, but the files within
# it may not, indicating the process is gone, see:
# https://github.com/giampaolo/psutil/issues/2418
if not os.path.exists(f"{self._procfs_path}/{pid}/stat"):
raise NoSuchProcess(pid, name) from err
raise
```
I think this is where the exception is coming from.
I wonder if this could be caused by something kube/containerd and a procfs proxy/causing a delay. I couldn't find any other reports about this
If that is the case I think I'd rather we do something like
```python
try:
proc = psutil.Process(pid)
except psutil.ProcessNotFound:
time.sleep(0.1)
proc = psutil.Process(pid)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4236429632
Note however: I am still very very skeptical this is the fix
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Avoid psutil lookup race when starting supervised task subprocesses [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on PR #64995:
URL: https://github.com/apache/airflow/pull/64995#issuecomment-4223267514
What might be worth doing is catching the error, and then when it does read the data from stdout/stderr sockets -- it's possible something is written on one those.
However even then, if the process has died it's procfs entry should stay around until something `wait()`s it!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org