Skip to content

Don't assume all S_IFREG files are seekable#120736

Merged
rzikm merged 2 commits into
mainfrom
120649-lseek-azl3
Oct 15, 2025
Merged

Don't assume all S_IFREG files are seekable#120736
rzikm merged 2 commits into
mainfrom
120649-lseek-azl3

Conversation

@rzikm

@rzikm rzikm commented Oct 15, 2025

Copy link
Copy Markdown
Member

Fixes #120577.

It seems that special files like /proc/net/route are not seekable on AzureLinux 3, which trips an assert in SafeFileHandle.Init.
This PR excludes 0-length files (including all pseudofiles) from the assert to unblock CI.

Original outdated description

Since these files report 0 length in fstat, we can relax the seekabilit assumption to only files with Size > 0.

In practice, most file reads are done via RandomAccess.ReadAtOffset and we keep our own seek offset in the SafeFileHandle internally, so seekability does not matter much (the affected unit tests don't fail in release builds), but it breaks in e.g. following scenario where access to SafeFileHandle property would attempt to synchronize the position with the underlying OS handle:

FileStream file = new FileStream("/proc/net/route", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 10);

System.Console.WriteLine($"Position: {file.Position}, CanSeek: {file.CanSeek}, Length: {file.Length}");

file.Position = 1;

System.Console.WriteLine($"SafeFileHandle: {file.SafeFileHandle}");

This would produce:

Position: 0, CanSeek: True, Length: 0

      System.IO.IOException : Illegal seek : '/proc/net/route'
      Stack Trace:
        /_/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs(104,0): at System.IO.Strategies.OSFileStreamStrategy.get_SafeFileHandle()
...

Copilot AI review requested due to automatic review settings October 15, 2025 08:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue where regular files with S_IFREG type were incorrectly assumed to be seekable on all systems. The fix specifically addresses a problem on AzureLinux 3 where special files like /proc/net/route are not seekable despite having the S_IFREG file type, causing assertions to fail in SafeFileHandle.Init.

  • Adds a size check to only assume seekability for regular files with size > 0
  • Preserves existing behavior for normal files while avoiding assumptions about zero-length special files

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

@rzikm

rzikm commented Oct 15, 2025

Copy link
Copy Markdown
Member Author

cc @ManickaP

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is correct, but I would prefer to just modify the assertion. PTAL at my comment for full picture.

cc @tmds

@rzikm rzikm force-pushed the 120649-lseek-azl3 branch from cbf483b to 2254d98 Compare October 15, 2025 10:58

@ManickaP ManickaP left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, thanks!

@rzikm

rzikm commented Oct 15, 2025

Copy link
Copy Markdown
Member Author

Filed #120741 for followup on better CanSeek detection.

@rzikm rzikm enabled auto-merge (squash) October 15, 2025 11:15
@rzikm rzikm merged commit 8d7e299 into main Oct 15, 2025
140 of 142 checks passed
@agocke agocke deleted the 120649-lseek-azl3 branch October 15, 2025 17:08
@rzikm

rzikm commented Oct 17, 2025

Copy link
Copy Markdown
Member Author

/backport to release/9.0-staging

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/18584668226

@rzikm

rzikm commented Oct 17, 2025

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0: https://github.com/dotnet/runtime/actions/runs/18584672863

@github-actions github-actions Bot unlocked this conversation Dec 4, 2025
@github-actions

github-actions Bot commented Dec 4, 2025

Copy link
Copy Markdown
Contributor

Started backporting to release/8. (link to workflow run)

@rzikm

rzikm commented Dec 4, 2025

Copy link
Copy Markdown
Member Author

/backport to release/8.0

@github-actions

github-actions Bot commented Dec 4, 2025

Copy link
Copy Markdown
Contributor

Started backporting to release/8.0 (link to workflow run)

@github-actions

github-actions Bot commented Dec 4, 2025

Copy link
Copy Markdown
Contributor

@rzikm an error occurred while backporting to release/8.. See the workflow output for details.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Dec 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assert Interop.Sys.LSeek(this, 0, Interop.Sys.SeekWhence.SEEK_CUR) >= 0 in System.Net.XXX tests

5 participants