Skip to content

Commit

Permalink
Fix more undisposed handles
Browse files Browse the repository at this point in the history
  • Loading branch information
asmichi committed Sep 22, 2024
1 parent 02f3bfd commit b505970
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
18 changes: 12 additions & 6 deletions src/ChildProcess/PlatformAbstraction/ConsolePal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Asmichi.PlatformAbstraction
{
internal interface IConsolePal
{
SafeFileHandle GetStdInputHandleForChild(bool createNewConsole);
SafeFileHandle GetStdOutputHandleForChild(bool createNewConsole);
SafeFileHandle GetStdErrorHandleForChild(bool createNewConsole);
SafeFileHandle? CreateStdInputHandleForChild(bool createNewConsole);
SafeFileHandle? CreateStdOutputHandleForChild(bool createNewConsole);
SafeFileHandle? CreateStdErrorHandleForChild(bool createNewConsole);
bool HasConsoleWindow();
}

Expand All @@ -28,9 +28,15 @@ private static IConsolePal CreatePlatformSpecificImpl()
};
}

public static SafeFileHandle GetStdInputHandleForChild(bool createNewConsole) => Impl.GetStdInputHandleForChild(createNewConsole);
public static SafeFileHandle GetStdOutputHandleForChild(bool createNewConsole) => Impl.GetStdOutputHandleForChild(createNewConsole);
public static SafeFileHandle GetStdErrorHandleForChild(bool createNewConsole) => Impl.GetStdErrorHandleForChild(createNewConsole);
/// <summary>
/// Creates a duplicate handle to the stdin of the current process that can be inherited by a child process.
/// </summary>
/// <returns>
/// The created handle. <see langword="null"/> if such an inheritable handle cannot be created.
/// </returns>
public static SafeFileHandle? CreateStdInputHandleForChild(bool createNewConsole) => Impl.CreateStdInputHandleForChild(createNewConsole);
public static SafeFileHandle? CreateStdOutputHandleForChild(bool createNewConsole) => Impl.CreateStdOutputHandleForChild(createNewConsole);
public static SafeFileHandle? CreateStdErrorHandleForChild(bool createNewConsole) => Impl.CreateStdErrorHandleForChild(createNewConsole);
public static bool HasConsoleWindow() => Impl.HasConsoleWindow();
}
}
12 changes: 6 additions & 6 deletions src/ChildProcess/PlatformAbstraction/Unix/UnixConsolePal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ internal class UnixConsolePal : IConsolePal
private const int StdOutFileNo = 1;
private const int StdErrFileNo = 2;

public SafeFileHandle GetStdInputHandleForChild(bool createNewConsole) =>
DuplicateStdFileForChild(StdInFileNo, createNewConsole) ?? FilePal.OpenNullDevice(System.IO.FileAccess.Read);
public SafeFileHandle GetStdOutputHandleForChild(bool createNewConsole) =>
DuplicateStdFileForChild(StdOutFileNo, createNewConsole) ?? FilePal.OpenNullDevice(System.IO.FileAccess.Write);
public SafeFileHandle GetStdErrorHandleForChild(bool createNewConsole) =>
DuplicateStdFileForChild(StdErrFileNo, createNewConsole) ?? FilePal.OpenNullDevice(System.IO.FileAccess.Write);
public SafeFileHandle? CreateStdInputHandleForChild(bool createNewConsole) =>
DuplicateStdFileForChild(StdInFileNo, createNewConsole);
public SafeFileHandle? CreateStdOutputHandleForChild(bool createNewConsole) =>
DuplicateStdFileForChild(StdOutFileNo, createNewConsole);
public SafeFileHandle? CreateStdErrorHandleForChild(bool createNewConsole) =>
DuplicateStdFileForChild(StdErrFileNo, createNewConsole);

