kernel: make select handle file descriptors through a descriptor table shared with sockets (CP2077) #1765

Closed
github-bot wants to merge 2 commits from gh-pr/1765/head into gh-pr/1765/base
Owner

@⁠ArtyomZabroda on GitHub · 2026-10-20 16:37 UTC · original
ArtyomZabroda:pr/descriptor-table-main → main · +225 −115 · 3 files · 2 commits · closed without merging

Fixes #182.

AI use:
I used the claude code to find the source of the problem, made an initial implementation myself, and used claude to refine it. PR description was written by claude.

Before: select on a descriptor that is an open file or device, such as /dev/urandom, failed with EBADF, because Net::Select only knew sockets. Cyberpunk 2077 gathers entropy the way OpenSSL's RAND_poll does: open /dev/urandom, select with a 10 ms timeout, and read only if it is ready. The failed select left it with no entropy, so it called sceKernelDebugRaiseExceptionOnReleaseMode and returned from main. The guest exit then freed the module code while redHTTPWorker was still running, and that thread crashed executing at eboot+0x1ddc93d.

After: select reports open files as ready for reading, writing and error conditions, as POSIX specifies for regular files. When a file is already ready, it does not wait on the sockets in the same set.

To tell a file from a socket, select needs every number to have one owner. Until now files and sockets were numbered separately: files from 3 upward, sockets from 128 to 1023. Once a game had more than 125 files open, the same number could belong to both.

How:

  • ReserveDescriptor, ReleaseDescriptor and GetDescriptorKind in kernel/fileSystem keep one table of numbers for files and sockets. Both take the lowest free number after the standard streams. Sockets stay below 1024 (FD_SETSIZE).
  • Select, Read, Write, Close, Preadv, Writev and Ftruncate check the kind of the descriptor instead of calling Net::IsSocket.
  • Fcntl, Fstat and Lseek no longer fail with EBADF on a socket: F_GETFD/F_SETFD succeed, Fstat reports S_IFSOCK, Lseek fails with ESPIPE.

The first commit only applies clang-format to the touched files. The second commit holds the change.

Before:
Снимок экрана 2026-10-20 180624
After:
Снимок экрана 2026-10-20 192913

**@⁠ArtyomZabroda** on GitHub · 2026-10-20 16:37 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1765) `ArtyomZabroda:pr/descriptor-table-main` → `main` · +225 −115 · 3 files · 2 commits · closed without merging Fixes #182. AI use: I used the claude code to find the source of the problem, made an initial implementation myself, and used claude to refine it. PR description was written by claude. Before: `select` on a descriptor that is an open file or device, such as `/dev/urandom`, failed with `EBADF`, because `Net::Select` only knew sockets. Cyberpunk 2077 gathers entropy the way OpenSSL's `RAND_poll` does: open `/dev/urandom`, `select` with a 10 ms timeout, and `read` only if it is ready. The failed `select` left it with no entropy, so it called `sceKernelDebugRaiseExceptionOnReleaseMode` and returned from `main`. The guest `exit` then freed the module code while `redHTTPWorker` was still running, and that thread crashed executing at eboot+0x1ddc93d. After: `select` reports open files as ready for reading, writing and error conditions, as POSIX specifies for regular files. When a file is already ready, it does not wait on the sockets in the same set. To tell a file from a socket, `select` needs every number to have one owner. Until now files and sockets were numbered separately: files from 3 upward, sockets from 128 to 1023. Once a game had more than 125 files open, the same number could belong to both. How: - `ReserveDescriptor`, `ReleaseDescriptor` and `GetDescriptorKind` in `kernel/fileSystem` keep one table of numbers for files and sockets. Both take the lowest free number after the standard streams. Sockets stay below 1024 (`FD_SETSIZE`). - `Select`, `Read`, `Write`, `Close`, `Preadv`, `Writev` and `Ftruncate` check the kind of the descriptor instead of calling `Net::IsSocket`. - Fcntl, Fstat and Lseek no longer fail with EBADF on a socket: F_GETFD/F_SETFD succeed, Fstat reports S_IFSOCK, Lseek fails with ESPIPE. The first commit only applies clang-format to the touched files. The second commit holds the change. Before: <img width="1109" height="619" alt="Снимок экрана 2026-10-20 180624" src="https://github.com/user-attachments/assets/00c12778-a690-4f8f-9c94-7d3d981eb944" /> After: <img width="1109" height="617" alt="Снимок экрана 2026-10-20 192913" src="https://github.com/user-attachments/assets/249f985f-287c-40e5-81e2-54424b0888a3" />
Whitespace, alignment and line wrapping only; no code changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FafLdpHW3ksyHuP7fthVCf
Select reports open regular files as ready instead of failing on them.
Files and sockets now share one descriptor table, so a number belongs to
exactly one of them: ReserveDescriptor, ReleaseDescriptor and
GetDescriptorKind keep one owner per number, both take the lowest free
number after the standard streams, and functions taking a descriptor
dispatch on its kind. Select, Read, Write, Close and the other kernel file
functions use it; Fcntl, Fstat and Lseek now answer for sockets
(F_GETFD/F_SETFD, S_IFSOCK, ESPIPE) instead of EBADF.

Also fixes the case of the kernel/fileSystem.h include in network.cpp,
which broke case-sensitive builds.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FafLdpHW3ksyHuP7fthVCf
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:05:10 +00:00
github-bot closed this pull request 2026-11-26 10:05:10 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-20 16:43 UTC · original · edited 2026-10-21 13:24 UTC

Review in Change Stack →

📝 Walkthrough

