Skip to content

Make Process.Start have a option to change handle inheritance #13943

Description

@pdelvo

Currently if you call Process.Start internally CreateProcess is called with bInheritHandles = true (hard coded). It would be great to make it possible to change this behavior, e.g. by adding a Property to ProcessStartInfo.

Currently there is no way I know of to change this other then reimplementing System.Diagnostics.Process.

Example

If you run this application twice without exiting the first notepad instance the second instance will not be able to open the tcp port, because notepad is still running. This can be a problem for server applications that are starting child processes themself and crash, or are killed by the user before the socket can be closed.

using System.Diagnostics;
using System.Net;
using System.Net.Sockets;

class Program
{
    static void Main()
    {
        TcpListener listener = new TcpListener(IPAddress.Any, 4567);
        listener.Start();

        Process.Start(new ProcessStartInfo("notepad.exe") { UseShellExecute = false });
        //Simulate application crash without freeing resources
    }
}

Design proposal

The easiest way to make this possible is to add a new Property to ProcessStartInfo and use this in the Call to CreateProcess

namespace System.Diagnostics
{
    public partial class ProcessStartInfo
    {
        public bool InheritHandles
        {
            get;  // defaults to true
            [MinimumOSPlatform("windows7.0")]
            set;
        }
    }
}

Questions

  • Is there a very important reason why this was hardcoded like this in the first place?

Activity

  1. krwq commented on Dec 22, 2014

    @krwq
    Member

    I agree we should add this API. IMO we should also make TcpListener have option to change HandleInheritability and change the default behavior to not inherit handles if possible (at least when targeting newer version of the framework).

    In most cases we should not change InheritHandles on Process and should rather do it whenever we create handles but we should still have an option to disable this.

  2. pdelvo commented on Dec 23, 2014

    @pdelvo
    Author

    I think adding options to TcpListener/TcpClient/Socket would be a good idea. Are there other places where handle inheritance can be a problem? I was not able to create a problem like the one mentioned above by exclusivly opening a file

  3. terrajobst commented on Dec 24, 2014

    @terrajobst
    Contributor

    This seems quite reasonable. As @krwq we should probably do a pass and look into similar classes that perform handle inheritability. This helps in understanding what API we could use.

  4. krwq commented on Dec 29, 2014

    @krwq
    Member

    @pdelvo, We were able to repro this with FileStream although that one creates non-inheritable handles by default so had to override that. I believe that every library should have non-inheritable handles by default but that might be hard to change at this point because of compat reasons

  5. ellismg commented on Jan 9, 2015

    @ellismg
    Contributor

    One open question I have is what this would mean when we try to support this feature cross platform. What would it mean to say that handles are not inherited in Unix? Does "handles" here mean open file descriptors? If they are not to be inherited, does the framework have to find and close them in the child process? How does it interact with file descriptors marked FD_CLOEXEC?

  6. self-assigned this
    on Jan 9, 2015
  7. stephentoub commented on Jan 9, 2015

    @stephentoub
    Member

    I expect there are going to be a fair number of features in System.Diagnostics.Process that result in PlatformNotSupportedException; this might be one of them, if e.g. explicitly closing all fds above 2 doesn't work out or is prohibitive for some reason.

  8. terrajobst commented on Jan 15, 2015

    @terrajobst
    Contributor

    We've reviewed this proposal and don't believe it's ready yet. Please take a look at the notes.

  9. danmoseley commented on Dec 6, 2016

    @danmoseley
    Contributor
  10. Priya91 commented on Dec 7, 2016

    @Priya91
    Contributor

    @pdelvo @whoisj Would one of you be interested in working on the api proposal for this, responding to the last api review in @danmosemsft's link..

  11. whoisj commented on Dec 7, 2016

    @whoisj

    @Priya91 yeah I could do something, but likely not until after VS 2017 ships (it is an all consuming effort). If @pdelvo wants to start on something, I'd be happy to collaborate as well.

    What are the requirements for CoreFx API changes? Does the API need x-plat support, can there be Windows specific elements? Etc.

    As a side note, I'd prefer to see a method added to System.Diagnostics.ProcessStartInfo like public void AddInhertiableHandles(IEnumerable<SafeHandle> handles), public void AddInhertiableHandles(IEnumerable<IntPtr> handles) than a misleading property like public bool InheritHandles { get; set; } because any time there's a redirection of pipes, inheritance needs to be enabled but consumers of the API do not likely mean to inherit every handle the parent process has.

  12. Priya91 commented on Dec 7, 2016

    @Priya91
    Contributor

    What are the requirements for CoreFx API changes? Does the API need x-plat support

    We have the api addition process documented here. It also elaborates on the design principles. Yeah we do need x-plat support, as .NET Core supports Unix platforms as well.

    can there be Windows specific elements? Etc.

    Are you asking in terms of exposing an API only on Windows? We can't do that, although we have some windows specific APIs in .NET Core and throw PNSE on other platforms.

    For now the focus should be more on the API design as you suggested in using a method over property etc, which will take shape once we understand the scenarios and requirements on all platforms.

  13. whoisj commented on Dec 8, 2016

    @whoisj

    @Priya91 👍 and thanks.

  14. 124 remaining items

  15. added this to the 11.0.0 milestone on Mar 18, 2026
  16. jairbubbles commented on Mar 18, 2026

    @jairbubbles

    (typo in the link @adamsitnik)

  17. whoisj commented on Mar 18, 2026

    @whoisj

    @adamsitnik, it might be worth reaching out to Chad Boles (MSFT employee, or at least was) to have a look at the process class used in the VS Git Integration solution. VS is an extreme scenario with dozens and dozens of child-procs being spun up and down continually. This leads to "handle leakage" problems.

  18. added
    in-prThere is an active PR which will close this issue when it is merged
    on Mar 30, 2026
  19. added a commit that references this issue on Apr 8, 2026
    68b538c
  20. added a commit that references this issue on Apr 9, 2026
    0a06a15
  21. locked and limited conversation to collaborators on May 8, 2026
  22. adamsitnik commented on May 18, 2026

    @adamsitnik
    Member

    The API was implemented and released as part of .NET 11 Preview 4. This part of the blog post describes all important aspects of it: https://devblogs.microsoft.com/dotnet/process-api-improvements-in-dotnet-11/#handle-inheritance

  23. added a commit that references this issue on Jul 15, 2026
    248098f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Diagnostics.Processin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions