Skip to content

Process.MainModule doesn't return the proper module in some cases #23568

Closed
dotnet/corefx
#24106

Description

@janvorli

TL;DR - the System.Diagnostics.Process.MainModule returns other module than the main one in some cases.

When trying to build CoreCLR in Alpine docker container, I have bumped into a strange issue when msbuild would fail with internal error when forking another copy of itself. strace revealed that it was calling "access" syscall with "Tools/dotnetcli/shared/Microsoft.NETCore.App/2.0.0-preview1-002111-00/System.Private.CoreLib.ni.dll" file as a parameter. Looking at where that syscall came from, it turned out it was SystemNative_ForkAndExecProcess in System.Native.so, which was called from a call chain stemming from Microsoft.Build.BackEnd.NodeProviderOutOfProcBase.LaunchNode. This function was passing in a result of the System.Diagnostics.Process.MainModule and it was returning that dll instead of the actual main module.
It turns out that the System.Diagnostics.Process.MainModule relies on the main module being the first module listed in the /proc/self/maps. But in the Alpine container, it was not the case.

I can see two ways to fix that. Either ensure that the modules ProcessManager.GetModules always returns the main module as the first one or change the System.Diagnostics.Process.MainModule to search for the main exe by looping through all the modules and comparing their FileName to the Process.GetExePath(). And probably caching the result.

I've verified that the second approach works.

Activity

  1. janvorli commented on Sep 15, 2017

    @janvorli
    MemberAuthor
  2. self-assigned this
    on Sep 17, 2017
  3. stephentoub commented on Sep 17, 2017

    @stephentoub
    Member
  4. transferred this issue fromdotnet/corefxon Jan 31, 2020
  5. added this to the 2.1.0 milestone on Jan 31, 2020
  6. ghost locked as resolved and limited conversation to collaborators on Dec 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Diagnostics.Processbugos-linux

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions