Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processes orphaned in v3.7.0 instead of being killed #270

Closed
4 of 5 tasks
canon-cmre-chris-conway opened this issue Jan 13, 2025 · 1 comment · Fixed by #272
Closed
4 of 5 tasks

Processes orphaned in v3.7.0 instead of being killed #270

canon-cmre-chris-conway opened this issue Jan 13, 2025 · 1 comment · Fixed by #272
Labels

Comments

@canon-cmre-chris-conway
Copy link

canon-cmre-chris-conway commented Jan 13, 2025

Version

v3.7.0

Platform

.NET 8.0 / Windows 10

Steps to reproduce

  1. In an Nunit test, use CLIWrap to run a batch file, that runs another process (such as notepad.exe)
  2. Use a CancellationToken to stop the execution of the batch file
  3. Observe that the batch file (cmd.exe) and the child process are not killed and are now orphaned

Details

Expected Behaviour
The child processes should be killed.

Actual Behaviour
The child processes are not killed.

More information:

The behaviour in v3.7.0 is different to the previous version (v3.6.7).

I have example code. Running with 3.7.0 fails. Running with 3.6.7 passes.

Unit Test:

namespace test;

using System.Diagnostics;
using System.Text;
using CliWrap;
using NUnit.Framework;
public class NotepadTest
{

    [Test]
    public async Task LaunchAndKillNotepadViaCmd()
    {
        StringBuilder _outputBuilder = new();

        Command cmd = Cli.Wrap("run.bat")
                    .WithValidation(CommandResultValidation.ZeroExitCode)
                    .WithStandardErrorPipe(PipeTarget.ToStringBuilder(_outputBuilder))
                    .WithStandardOutputPipe(PipeTarget.ToStringBuilder(_outputBuilder));

        CancellationTokenSource cancellationTokenSource = new();
        TaskCompletionSource task_completion = new();

        CommandTask<CommandResult> cmd_task = null;

        await Task.Factory.StartNew(async() => {
            try {
                cmd_task = cmd.ExecuteAsync(cancellationTokenSource.Token);
                var result = await cmd_task;
                Console.Out.WriteLine("Task completed itself with exit code: " +  result.ExitCode);
            } finally {
                Console.Out.WriteLine("Finally");
                task_completion.SetResult();
            }
        });

        Console.WriteLine("Simulating work");
        Thread.Sleep(1000);
        Console.WriteLine("Finished work");

        var pid = cmd_task?.ProcessId;

        cancellationTokenSource.Cancel();
        await task_completion.Task;

        Process p = Process.GetProcessById((int)pid);
        if (p != null && !p.HasExited)
        {
            Assert.Fail("Process has not been killed");
        } else
        {
            Assert.Pass("Process has exited");
        }

    }
} 

run.bat simply contains:

notepad.exe

Checklist

  • I have looked through existing issues to make sure that this bug has not been reported before
  • I have provided a descriptive title for this issue
  • I have made sure that this bug is reproducible on the latest version of the package
  • I have provided all the information needed to reproduce this bug as efficiently as possible
  • I have sponsored this project
@Tyrrrz
Copy link
Owner

Tyrrrz commented Jan 17, 2025

This looks to be caused by the recent .NET bump (#271), I'll fix it shortly.

Tyrrrz added a commit that referenced this issue Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants