Ensure Environment.ExitCode works correctly after app domain unloaded.#10842
Ensure Environment.ExitCode works correctly after app domain unloaded.#10842jkotas merged 5 commits intodotnet:masterfrom mazong1123:fix-6206
Conversation
This PR addresses 2 problems of Environment.ExitCode: 1. Can't get correct exit code of main function. 2. Can't set %errorlevel%. Details can be found on #6206 Fix #6206
|
@jkotas Could you please review this PR? Thanks. |
|
@dotnet-bot test Tizen armel Cross Release Build please |
|
@dotnet-bot test OSX10.12 x64 Checked Build and Test please |
| CORECLR_HOSTING_API(coreclr_shutdown, | ||
| void* hostHandle, | ||
| unsigned int domainId); | ||
| unsigned int domainId, |
There was a problem hiding this comment.
I don't think we can change the signature of the existing hosting API. There are other Microsoft projects like dotnet cli and possibly also community projects using the current APIs and this would break them. It seems that we need to create a new version of coreclr_shutdown with the added latchedExitCode and keep the old one unchanged. Named e.g. coreclr_shutdown_2.
There was a problem hiding this comment.
@janvorli Should I do the same change on ICLRRuntimeHost::UnloadAppDomain (keep the old one, add a new ICLRRuntimeHost::UnloadAppDomain2)? I found somewhere out of unixinterface.cpp calling this method as well so I have to add a default value to pLatchedExitCode. If this interface is used by other projects we probably have to keep the old signature.
There was a problem hiding this comment.
Right, you cannot modify the ICLRRuntimeHost methods either. This is a COM interface and by COM rules, you need to add a new version of the whole interface, called ICLRRuntimeHost4 (there already is ICLRRuntimeHost3) and put the new method on it.
There was a problem hiding this comment.
@janvorli I went through the code from coreclr initialization to shutdown. It seems like we need more efforts to address this issue. Therefore I just write down the design here before diving to the code. Please correct me if anything is incorrect.
Current process of executing a managed assembly:
- In
ExecuteManagedAssembly,coreclr_initializeis called to initialize thehostHandle. - in
coreclr_initialize, aCorHost2object created, who implemented interfaceICLRRuntimeHost2, and a pointer toICLRRuntimeHost2is assigned tohostHandle. - In
ExecuteManagedAssembly,coreclr_execute_assemblyis called withhostHandlepassed in. - In
coreclr_execute_assembly,hostHandleis cast toICLRRuntimeHost2. - In
ExecuteManagedAssembly,coreclr_shutdownis called withhostHandlepassed in. - In
coreclr_shutdown,hostHandleis cast toICLRRuntimeHost2.
To fix the Environment.ExitCode issue while keeping ICLRRuntimeHost2 not being changed, I'm going to:
- Create a new COM interface
ICLRRuntimeHost4which introduces a newUnloadAppDomainmethod withlatchedExitCodeas its last parameter. - Create a
CorHost4class which implementsICLRRuntimeHost4. - Create
coreclr_initialize_2,coreclr_execute_assembly_2andcoreclr_shutdown_2functions. They have same logic ascoreclr_initialize,coreclr_execute_assemblyandcoreclr_shutdownbut useCorHost4andICLRRuntimeHost4instead.
There was a problem hiding this comment.
You should not need to create CorHost4 . The new interface can be implemented by CorHost2 just fine. Also, you should not need to create coreclr_initialize_2 or coreclr_execute_assembly_2. coreclr_shutdown_2 is the only one needed.
src/dlls/mscoree/unixinterface.cpp
Outdated
| unsigned int domainId, | ||
| int* latchedExitCode) | ||
| { | ||
| ICLRRuntimeHost4* hostHandleV4 = nullptr; |
There was a problem hiding this comment.
Instead of this query interface here, could you please change all references in this file from ICLRRuntimeHost2 to ICLRRuntimeHost4?
|
@dotnet-bot test Tizen armel Cross Debug Build please |
src/vm/assembly.cpp
Outdated
| END_ENTRYPOINT_THROWS; | ||
|
|
||
| // Sync the latched exit code with the return value of main function. | ||
| SetLatchedExitCode(iRetVal); |
There was a problem hiding this comment.
Is this really needed?
It will incorrectly overwrite latched exit code for non-void returning functions.
https://github.com/mazong1123/coreclr/blob/0c46640307dab1d8359f4e3b856a4390d9a6389d/src/vm/assembly.cpp#L1854 should take care of it for non-void returning main.
There was a problem hiding this comment.
The condition in following line will be false so that L1854 won't be executed at least for this example :
https://github.com/mazong1123/coreclr/blob/0c46640307dab1d8359f4e3b856a4390d9a6389d/src/vm/assembly.cpp#L1852
I don't know why we're checking the stringArgs here so I have to sync the latched exit code outside. Seems like we should remove the check. Do you have any idea about this line of code?
There was a problem hiding this comment.
Seems like we should remove the check
Agree.
Could you please also verify that the following program will have exit code 123?
using System;
class My {
static void Main() {
Environment.ExitCode = 123;
}
}
There was a problem hiding this comment.
I just ran the sample program and echo $? gave me 123. Note I've removed the check and my sync code before testing. So it works.
|
BTW: Once this goes through, we will also need to update the nuget aware dotnet host (https://github.com/dotnet/core-setup/blob/master/src/corehost/cli/coreclr.cpp) to use the new method when it is available. |
|
@jkotas Do we need a shutdown2 method instead of updating the method shutdown? I guess we need a coreclr_shutdown_2_fn as well? |
Updating shutdown should do the trick.
Agree. |
|
@jkotas I've fired a new issue here: https://github.com/dotnet/core-setup/issues/2050 I'll work on that once this PR merged. |
|
I have just noticed that we have not unified the Windows and Unix corerun yet, and the Windows one still goes through the interface. Could you please also update these to use ICLRRuntimeHost4 ? https://github.com/dotnet/coreclr/blob/master/src/coreclr/hosts/corerun/corerun.cpp |
|
@jkotas I created a new issue #10960 to track this change. I'll submit a new PR accordingly. Sorry for missing the Windows change. |
) * Change ICLRRuntimeHost2 to ICLRRuntimeHost4 for Windows corerun. Since we've introduced ICLRRuntimeHost4 in PR #10842 for Unix, we apply it for Windows corerun as well. Fix #10960
This PR addresses 2 problems of Environment.ExitCode:
Details can be found on #6206
Fix #6206