private static SafeFileHandle? DuplicateStdFileForChild(int stdFd, bool createNewConsole)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Asmichi.PlatformAbstraction.Windows
{
internal sealed class WindowsConsolePal : IConsolePal
{
public SafeFileHandle GetStdInputHandleForChild(bool createNewConsole) =>
GetStdHandleForChild(Kernel32.STD_INPUT_HANDLE, createNewConsole) ?? FilePal.OpenNullDevice(System.IO.FileAccess.Read);
public SafeFileHandle GetStdOutputHandleForChild(bool createNewConsole) =>
GetStdHandleForChild(Kernel32.STD_OUTPUT_HANDLE, createNewConsole) ?? FilePal.OpenNullDevice(System.IO.FileAccess.Write);
public SafeFileHandle GetStdErrorHandleForChild(bool createNewConsole) =>
GetStdHandleForChild(Kernel32.STD_ERROR_HANDLE, createNewConsole) ?? FilePal.OpenNullDevice(System.IO.FileAccess.Write);
public SafeFileHandle? CreateStdInputHandleForChild(bool createNewConsole) =>
GetStdHandleForChild(Kernel32.STD_INPUT_HANDLE, createNewConsole);
public SafeFileHandle? CreateStdOutputHandleForChild(bool createNewConsole) =>
GetStdHandleForChild(Kernel32.STD_OUTPUT_HANDLE, createNewConsole);
public SafeFileHandle? CreateStdErrorHandleForChild(bool createNewConsole) =>
GetStdHandleForChild(Kernel32.STD_ERROR_HANDLE, createNewConsole);

/// <summary>
/// Returns the std* handle of the current process that can be inherited by a child process.
Expand Down
39 changes: 36 additions & 3 deletions src/ChildProcess/ProcessManagement/PipelineStdHandleCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private SafeHandle ChooseInput(
{
return redirection switch
{
InputRedirection.ParentInput => ConsolePal.GetStdInputHandleForChild(createNewConsole),
InputRedirection.ParentInput => CreateStdInputHandleForChild(createNewConsole) ?? OpenNullDevice(FileAccess.Read),
InputRedirection.InputPipe => inputPipe!,
InputRedirection.File => OpenFile(fileName!, FileMode.Open, FileAccess.Read, FileShare.Read),
InputRedirection.Handle => handle!,
Expand All @@ -212,8 +212,8 @@ private SafeHandle ChooseOutput(
{
return redirection switch
{
OutputRedirection.ParentOutput => ConsolePal.GetStdOutputHandleForChild(createNewConsole),
OutputRedirection.ParentError => ConsolePal.GetStdErrorHandleForChild(createNewConsole),
OutputRedirection.ParentOutput => CreateStdOutputHandleForChild(createNewConsole) ?? OpenNullDevice(FileAccess.Write),
OutputRedirection.ParentError => CreateStdErrorHandleForChild(createNewConsole) ?? OpenNullDevice(FileAccess.Write),
OutputRedirection.OutputPipe => outputPipe!,
OutputRedirection.ErrorPipe => errorPipe!,
OutputRedirection.File => OpenFile(fileName!, FileMode.Create, FileAccess.Write, FileShare.Read),
Expand All @@ -224,6 +224,39 @@ private SafeHandle ChooseOutput(
};
}

private SafeFileHandle? CreateStdInputHandleForChild(bool createNewConsole)
{
var handle = ConsolePal.CreateStdInputHandleForChild(createNewConsole);
if (handle is null)
{
return null;
}
AddObjectsToDispose(handle);
return handle;
}

private SafeFileHandle? CreateStdOutputHandleForChild(bool createNewConsole)
{
var handle = ConsolePal.CreateStdOutputHandleForChild(createNewConsole);
if (handle is null)
{
return null;
}
AddObjectsToDispose(handle);
return handle;
}

private SafeFileHandle? CreateStdErrorHandleForChild(bool createNewConsole)
{
var handle = ConsolePal.CreateStdErrorHandleForChild(createNewConsole);
if (handle is null)
{
return null;
}
AddObjectsToDispose(handle);
return handle;
}

private SafeFileHandle OpenFile(
string fileName,
FileMode mode,
Expand Down

0 comments on commit b505970

Please sign in to comment.