-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crash when trying to load an Assembly in a hostfxr environment after calling FreeConsole() #91856
Comments
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsDescriptionHi This works fine for the most part, however we noticed that Reproduction StepsThe latest nightly release of ImHex triggers this issue at launch on Windows 10 sometimes. It's not consistent but it happens often. Alternatively, a simple application which uses the CONSOLE subsystem, calls Expected behavior
Actual behaviorThe call to runtime/src/coreclr/jit/ee_il_dll.cpp Line 92 in de0ab15
Regression?No response Known WorkaroundsInterestingly enough, this issue seems to only affect Windows 10 (and potentially even older versions of Windows). It does not happen on Windows 11. Configuration
Other information
|
Does the release build of the JIT need to do this I/O initialization unconditionally during startup? It is unnecessary work unless there needs to be something printed to the console. |
Can you see what the value of
It would presumably be possible to refactor it to be initialized lazily, though there are a number of places we make use of |
This issue has been marked |
I marked it .NET 9 for now, but we can adjust that when we get a repro and/or fix. |
I guessed that there's a race condition too. Must be noted that we launch plugins on paralell to the execution. Quick summary of the situationIn this case we're under a This crash is probably caused by FreeConsole. FreeConsole call does close the standard streams. We're getting a crash trying to load hostfxr.
There are times (just like this) where a CUI/CLI application wants to detach from the console by using FreeConsole and that invalidates the streams that were used on hostfxr initialization. NoteThis is kind of a |
It would definitely need a safe check on |
So are you saying This code runs just once at start up, so a race condition seems unlikely if you can readily reproduce it. |
|
Does it mean there is no safe way to use
There's also this SO post that seems related, but doesn't give much insight: https://stackoverflow.com/questions/60833724/allocconsole-and-stdin-stdout |
If some other component in the process closes the underlying OS handle for standard output, using stdout is not going to work. That is by design. Nothing to fix there. This issue highlighted that the JIT initialization duplicates OS file handles that are not going to be used in 99.99% cases. I think it is something we should fix. OS handles and OS syscalls are not free. |
I agree that we should change it to be lazily initialized. In practice it'll fix this issue too. Still, I am curious about the underlying issue here, and how we should be detecting whether or not |
As far as I understand it, the issue is caused by FreeConsole() only closing the underlying kernel handles and not the file descriptors themselves, therefor leaving them in an undefined state. Normally this shouldn't be possible. I found this post on StackOverflow that mostly describes the issue and how to work around it, however some of the code seems to be missing and some functions aren't available to us since we're using MinGW: https://stackoverflow.com/questions/12676312/freeconsole-behaviour-on-windows-8 |
I believe that this issue was fixed in more recent Windows versions.
There is no way to reliably detect this. If the original handle was closed, the handle value could have been later reused for a different object. |
This is absolutely possible yeah. I was never able to reporduce the issue on Windows 11 but it's pretty consistent when using a Windows 10 VM.
I don't know how viable this is but maybe it would make sense to be able to pass in a stream that is used then when calling into the hostfxr interface. And just default to stdout if none is passed in. |
The optimization @jkotas suggested above will in practice fix the issue: we can avoid any I/O operations until the JIT actually needs the handle. The JIT does not need to do anything with stdout in normal scenarios -- only when certain environment variables are set. |
Avoid duplicating a handle and doing several I/O operations on the startup path. Fixes dotnet#91856 as a side effect.
Avoid duplicating a handle and doing several I/O operations on the startup path. Fixes dotnet#91856 as a side effect.
* JIT: Initialize jitstdout lazily Avoid duplicating a handle and doing several I/O operations on the startup path. Fixes #91856 as a side effect. * Fix jitShutdown * Add volatile * CSE * Ensure jitstdout() is not used eagerly * Move comment, fix printf -> jitprintf * Clean up
Avoid duplicating a handle and doing several I/O operations on the startup path. Fixes #91856 as a side effect.
Description
Hi
I'm currently experiencing a crash in one of my applications which uses the hostfxr environment to load .NET Assemblies. The application is generally a GUI application, however we use the CONSOLE subsystem to have support for a command line interface. When run from the regular explorer, the application detects this and calls
FreeConsole()
to hide the console window.This works fine for the most part, however we noticed that
FreeConsole()
apparently leaves the stdout, stderr and stdin handles in an invalid state which later on causes a crash here when the JIT tries to duplicate the stdout handle. The code there seems to do some checks already but doesn't seem to catch this problem.Reproduction Steps
The latest nightly release of ImHex triggers this issue at launch on Windows 10 sometimes. It's not consistent but it happens often.
It can be downloaded from https://imhex.download/#nightly
Alternatively, a simple application which uses the CONSOLE subsystem, calls
FreeConsole()
and then loads an Assembly through the hostfxr functions should reproduce the issue on Windows 10.Expected behavior
stdout
should simply not be used in this case instead of causing a crashActual behavior
The call to
_dup()
atruntime/src/coreclr/jit/ee_il_dll.cpp
Line 92 in de0ab15
Regression?
No response
Known Workarounds
Interestingly enough, this issue seems to only affect Windows 10 (and potentially even older versions of Windows). It does not happen on Windows 11.
Configuration
Other information
https://github.com/WerWolv/ImHex
FreeConsole()
: https://github.com/WerWolv/ImHex/blob/master/main/source/window/win_window.cpp#L238-L251The text was updated successfully, but these errors were encountered: