Skip to content

Stopping gradle task execution does not close the SSH connection #409

@matic277

Description

@matic277

Environment info

null-null (groovy-ssh-2.11.2, jsch-0.2.5, groovy-3.0.17, java-17.0.4.1)
Gradle version: 8.4

Scenario:

  1. Run a gradle task that executes a program on remote server, recieves logs, and waits for program to stop executing.
  2. Killing the gradle task with CTRL+C, then confirming: Terminate batch job (Y/N)? with Y, the gradle task stops.

But the program executing on the remote server does not stop.
It does not matter if the gradle task is run from IntelliJ Idea, or directly from the command line.

What is the expected behaviour in such case?
Because i would expect the ssh client to close the connection, which should then also destroy the process that was spawned from it.

Build.gradle

tasks.register('runOnRemote') {
  group 'build'
  doLast {
    ssh.run {
      session(remotes.dev) {
        execute "cd TEST && java test.java" // TEST directory exists on remote
      }
    }
  }
}

test.java:
(simple program ta appends numbers to a file once per second)

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
class Main {
  public static void main(String[] args) throws Exception {
    final int[] c = { 0 };
    final var file = Paths.get("out"); // expecting empty file at start
    new Thread(() ->
    {
      while(true)
      {
        final var line = (c[0]++) + System.lineSeparator();
        try { Files.write(file, line.getBytes(), StandardOpenOption.APPEND); Thread.sleep(1000);}
        catch (Exception e) { throw new RuntimeException(e); }
      }
    }).start();
  }
}

After quitting the gradle task mid-execution, and inspecting the file "out" log file, the program keeps printing.
I can see the process with $ glances, and must shut it down manually.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions