Skip to content

[Bug]: morpc Stream.Close(false) deadlocks behind a full receive channel #25943

Closed

Description

@Ariznawlll

Bug description

morpc.stream.done holds s.mu.RLock() while sending a response to the bounded receive channel. When that channel is full and the response context has no deadline, the send blocks indefinitely while retaining the read lock.

At the same time, Stream.Close(false) needs s.mu.Lock() before it can drain the channel and mark the stream closed. It therefore cannot make progress, producing a lock/channel deadlock:

  1. The receive channel is full.
  2. Another response enters stream.done, obtains RLock, and blocks on s.c <- response.
  3. The caller invokes Close(false).
  4. Close(false) waits for the write lock, but only Close can drain the channel and unblock done.

Close(true) can avoid this in some cases by stopping the backend first, but the normal Close(false) API has no cancellation path for the blocked delivery.

Impact

A caller that stops consuming a stream and closes it while responses are still arriving can hang permanently. This can leak the stream, block cleanup, and leave callers waiting forever during cancellation or shutdown.

Reproduction

A white-box test with a receive channel of capacity one:

  1. Deliver sequence 1 to fill the channel.
  2. Deliver sequence 2 with context.Background() in another goroutine; it blocks while holding RLock.
  3. Call Close(false) and wait for it to return.

The audit was run three times. All 3/3 runs showed Close(false) still blocked after 200 ms; it returned only after the test manually drained the first response.

go test ./pkg/common/morpc \
  -run '^TestAuditStreamCloseDoesNotBlockBehindFullReceiveChannel$' \
  -count=3 -v

Expected behavior

Stream.Close(false) must promptly cancel or otherwise unblock pending response delivery, even when the receive channel is full and unread.

Tested version

  • Audit baseline: main@7f9ef5de9bb9337d8db0a9b15c44cba3b2838aa2
  • GitHub main was checked again before filing and still holds RLock across the blocking channel send.

Suggested direction

Do not hold the stream read lock across a potentially blocking channel send. Introduce a stream-local cancellation signal that both done and Close select on, or otherwise arrange for close to unblock pending delivery before acquiring the exclusive lock.

Activity

  1. added this to the MO-202607 milestone on Jul 21, 2026
  2. added
    kind/bug
    on Jul 21, 2026
  3. added theissue type on Jul 21, 2026
  4. added a commit that references this issue on Jul 22, 2026
    e917fbf
  5. assigned and unassigned on Jul 22, 2026
  6. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 上完成验证,结论:该问题已修复。

    测试版本

    • MatrixOne 分支:main
    • MatrixOne commit:50319026a69e45c3c190cb7a09e9c5fbdb7f6857
    • 对应修复:PR #26004,commit e917fbfc5

    测试步骤

    1. 创建容量为 1 的 morpc stream 接收通道。
    2. 投递第一个 response 填满接收通道。
    3. 另一个 goroutine 投递第二个 response,使其进入原问题中的阻塞发送路径。
    4. 不消费通道内容,直接调用 Stream.Close(false)。
    5. 检查 Close 是否可以主动解除等待并及时返回,同时检查关闭后的 stream/channel 不会被错误复用。

    执行命令:

    go test ./pkg/common/morpc \
      -run '^(TestCloseStreamUnblocksFullReceiveChannel|TestClosedStreamHandleAndChannelAreNotReused)$' \
      -count=10
    
    go test -race ./pkg/common/morpc \
      -run '^TestCloseStreamUnblocksFullReceiveChannel$' \
      -count=1
    

    测试结果

    • 普通模式连续 10 轮通过。
    • -race 模式通过,未发现数据竞争。
    • 接收通道已经满且没有消费者时,Close(false) 仍能及时返回。
    • response delivery 不再持锁永久等待,Close 与发送路径之间未出现锁/通道环形等待。
    • stream 关闭后的内部对象不会被错误复用。

    因此,原 issue 中“Stream.Close(false) 被满接收通道永久阻塞”的死锁在当前 main 上未再复现。

    该问题必须构造 morpc 内部通道和锁状态,属于稳定的白盒并发测试,不适合加入 motr SQL 黑盒回归。

  7. added 3 commits that reference this issue on Sep 17, 2026
    e01ce65
    ea615fe
    51f05e9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions