Skip to content

Conversation

@fgrosse
Copy link
Contributor

@fgrosse fgrosse commented Sep 18, 2025

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

@fgrosse fgrosse mentioned this pull request Sep 18, 2025
@fgrosse fgrosse changed the title Fix goroutine leaks in unit tests mcp: fix goroutine leaks in unit tests Sep 19, 2025
@fgrosse fgrosse marked this pull request as ready for review September 19, 2025 09:26
@fgrosse
Copy link
Contributor Author

fgrosse commented Sep 24, 2025

@findleyr let me know what you think when you have time :)

Copy link
Contributor

@findleyr findleyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience, and for these improvements. I got busy and had to put down this review temporarily.

@fgrosse
Copy link
Contributor Author

fgrosse commented Oct 6, 2025

I have addressed all comments and merged the most recent main into this branch. Good to go?

Copy link
Contributor

@findleyr findleyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, just superficial comments at this point. Really appreciate your time and diligence in tracking this down.

My largest comment was that I don't want to add a dependency on go-leak, and would prefer to do this analysis in an ad-hoc manner. (I actually thought I'd already left that feedback, but alas my review was still pending--sorry).

// Connect the server and client...
t1, t2 := mcp.NewInMemoryTransports()
if _, err := s.Connect(ctx, t1, nil); err != nil {
sess1, err := s.Connect(ctx, t1, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/sess1/serverSession (or ss)
s/sess2/clientSession (or cs)

sess1 and sess2 obscures the fact that these variables have different types.

go.mod Outdated
github.com/google/go-cmp v0.7.0
github.com/google/jsonschema-go v0.3.0
github.com/yosida95/uritemplate/v3 v3.0.2
go.uber.org/goleak v1.3.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't think we should add an additional dependency just for this purpose. It seems like handling these as a one-off, every once in a while, is sufficient for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point and removed this from the PR so we can move ahead.

It would have been beneficial to keep an automated test because it will be very easy to introduce regressions without it, but of course that decision is up to you.

@findleyr
Copy link
Contributor

Hi Friedrich, we really appreciate this contribution and would like to land it. Let me know if you'd like me to take it over (mea culpa for the review latency--it has been a busy few weeks!).

@fgrosse
Copy link
Contributor Author

fgrosse commented Oct 15, 2025

Sorry it took so long to respond. I finally took some time and addressed the remaining comments, so this should be good to merge now. If you would like to see more changes and want to move quickly, feel free to make them directly.

@findleyr
Copy link
Contributor

Thanks!

@findleyr findleyr merged commit 010bdbc into modelcontextprotocol:main Oct 16, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants