Close() aborts the closing handshake when an in-flight Ping arrives after Close is sent

未关闭
#585 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

  • #595 来自 @Mohith26 —— 已关闭,未合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
go
领域
networking

调研方向

Start by reading writeFrame, handleControl, and waitCloseHandshake, then run the raw-socket reproduction described in the issue to confirm the missing Pong and unread peer Close. Check the existing close and Ping tests for nearby coverage. Done means a Ping received after sending Close gets the required Pong, the peer's Close is consumed, and the regression is covered by a test.

由索引模型根据 Issue 内容生成。

描述

Description

Conn.Close does not complete the RFC 6455 closing handshake when a Ping that was already on the wire arrives after this endpoint has sent Close.

What happens:

  1. writeClose sets closeSentErr.
  2. waitCloseHandshake → readLoop → handleControl sees opPing and calls writeControl(opPong, …).
  3. writeFrame returns net.ErrClosed because closeSentErr != nil (every opcode is refused after Close).
  4. That error aborts the drain before the peer's Close is read.
  5. Close's defer turns errors.Is(err, net.ErrClosed) into nil, so the caller sees success.
  6. No Pong is written; the peer's Close is never consumed; TCP is closed anyway.

Data frames and a peer Close alone in the same state are fine (waitCloseHandshake discards data and returns on Close). Only Ping breaks the drain.

Spec

RFC 6455 §5.5.2:

Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in response, unless it already received a Close frame.

At this point Close has been sent, not received, so a Pong is required. §5.5.1 forbids further data frames after Close; Pong is a control frame. §7.1.2 expects the endpoint to finish sending and receiving Close before tearing down TCP.

Reproduction

Raw-socket peer that lets the library send Close first, then writes Ping+Close without having read that Close (simulates frames written before the peer saw Close):

// library side
c, _ := websocket.Accept(w, r, &websocket.AcceptOptions{
    OnPingReceived: func(ctx context.Context, payload []byte) bool { return true },
})
_ = c.Write(ctx, websocket.MessageText, []byte("ready"))
time.Sleep(100 * time.Millisecond)
err := c.Close(websocket.StatusNormalClosure, "server-close")
// err == nil even when the peer's Close was never read
# peer: after reading "ready", wait until library Close is on the wire, then
# write Ping + Close without reading
sock.send(ping_frame)
sock.send(close_frame(1000))
# observe: library Close arrives, TCP closes, **no Pong**

Observed on master @ 5d0eacc and v1.8.15: Close() returns nil, OnPingReceived fires, no Pong on the wire, peer Close unread.

Controls: same harness with only Data+Close, or only Close, completes cleanly.

Suggested fix

In writeFrame, keep rejecting writes after Close except for opPong:

if closeSentErr != nil {
    if opcode != opPong {
        return 0, net.ErrClosed
    }
}

That restores the §5.5.2 Pong and lets waitCloseHandshake continue to the peer Close. Verified locally against the repro above; happy to open a PR.

Related
  • #298 / #394 — outbound Ping racing Close on the write lock / proposal to discard inbound Ping after Close. Closed via 28c6709 (TestConcurrentClosePing write-timeout reset). That did not cover the closeSentErr → abort → net.ErrClosed swallow path here.
  • #555 — 5s pong write timeout under contention (different).
  • #448 / #476 — other close/write races (fixed).
主要语言
Go
星标
5.6k
派生
394
平均合并
4 天 12 小时
30 天内合并 PR
11

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

coder/websocket 的其他 Issue

查看 coder/websocket 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。