File and socket descriptors now share a descriptor-kind registry. File-system operations use it to identify sockets and apply socket-specific behavior. Network socket allocation and release use the shared table. Select treats regular files and directories as immediately ready while continuing to poll sockets.

Suggested reviewers: nmzik

Priority: ➖ Normal

Change: Bug fix

Merge Risk: 🟡 Moderate · up to da932

Descriptor-free timed waits can return immediately and cause guest callers to spin. Socket descriptor flags also cannot be read back after being set. Restore the timeout behavior before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 2.70% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 37 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: shared descriptor handling for files and sockets so select can process file descriptors.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The pull request description directly explains the shared descriptor table, socket and file handling changes, and the Cyberpunk 2077 select failure addressed by the changeset.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-20 16:43 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1765#issuecomment-6296105820) · edited 2026-10-21 13:24 UTC <!-- 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/1765?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-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- walkthrough_start --> <details> <summary><strong>📝 Walkthrough</strong></summary> <dl> <dd> File and socket descriptors now share a descriptor-kind registry. File-system operations use it to identify sockets and apply socket-specific behavior. Network socket allocation and release use the shared table. `Select` treats regular files and directories as immediately ready while continuing to poll sockets. **Suggested reviewers:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"da932d24d47b13bf910b6640ee26aa2d583525a6" --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong>🟡 Moderate</strong> · up to <code>da932</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🟡 Moderate** · up to `da932` --> <!-- final_review_risk_coverage:{"sourceCommitId":"da932d24d47b13bf910b6640ee26aa2d583525a6","coveredCommitId":"da932d24d47b13bf910b6640ee26aa2d583525a6","kind":"reviewed"} --> Descriptor-free timed waits can return immediately and cause guest callers to spin. Socket descriptor flags also cannot be read back after being set. Restore the timeout behavior before merging. <!-- 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 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 warning</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | Docstring coverage is 2.70% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 37 functions across 3 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><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> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly and concisely describes the main change: shared descriptor handling for files and sockets so select can process file descriptors. | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The pull request description directly explains the shared descriptor table, socket and file handling changes, and the Cyberpunk 2077 select failure addressed by the changeset. | </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1765)! 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> - [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> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
@ -605,0 +656,4 @@
case 1: return 0; // F_GETFD
case 2: return OK; // F_SETFD
default: return KERNEL_ERROR_EINVAL;
}
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-20 16:43 UTC · original

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Retain socket descriptor flags for F_GETFD.

If a guest calls F_SETFD with FD_CLOEXEC on a socket, the next F_GETFD returns zero. This breaks the read-after-write contract even though the guest never calls exec. Store the flag with the socket descriptor and return its current value from F_GETFD.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-20 16:43 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1765#discussion_r4327621688) **🎯 Functional Correctness** | **🟡 Minor** | **⚡ Quick win** **Retain socket descriptor flags for `F_GETFD`.** If a guest calls `F_SETFD` with `FD_CLOEXEC` on a socket, the next `F_GETFD` returns zero. This breaks the read-after-write contract even though the guest never calls `exec`. Store the flag with the socket descriptor and return its current value from `F_GETFD`. <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:c745f32ff1080d3ef00f5782 --> <!-- This is an auto-generated comment by CodeRabbit -->
@ -2645,3 +2662,1 @@
} else
#endif
{
if (!descriptors.empty()) {
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-20 16:43 UTC · original

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Preserve timeout waits when no descriptors are requested.

On Linux, Select(0, nullptr, nullptr, nullptr, timeout) skips SelectSockets and returns immediately. A caller using select as a timed wait can then spin instead of waiting. Keep the native empty-set select path on non-Windows hosts when file_descriptors is empty.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-20 16:43 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1765#discussion_r4327621697) **🩺 Stability & Availability** | **🟠 Major** | **⚡ Quick win** **Preserve timeout waits when no descriptors are requested.** On Linux, `Select(0, nullptr, nullptr, nullptr, timeout)` skips `SelectSockets` and returns immediately. A caller using `select` as a timed wait can then spin instead of waiting. Keep the native empty-set `select` path on non-Windows hosts when `file_descriptors` is empty. <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:f090f273e44aee6e06a65cb0 --> <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-20 16:43 UTC · original · reviewed

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f88c4a95-e37d-4253-9d6e-95180783627d
📥 Commits

Reviewing files that changed from the base of the PR and between 7b7f9a70c6 and da932d24d4.

📒 Files selected for processing: 3
  • src/kernel/fileSystem.cpp
  • src/kernel/fileSystem.h
  • src/libs/network.cpp

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

**@⁠coderabbitai[bot]** on GitHub · 2026-10-20 16:43 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1765#pullrequestreview-5588410816) · **reviewed** **Actionable comments posted: 2** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `f88c4a95-e37d-4253-9d6e-95180783627d` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 7b7f9a70c652afd6ec8ea8599884d9b4b3abc9f3 and da932d24d47b13bf910b6640ee26aa2d583525a6. </details> <details> <summary>📒 Files selected for processing: 3</summary> * `src/kernel/fileSystem.cpp` * `src/kernel/fileSystem.h` * `src/libs/network.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=b00ffc14-9439-4df0-bf46-750ea603c1c1 attempt=28356914-e9b8-47de-a19e-071e653ac315 batch=1/1 -->
Author
Owner

@⁠Nmzik on GitHub · 2026-10-22 20:12 UTC · original

@⁠ArtyomZabroda Credited Thanks!

**@⁠Nmzik** on GitHub · 2026-10-22 20:12 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1765#issuecomment-6350289573) @⁠ArtyomZabroda Credited Thanks!

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.