Skip to content

MetadataReader ReadFile fast path does not work with async file streams #14276

Closed

Description

@nguerrera

When a FileStream is opened with isAsync: true (.NET Core default!), the ReadFile P/Invoke code that we have in the metadata reader to read small files straight to native memory. ReadFile fails with invalid param error code because our handle was opened for overlapped IO.

For now, I'm changing the code to be more defensive and always fall back to the slow file -> managed heap -> native heap code path. (It currently throws.)

@ericstj: Can you help me figure out how to tweak FileStreamReadLightUp.cs to handle this?

Also, @tmat has already mentioned to me that this code path will slow us down on Linux and Mac. Perhaps, this is the right opportunity to define a portable FileStream.Read overload that reads to unmanaged memory?

Activity

  1. ericstj commented on Feb 23, 2015

    @ericstj
    Member

    If you are getting the handle out of FileStream you should pass all arguments to the FileStream constructor (Either UseAsync or FileOptions).

  2. nguerrera commented on Feb 23, 2015

    @nguerrera
    ContributorAuthor

    @ericstj I don't control the construction of the FileStream. The reader tries to light up and do a fast read via P/Invoke on the Stream that gets passed in already constructed.

  3. ericstj commented on Feb 23, 2015

    @ericstj
    Member

    In that case you never know if you are getting a sync or overlapped handle. The fact that the default changed is irrelevant since it could have happened before. The way to detect it is to do a ReadFile specifying null for overlapped and see if it fails with ERROR_INVALID_PARAMETER. See VerifyHandleIsSync. You could fallback to actually doing the overlapped IO rather than falling back to managed heap.

  4. self-assigned this
    on Feb 24, 2015
  5. nguerrera commented on Feb 24, 2015

    @nguerrera
    ContributorAuthor

    I realize this could have happened before, I just meant that the new default is why it was only uncovered now. I'll look into doing the overlapped IO, but this code is getting gnarly as it has to worry about running on non-Windows.

    I'd like to talk some more about having a Stream.Read into byte* with an efficient override in FileStream that just does the right thing. If we think we'll do that, I'd rather wait for that than make this light-up code any more complex.

    I'd have to benchmark to see if this is even worth it. We only go down this path for files of size <= 16KB...

  6. nguerrera commented on Dec 2, 2015

    @nguerrera
    ContributorAuthor

    dotnet/corefx#4786 tracks the BCL feature that could make all of this code go away from S.R.Metadata.

  7. karelz commented on Nov 11, 2016

    @karelz
    Member

    This looks tricky. Where to start is mentioned above.

  8. transferred this issue fromdotnet/corefxon Jan 31, 2020
  9. added this to the Future milestone on Jan 31, 2020
  10. removed
    untriagedNew issue has not been triaged by the area owner
    on Jun 29, 2020
  11. self-assigned this
    on Mar 29, 2021
  12. ghost added
    needs-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
    on Mar 29, 2021
  13. modified the milestones: Future, 6.0.0 on Mar 29, 2021
  14. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Mar 29, 2021
  15. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Mar 31, 2021
  16. ghost locked as resolved and limited conversation to collaborators on Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Reflection.Metadatabughelp wanted[up-for-grabs] Good issue for external contributorsneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

Type

No type

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions