#1526·go-git

v6-transport: `PackSession.Handshake()` can leak `cmd` and/or a goroutine, and has a data race

Author: mhaggerCreated Apr 21, 2025Updated Oct 1, 2026
Labelsbuggood first issuehelp wantedstale

In branch v6-transport, PackSession.Handshake()[starts up a goroutine](https://github.com/go-git/go-git/blob/675edb808690b47e0a9cb99c44a0f2b8245209a6/plumbing/transport/pack.go#L91) to copy data fromstderrto&c.stderrBufand then [starts something usingcmd.Start()](https://github.com/go-git/go-git/blob/675edb808690b47e0a9cb99c44a0f2b8245209a6/plumbing/transport/pack.go#L97). (What cmd.Start()` starts seems to depend on the details, but I presume that at least sometimes it needs to be stopped.)

But if PackSession.Handshake() exits with an error after one or both of these steps is complete, the goroutine, or both the goroutine and the cmd, are never stopped and seem to be leaked.

Moreover, the same method uses a defer to copy the stderr buffer into err. But nothing prevents this defer from running before the copy goroutine has completed writing into the same stderr buffer. This therefore constitutes a data race. (I've observed this race using the race detector.)

/cc @aymanbagabas, who I think worked on this part of the code.