mcp: fix goroutine leaks in unit tests (#496)

This PR fixes goroutine leaks in all unit tests.

To find the leaks I integrated https://github.com/uber-go/goleak and I
suggest to keep using it to catch any future regressions.

I used the folowing script to more easily find individual tests which
were leaking goroutines:

```
for t in $(go test -run=Nothing -list=. ./mcp | grep -v ok); do go test ./mcp -run="$t" > /dev/null && echo -n . || echo -e "\nDetected leak: $t"; done
```
This commit is contained in:
Friedrich Große 2025-10-16 17:13:30 +02:00 • committed by GitHub
commit 010bdbc64d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -310,7 +310,14 @@ func (r rwc) Write(p []byte) (n int, err error) {
}
func (r rwc) Close() error {
return errors.Join(r.rc.Close(), r.wc.Close())
rcErr := r.rc.Close()
var wcErr error
if r.wc != nil { // we only allow a nil writer in unit tests
wcErr = r.wc.Close()
}
return errors.Join(rcErr, wcErr)
}
// An ioConn is a transport that delimits messages with newlines across