Previously, you were getting lucky when the timing of the syscalls would cause a write error in your second io.Copy
[line 41]. (Ignoring errors tends to hide bugs)
This was purely by accident (and may even be incorrect). Since the source connection of that copy (conn2
) is never closed, the io.Copy
never receives an io.EOF
and doesn't return. You need to close the opposing connection in each of the copy goroutines to unblock the other's call to io.Copy
.
wg.Add(1)
go func() {
io.Copy(conn1, conn2)
// conn2 has returned EOF or an error, so we need to shut down the
// other half of the duplex copy.
conn1.Close()
wg.Done()
}()
wg.Add(1)
go func() {
io.Copy(conn2, conn1)
conn2.Close()
wg.Done()
}()