Repository navigation
Fix Unix file descriptor assertion - #132136
Conversation
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
Fixes an incorrect Unix file-descriptor debug assertion that treated the mutable soft RLIMIT_NOFILE as a validity bound, and adds a Unix regression test covering dup2 + lowering RLIMIT_NOFILE + RandomAccess.Write via SafeFileHandle.
Changes:
- Removes the
_SC_OPEN_MAX-based upper-bound assertion fromToFileDescriptor. - Adds a Unix-only regression test that duplicates a file descriptor, lowers
RLIMIT_NOFILE, then writes through the duplicatedSafeFileHandle. - Wires the new test and required Unix interop into the System.Runtime filesystem test project.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/native/libs/Common/pal_utilities.h | Adjusts Unix file-descriptor assertion to avoid using a mutable soft-limit as a validity bound. |
| src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj | Adds the new Unix dup2 regression test and required RLIMIT interop file to the test compilation list. |
| src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/RandomAccess/Dup2.Unix.cs | Introduces a new Unix regression test exercising dup2 + lowered RLIMIT_NOFILE + RandomAccess.Write. |
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot please address my feedback
|
Tagging subscribers to this area: @dotnet/area-system-io |
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Addressed in Note This response was generated by Copilot. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (1)
src/native/libs/Common/pal_utilities.h:83
ToFileDescriptorUncheckedtruncatesintptr_ttoint. After removing thesysconf(_SC_OPEN_MAX)bound,ToFileDescriptorno longer asserts that the value is representable as anint, so a largefdcould silently wrap/truncate in debug builds. Consider keeping an upper-bound assertion that doesn’t depend on the mutable soft limit, e.g.fd <= INT_MAX.
inline static int ToFileDescriptor(intptr_t fd)
{
#ifndef TARGET_WASI // the valid range of file descriptors is probably INT32_MIN <= fd && fd <= INT32_MAX, the negative handles are valid for console.
assert(0 <= fd);
#endif
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot address the feedback
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Addressed in Note This response was generated by Copilot. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (2)
src/native/libs/Common/pal_utilities.h:83
ToFileDescriptorcastsintptr_ttoint. The new upper-bound assert usesfd < INT_MAX, butINT_MAXitself is representable byintand the comment above saysfd <= INT32_MAX. Using<can trip debug builds for a theoretically valid descriptor value; the check should match the cast range (<= INT_MAX).
#ifndef TARGET_WASI // the valid range of file descriptors is probably INT32_MIN <= fd && fd <= INT32_MAX, the negative handles are valid for console.
assert(0 <= fd);
assert(fd < INT_MAX);
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs:113
- The test potentially targets low file descriptors when the current soft limit is already small (e.g., 3 =>
maxAllowedFileDescriptorbecomes 2, overwriting stderr viadup2). This can break the RemoteExecutor process (pipes/std* fds) and make the test flaky. Consider raising the soft limit up to the hard limit (capped), and skip when the resulting limit is too small for a safe high descriptor.
Assert.Equal(0, Interop.Sys.GetRLimit(Interop.Sys.RlimitResources.RLIMIT_NOFILE, out Interop.Sys.RLimit limits));
limits.CurrentLimit = Math.Min(limits.CurrentLimit, MaximumFileDescriptorLimit);
Assert.InRange(limits.CurrentLimit, 2UL, (ulong)int.MaxValue);
Assert.Equal(0, Interop.Sys.SetRLimit(Interop.Sys.RlimitResources.RLIMIT_NOFILE, ref limits));
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot address my feedback
…side ifdef Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (2)
src/native/libs/Common/pal_utilities.h:85
ToFileDescriptorstill allows values < INT_MIN (whenTARGET_WASIis defined) to pass the assertions, even though the subsequent cast tointwould truncate/produce an invalid descriptor. It would be safer for the debug-time contract to assert the fullintrange before narrowing.
#ifndef TARGET_WASI // the valid range of file descriptors is probably INT32_MIN <= fd && fd <= INT32_MAX, the negative handles are valid for console.
assert(0 <= fd);
#endif
assert(fd <= INT_MAX);
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Unix.cs:113
- This test can end up dup2'ing onto fd 0/1/2 when
RLIMIT_NOFILEis very small (e.g., 2 or 3), which can break the remote test process’ stdin/stdout/stderr and make the failure mode noisy/flaky. Guard against too-small limits and skip in that case.
limits.CurrentLimit = Math.Min(limits.CurrentLimit, MaximumFileDescriptorLimit);
Assert.InRange(limits.CurrentLimit, 2UL, (ulong)int.MaxValue);
Assert.Equal(0, Interop.Sys.SetRLimit(Interop.Sys.RlimitResources.RLIMIT_NOFILE, ref limits));
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
/ba-g Helix Job Monitor timeout is unrelated |
The Unix descriptor assertion treated the mutable soft open-file limit as a validity bound.
Changes
dup2regression test that lowersRLIMIT_NOFILEafter duplicating a handle, then writes through the resultingSafeFileHandlewithRandomAccess.Write.