apenella / go-ansible

Go-ansible is a Go package that enables the execution of ansible-playbook or ansible commands directly from Golang applications. It supports a wide range of options for each command, enabling smooth integration of Ansible functionality into your projects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Many ssh <defunct> then using library.

neodiz opened this issue · comments

Good afternoon. I think I found a problem in the library, a large number of child processes are not closed. Here is my example

`func ansible() error {
fmt.Println("test")
ansiblePlaybookConnectionOptions := &options.AnsibleConnectionOptions{
User: "inurmuhametov",
SSHExtraArgs: "-o ControlPersist=360s",
SCPExtraArgs: "-o ControlPersist=360s",
SFTPExtraArgs: "-o ControlPersist=360s",
}

ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
	Inventory:  "<server>,",
	VerboseVVVV:true,
}

playbook := &playbook.AnsiblePlaybookCmd{
	Playbooks:         []string{
		"/tmp/performance.yml"},
	ConnectionOptions: ansiblePlaybookConnectionOptions,
	Options:           ansiblePlaybookOptions,
}

err := playbook.Run(context.TODO())
if err !=nil {
	playbook = nil
	return err
}
playbook = nil
return nil

}
func main() {

for ; ;  {
	ansible()
	time.Sleep(5 * time.Minute)
}

}`

`
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 1229188 6164 pts/0 Ssl+ 12:13 0:00 /app
root 17 0.0 0.0 0 0 ? Zs 12:13 0:00 [ssh]
root 18 0.0 0.0 0 0 ? Zs 12:13 0:00 [ssh]
root 64 0.0 0.0 7636 4192 pts/1 Ss 12:14 0:00 bash
root 102 0.0 0.0 0 0 ? Zs 12:18 0:00 [ssh]
root 103 0.0 0.0 0 0 ? Zs 12:18 0:00 [ssh]
root 393 0.0 0.0 0 0 ? Zs 12:23 0:00 [ssh]
root 394 0.0 0.0 0 0 ? Zs 12:23 0:00 [ssh]
root 437 0.0 0.0 0 0 ? Zs 12:29 0:00 [ssh]
root 438 0.0 0.0 0 0 ? Zs 12:29 0:00 [ssh]
root 481 0.0 0.0 0 0 ? Zs 12:34 0:00 [ssh]
root 482 0.0 0.0 0 0 ? Zs 12:34 0:00 [ssh]
root 525 0.0 0.0 0 0 ? Zs 12:39 0:00 [ssh]
root 526 0.0 0.0 0 0 ? Zs 12:39 0:00 [ssh]
root 569 0.0 0.0 0 0 ? Zs 12:44 0:00 [ssh]
root 570 0.0 0.0 0 0 ? Zs 12:44 0:00 [ssh]
root 613 0.0 0.0 0 0 ? Zs 12:49 0:00 [ssh]
root 614 0.0 0.0 0 0 ? Zs 12:49 0:00 [ssh]
root 657 0.0 0.0 0 0 ? Zs 12:54 0:00 [ssh]
root 658 0.0 0.0 0 0 ? Zs 12:54 0:00 [ssh]
root 701 0.0 0.0 0 0 ? Zs 12:59 0:00 [ssh]
root 702 0.0 0.0 0 0 ? Zs 12:59 0:00 [ssh]
root 745 0.0 0.0 0 0 ? Zs 13:05 0:00 [ssh]
root 746 0.0 0.0 0 0 ? Zs 13:05 0:00 [ssh]
root 789 0.0 0.0 0 0 ? Zs 13:10 0:00 [ssh]
root 790 0.0 0.0 0 0 ? Zs 13:10 0:00 [ssh]
root 833 0.0 0.0 0 0 ? Zs 13:15 0:00 [ssh]
root 834 0.0 0.0 0 0 ? Zs 13:15 0:00 [ssh]
root 877 0.0 0.0 0 0 ? Zs 13:20 0:00 [ssh]
root 878 0.0 0.0 0 0 ? Zs 13:20 0:00 [ssh]
root 921 0.0 0.0 0 0 ? Zs 13:25 0:00 [ssh]
root 922 0.0 0.0 0 0 ? Zs 13:25 0:00 [ssh]
root 965 0.0 0.0 0 0 ? Zs 13:30 0:00 [ssh]
root 966 0.0 0.0 0 0 ? Zs 13:30 0:00 [ssh]
root 1009 0.0 0.0 0 0 ? Zs 13:35 0:00 [ssh]
root 1010 0.0 0.0 0 0 ? Zs 13:35 0:00 [ssh]
root 1053 0.0 0.0 0 0 ? Zs 13:41 0:00 [ssh]
root 1054 0.0 0.0 0 0 ? Zs 13:41 0:00 [ssh]
root 1097 0.0 0.0 0 0 ? Zs 13:46 0:00 [ssh]
root 1098 0.0 0.0 0 0 ? Zs 13:46 0:00 [ssh]
root 1141 0.0 0.0 0 0 ? Zs 13:51 0:00 [ssh]
root 1142 0.0 0.0 0 0 ? Zs 13:51 0:00 [ssh]
root 1185 0.0 0.0 0 0 ? Zs 13:56 0:00 [ssh]
root 1186 0.0 0.0 0 0 ? Zs 13:56 0:00 [ssh]
root 1229 0.0 0.0 0 0 ? Zs 14:01 0:00 [ssh]
root 1230 0.0 0.0 0 0 ? Zs 14:01 0:00 [ssh]
root 1273 0.0 0.0 0 0 ? Zs 14:06 0:00 [ssh]
root 1274 0.0 0.0 0 0 ? Zs 14:06 0:00 [ssh]
root 1317 0.0 0.0 0 0 ? Zs 14:11 0:00 [ssh]
root 1318 0.0 0.0 0 0 ? Zs 14:11 0:00 [ssh]
root 1361 0.0 0.0 0 0 ? Zs 14:16 0:00 [ssh]
root 1362 0.1 0.0 0 0 ? Zs 14:16 0:00 [ssh]

`

Hi @neodiz!
Thank you very much for raising that issue. I will take a look at this.
Just letting you know, I will work on fixing that over the v2.0.0. By the way, have you tried setting a cancel context instead of the TODO one?

I keep you updated!

Hi @neodiz!
I could reproduce what you experienced, which allowed me to work on a solution to that issue. Unfortunately, I don't have a fix for that yet.
My first thought was that the go-ansible code did not manage properly the processes spawned by the process started by the library. I tried to set up the SysProcAttr or create a process group to handle the child processes but any of those was a deterministic solution.

I have realized that when I executed the ansible-playbook it also left some SSH zombie processes, and that allowed me to explore new avenues.
The cause of my problem was that I executed the playbook inside a container without the init enabled (read here), so no one reaped zombie processes.

Please, can you provide me with the following information?

  • Are you running Ansible within a container?
  • As is said here, Ansible sets the SSH -tt parameter and that could leave zombie process on your system, coming from the pseudo-tty. That parameter should be overwritten by adding the -T on the SSH extra arguments. Can you confirm if you still have zombie processes after setting the -T parameter?

Thank you very much!

Hi @neodiz!
I understand that the issue has been solved and I could close the issue.
I will follow your recommendation and I will include a section in the documentation explaining that behaviour and how to solve this.

Thank you very much!

Available in v2.0.0-rc-3