Skip to content

Replace deprecated CloseNotify with Request.Context().Done() for better client disconnect detection #4217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

burybell
Copy link

This PR updates the Context.Stream function to use Request.Context().Done() instead of the deprecated CloseNotify() method.

Changes:
Replace w.CloseNotify() with c.Request.Context().Done() for client disconnect signal.

Motivation:
CloseNotify is deprecated and unreliable in modern HTTP/2 environments or when the connection passes through certain proxies or gateways.

Request.Context().Done() provides a more robust and standard-compliant way to detect client disconnects.

Impact:
This change improves compatibility and correctness, especially when using HTTP/2 or reverse proxies.

Let me know if you'd prefer backward compatibility logic for older Gin versions or if further changes are needed.

@appleboy appleboy requested a review from Copilot May 21, 2025 00:29
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR replaces the deprecated CloseNotify() method with Request.Context().Done() to improve client disconnect detection, especially in HTTP/2 and reverse proxy environments.

  • Replaces w.CloseNotify() with c.Request.Context().Done() in the stream handler
  • Enhances compatibility and correctness for client disconnect detection
Comments suppressed due to low confidence (1)

context.go:1211

  • The switch to using Request.Context().Done() is a solid update over the deprecated CloseNotify(). Please ensure that c.Request is always non-nil and its context properly reflects cancellation in all deployment scenarios.
clientGone := c.Request.Context().Done()

@appleboy appleboy added this to the v1.11 milestone May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants