Skip to content

Conversation

rzikm
Copy link
Member

@rzikm rzikm commented Oct 15, 2025

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 Copilot AI review requested due to automatic review settings October 15, 2025 08:32
Copy link
Contributor

@Copilot Copilot AI left a comment

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

Copy link
Contributor

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

@rzikm
Copy link
Member Author

rzikm commented Oct 15, 2025

cc @ManickaP

Copy link
Member

@adamsitnik adamsitnik left a comment

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
Copy link
Member

@ManickaP ManickaP left a comment

Choose a reason for hiding this comment

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

Great catch, thanks!

@rzikm
Copy link
Member Author

rzikm commented Oct 15, 2025

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
Copy link
Member Author

rzikm commented Oct 17, 2025

/backport to release/9.0-staging

Copy link
Contributor

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

@rzikm
Copy link
Member Author

rzikm commented Oct 17, 2025

/backport to release/10.0

Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

4 participants