Skip to content

Commit

Permalink
File Handlers leakage on Mono's runtime detected and worked around.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisias committed Apr 18, 2022
1 parent 088ab26 commit facda5b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
8 changes: 7 additions & 1 deletion Source/KSPe/Multiplatform/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ private static string Reparse_readlink(string path)
}
catch (Win32Exception e)
{
// WHAT IN HELL??? Since then this crappy exception is being thrown by Mono here???
// This happens here when the System is running out of File Handlers.
// The Shell.Command *WAS* being used on a IDisposable structure, but unfortunately
// the apparently **IDIOTS** that coded that crap apparently was not closing the
// file handlers on the IDisposable implementation.
//
// This is also a hint that I should push KSPe to mainstream the fastest I can, and start to phase out
// the KSPe.Light program - I'm getting a huge surface of exposition to MS/Mono/Whoever crap.
Log.warn("Reparse_readlink got a {0} ", e.Message);
return path; // May God help the caller. :)
}
Expand Down
34 changes: 30 additions & 4 deletions Source/KSPe/Multiplatform/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,22 @@ internal static void raise(string command, int exitCode, string stderr = "")

public static string run(string program, string[] text)
{
using (Process cmd = new Process())
{
/*
* This is absolutely f*cking unbeliable!!!
* They didn't called Close on the IDisposable implementation, so every
* shell command I launched was leaking at least 3 file handlers (stdin, stdout and stderr)!!
*
* By swithing using (Process cmd = new Process()) by this try-finally structure, I solved the
* avalanche of Win32Exceptions being thrown when I have enought KSP.Lights being loaded.
*
* GOD KNOWS how many users were royally screwed by this crap!!!
*
* DAMN YOU MONO AND MICROSOFT "DEVELOPERS".
*/
Process cmd = null;
try
{
cmd = new Process();
cmd.StartInfo.FileName = program;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
Expand All @@ -77,12 +91,20 @@ public static string run(string program, string[] text)
if (0 != cmd.ExitCode) Exception.raise(program, cmd.ExitCode, cmd.StandardError.ReadToEnd());
return cmd.StandardOutput.ReadToEnd();
}
finally
{
cmd?.Close();
}
}

public static string command(string command, string commandline)
{
using (Process cmd = new Process())
{
// See the remarks on `run` method. DAMN!!
Process cmd = null;
try
{
cmd = new Process();

cmd.StartInfo.FileName = command;
cmd.StartInfo.Arguments = commandline;
cmd.StartInfo.RedirectStandardInput = false;
Expand All @@ -96,6 +118,10 @@ public static string command(string command, string commandline)
if (0 != cmd.ExitCode) Exception.raise(command, cmd.ExitCode, cmd.StandardError.ReadToEnd());
return cmd.StandardOutput.ReadToEnd();
}
finally
{
cmd?.Close();
}
}
}
}

0 comments on commit facda5b

Please sign in to comment.