tests: read a guest thread's host state through Mach on macOS (#2487) #2511

Open
github-bot wants to merge 1 commit from github-bot/gh-2511 into main AGit
Owner

@⁠mitsa-ng on GitHub · 2026-11-22 09:49 UTC · original
mitsa-ng:tests-macos-wait-until-sleeping → main · +49 −9 · 1 files · 1 commits · open

Fixes #2487.

Problem

Since db7035c3, kernel_file_system fails on macOS before reaching the checks covered by #1770: WaitUntilSleeping() decides a thread is blocked by reading /proc/self/task/<tid>/stat, which only exists on Linux, so on macOS the predicate never becomes true and the mutex hand-off section gives up.

Change

Implements the fix proposed by @⁠rafalohaki in the issue: on macOS, find the thread through task_threads() + THREAD_IDENTIFIER_INFO and read TH_STATE_WAITING from THREAD_BASIC_INFO. The Linux path is unchanged.

Evidence

On Apple M2 (Rosetta 2, current main at 61331721) the test failed at the mutex hand-off before and now passes it, stopping exactly at the PTHREAD_CANCELED check that #1770 covers, matching the issue's prediction. Green on Windows/Linux/macOS CI on my fork (run 44985636788).

Written with AI assistance (ZCode); built and tested by the author.

**@⁠mitsa-ng** on GitHub · 2026-11-22 09:49 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2511) `mitsa-ng:tests-macos-wait-until-sleeping` → `main` · +49 −9 · 1 files · 1 commits · open Fixes #2487. ## Problem Since db7035c3, `kernel_file_system` fails on macOS before reaching the checks covered by #1770: `WaitUntilSleeping()` decides a thread is blocked by reading `/proc/self/task/<tid>/stat`, which only exists on Linux, so on macOS the predicate never becomes true and the mutex hand-off section gives up. ## Change Implements the fix proposed by @⁠rafalohaki in the issue: on macOS, find the thread through `task_threads()` + `THREAD_IDENTIFIER_INFO` and read `TH_STATE_WAITING` from `THREAD_BASIC_INFO`. The Linux path is unchanged. ## Evidence On Apple M2 (Rosetta 2, current main at 61331721) the test failed at the mutex hand-off before and now passes it, stopping exactly at the `PTHREAD_CANCELED` check that #1770 covers, matching the issue's prediction. Green on Windows/Linux/macOS CI on my fork ([run 44985636788](https://github.com/mitsa-ng/KytyPS5/actions/runs/44985636788)). Written with AI assistance (ZCode); built and tested by the author.
WaitUntilSleeping decided a thread was blocked by reading
/proc/self/task/<tid>/stat, which only exists on Linux; on macOS the
predicate never became true and the mutex hand-off section gave up.
Find the thread with task_threads() and read TH_STATE_WAITING instead;
the Linux path is unchanged.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:21:41 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-22 09:52 UTC · original

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

On Apple platforms, WaitUntilSleeping now polls Mach thread state to detect when a guest thread’s host thread is waiting. The new helper matches the host thread ID to a Mach thread and releases the thread ports and allocated thread array. On other platforms, the existing /proc polling remains in use.

Suggested reviewers: nmzik

Change: Other · Severity of issue fixed: Low

Merge Risk: Minimal · up to 5fa7e

