Skip to content

Conversation

@alienx5499
Copy link

Closes #3116

Summary

Implements Context() method for network.Conn that returns a context cancelled when the connection closes, enabling proper resource cleanup and early cancellation of dependent work.

Changes

  • Added Context() method to network.Conn interface
  • Implemented context management in swarm.Conn with lifecycle-aware cancellation
  • Added Context() method to mock connections for testing
  • Added comprehensive test suite covering all scenarios

Benefits

  • Resource Cleanup: Clean up resources when connections close
  • DoS Resiliency: Prevent resource leaks from closed connections
  • Connection-Specific: Works per-connection, not per-peer (fixes the two-connection issue mentioned in [Proposal] .Context() for network.Conn (or maybe just swarm.Conn) #3116)
  • Early Cancellation: Cancel work that depends on the connection

Testing

✅ All tests pass
✅ No regressions
✅ Build succeeds

Prior Art

Follows patterns from Go stdlib (*http.Request.Context(), *tls.ClientHelloInfo.Context()) and quic-go (quic.Connection.Context())

@alienx5499 alienx5499 changed the title # Add Context() method to network.Conn Add Context() method to network.Conn Oct 27, 2025
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.

[Proposal] .Context() for network.Conn (or maybe just swarm.Conn)

1 participant