This change only affects how a test detects a blocked guest thread on macOS. Production behavior is untouched and the Linux path is unchanged. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a macOS-specific path in the test that reads a guest thread's host state through Mach APIs instead of /proc. It also references the issue number. It is specific and ma…
Description check Passed The description states the problem (WaitUntilSleeping reads /proc/self/task//stat, which fails on macOS), the proposed change (task_threads, THREAD_IDENTIFIER_INFO, TH_STATE_WAITING on macOS with…
Linked Issues check Passed Directly linked issue #2487 requires that WaitUntilSleeping() detect a blocked guest thread on macOS, so that kernel_file_system passes the mutex hand-off section. At the reviewed head, tests/KernelFi…
Out of Scope Changes check Passed The change set, as summarized (+49/-9), touches only tests/KernelFileSystemTests.cpp. Every change is in the macOS host-thread wait path: the conditional <mach/mach.h> include, the IsHostThreadWaiting…
Full details: Title check

Explanation

The title names the main change: a macOS-specific path in the test that reads a guest thread's host state through Mach APIs instead of /proc. It also references the issue number. It is specific and matches the changeset summary.

Full details: Description check

Explanation

The description states the problem (WaitUntilSleeping reads /proc/self/task/<tid>/stat, which fails on macOS), the proposed change (task_threads, THREAD_IDENTIFIER_INFO, TH_STATE_WAITING on macOS with Linux path unchanged), and testing evidence. It is directly related to the changeset.

Full details: Linked Issues check

Explanation

Directly linked issue #2487 requires that WaitUntilSleeping() detect a blocked guest thread on macOS, so that kernel_file_system passes the mutex hand-off section. At the reviewed head, tests/KernelFileSystemTests.cpp adds an Apple-only IsHostThreadWaiting() helper. It calls task_threads(), matches THREAD_IDENTIFIER_INFO.thread_id against PthreadGetHostThreadId(), and returns true when THREAD_BASIC_INFO.run_state == TH_STATE_WAITING. WaitUntilSleeping() uses this helper under APPLE and keeps the /proc/self/task/<tid>/stat path under #else. This matches the issue's proposed fix. The author reports, on Apple M2 under Rosetta 2, that the test now passes the hand-off and stops at the PTHREAD_CANCELED check. That check belongs to #1770, which is not part of this PR. The issue's five-run all-pass result needs #1770 and #2269, which this PR does not include. Those fixes are outside this PR's scope. The macOS run is author-reported. Repository evidence confirms the code only. The whole-PR diff could not be read, so this assessment relies on the head file content and the PR summary.

Full details: Out of Scope Changes check

Explanation

The change set, as summarized (+49/-9), touches only tests/KernelFileSystemTests.cpp. Every change is in the macOS host-thread wait path: the conditional <mach/mach.h> include, the IsHostThreadWaiting() helper, and the #if branch in WaitUntilSleeping(). The Linux path is unchanged. No unrelated code or other test is modified in the reviewed summary. The whole-PR diff could not be read, so file-level scope relies on the summary. Minor style note only: the new Apple block uses tabs, while the surrounding file uses two-space indentation. This does not affect scope.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 6133172164 and 5fa7e3e71e.

Files selected for processing: 1

  • tests/KernelFileSystemTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-11-22 09:52 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2511#issuecomment-7077427488) <!-- coderabbit-ai-generated:v1 --> <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/2511?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg" alt="Explain Change →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- recent_review_status_start --> No actionable comments were generated in the recent review. <!-- recent_review_status_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> <br> On Apple platforms, `WaitUntilSleeping` now polls Mach thread state to detect when a guest thread’s host thread is waiting. The new helper matches the host thread ID to a Mach thread and releases the thread ports and allocated thread array. On other platforms, the existing `/proc` polling remains in use. **Suggested reviewers:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <!-- change_assessment_commit:"5fa7e3e71eb95ac83235b1715f21064af61f9a59" --> <!-- change_assessment_priority:{"version":1,"urgency":"p3","manual":false} --> **Change:** Other · **Severity of issue fixed:** Low <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · up to <code>5fa7e</code></summary> <dl> <dd> <br> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **⚪ Minimal** · up to `5fa7e` --> <!-- final_review_risk_coverage:{"sourceCommitId":"5fa7e3e71eb95ac83235b1715f21064af61f9a59","coveredCommitId":"5fa7e3e71eb95ac83235b1715f21064af61f9a59","kind":"reviewed"} --> This change only affects how a test detects a blocked guest thread on macOS. Production behavior is untouched and the Linux path is unchanged. No merge-blocking risk was found. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4</strong></summary> <dl> <dd> <br> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title names the main change: a macOS-specific path in the test that reads a guest thread's host state through Mach APIs instead of /proc. It also references the issue number. It is specific and ma… | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description states the problem (WaitUntilSleeping reads /proc/self/task/<tid>/stat, which fails on macOS), the proposed change (task_threads, THREAD_IDENTIFIER_INFO, TH_STATE_WAITING on macOS with… | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Directly linked issue `#2487` requires that WaitUntilSleeping() detect a blocked guest thread on macOS, so that kernel_file_system passes the mutex hand-off section. At the reviewed head, tests/KernelFi… | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The change set, as summarized (+49/-9), touches only tests/KernelFileSystemTests.cpp. Every change is in the macOS host-thread wait path: the conditional <mach/mach.h> include, the IsHostThreadWaiting… | <details> <summary><strong>Full details: Title check</strong></summary> <dl> <dd> <br> **Explanation** The title names the main change: a macOS-specific path in the test that reads a guest thread's host state through Mach APIs instead of /proc. It also references the issue number. It is specific and matches the changeset summary. </dd> </dl> </details> <details> <summary><strong>Full details: Description check</strong></summary> <dl> <dd> <br> **Explanation** The description states the problem (WaitUntilSleeping reads /proc/self/task/&lt;tid&gt;/stat, which fails on macOS), the proposed change (task_threads, THREAD_IDENTIFIER_INFO, TH_STATE_WAITING on macOS with Linux path unchanged), and testing evidence. It is directly related to the changeset. </dd> </dl> </details> <details> <summary><strong>Full details: Linked Issues check</strong></summary> <dl> <dd> <br> **Explanation** Directly linked issue `#2487` requires that WaitUntilSleeping() detect a blocked guest thread on macOS, so that kernel_file_system passes the mutex hand-off section. At the reviewed head, tests/KernelFileSystemTests.cpp adds an Apple-only IsHostThreadWaiting() helper. It calls task_threads(), matches THREAD_IDENTIFIER_INFO.thread_id against PthreadGetHostThreadId(), and returns true when THREAD_BASIC_INFO.run_state == TH_STATE_WAITING. WaitUntilSleeping() uses this helper under __APPLE__ and keeps the /proc/self/task/&lt;tid&gt;/stat path under `#else`. This matches the issue's proposed fix. The author reports, on Apple M2 under Rosetta 2, that the test now passes the hand-off and stops at the PTHREAD_CANCELED check. That check belongs to `#1770`, which is not part of this PR. The issue's five-run all-pass result needs `#1770` and `#2269`, which this PR does not include. Those fixes are outside this PR's scope. The macOS run is author-reported. Repository evidence confirms the code only. The whole-PR diff could not be read, so this assessment relies on the head file content and the PR summary. </dd> </dl> </details> <details> <summary><strong>Full details: Out of Scope Changes check</strong></summary> <dl> <dd> <br> **Explanation** The change set, as summarized (+49/-9), touches only tests/KernelFileSystemTests.cpp. Every change is in the macOS host-thread wait path: the conditional &lt;mach/mach.h&gt; include, the IsHostThreadWaiting() helper, and the `#if` branch in WaitUntilSleeping(). The Linux path is unchanged. No unrelated code or other test is modified in the reviewed summary. The whole-PR diff could not be read, so file-level scope relies on the summary. Minor style note only: the new Apple block uses tabs, while the surrounding file uses two-space indentation. This does not affect scope. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=2511)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>Share</strong></summary> <dl> <dd> <br> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <!-- tips_end --> <!-- recent_review_start --> <h6> <details> <summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Recent review info</strong></summary> <dl> <dd> <br> <!-- review_info_content --> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</strong></summary> <dl> <dd> <br> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/613317216495ad7c98ea0e3743cded89d4596062/.coderabbit.yaml>) - **Commit**: [6133172](<https://github.com/KytyPS5/KytyPS5/commit/613317216495ad7c98ea0e3743cded89d4596062>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `d63a2249-7db5-4507-8084-77783604e826` </dd> </dl> </details> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</strong></summary> <dl> <dd> <br> Reviewing files that changed from the base of the PR and between 613317216495ad7c98ea0e3743cded89d4596062 and 5fa7e3e71eb95ac83235b1715f21064af61f9a59. </dd> </dl> </details> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 1</strong></summary> <dl> <dd> <br> * `tests/KernelFileSystemTests.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review. </dd> </dl> </details> </h6> <!-- recent_review_end -->
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2511/head:github-bot/gh-2511'
git switch 'github-bot/gh-2511'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2511'
git switch 'github-bot/gh-2511'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2511'
git switch 'github-bot/gh-2511'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2511'
git switch 'main'
git merge --squash 'github-bot/gh-2511'
git switch 'main'
git merge --ff-only 'github-bot/gh-2511'
git switch 'main'
git merge 'github-bot/gh-2511'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.