Skip to content

fix(x-sdk): release XStream reader on early exit - #159

Merged
cc-hearts merged 1 commit into
mainfrom
sync/ant-design-x-1970
Aug 4, 2026
Merged

fix(x-sdk): release XStream reader on early exit#159
cc-hearts merged 1 commit into
mainfrom
sync/ant-design-x-1970

Conversation

@cc-hearts

@cc-hearts cc-hearts commented Aug 3, 2026

Copy link
Copy Markdown
Member

中文版模板 / Chinese template

🤔 This is a ...

  • 🆕 New feature
  • 🐞 Bug fix
  • 📝 Site / documentation improvement
  • 📽️ Demo improvement
  • 💄 Component style improvement
  • 🤖 TypeScript definition improvement
  • 📦 Bundle size optimization
  • ⚡️ Performance optimization
  • ⭐️ Feature enhancement
  • 🌐 Internationalization
  • 🛠 Refactoring
  • 🎨 Code style optimization
  • ✅ Test Case
  • 🔀 Branch merge
  • ⏩ Workflow
  • ⌨️ Accessibility improvement
  • ❓ Other (about what?)

🔗 Related Issues

💡 Background and Solution

XStream acquired a reader for its async iterator without releasing it when a consumer exited a for await...of loop early. This left the stream locked and allowed the underlying source to continue buffering.

This change:

  • wraps the read loop in try/finally and distinguishes normal completion from early exit
  • cancels unfinished streams on break, return, or consumer errors
  • suppresses cancellation failures so they do not replace the consumer's original error
  • always releases the reader lock
  • covers early exit, cancellation failure, and normal completion with tests

Verification:

  • vp test packages/x-sdk/src/x-stream/__tests__/index.test.ts — 3 passed
  • vp test — 46 files, 445 tests passed
  • vp check packages/x-sdk/src/x-stream/index.ts packages/x-sdk/src/x-stream/__tests__/index.test.ts — passed
  • repository-wide vp check formatting passed; lint/type checking is currently blocked by 43 unrelated existing errors outside this change

📝 Change Log

Language Changelog
🇺🇸 English Release the XStream reader lock and cancel the underlying stream when async iteration exits early.
🇨🇳 Chinese 修复 XStream 异步迭代提前退出时 reader lock 未释放且底层流继续缓冲的问题。

@cc-hearts

Copy link
Copy Markdown
Member Author

Review:✅ 修复合理,建议合并

try/finally + completed 标志的组合正确区分了正常结束与提前退出:

  • 提前 break / return / throw → completed 仍为 falsereader.cancel() 取消底层流,停止继续缓冲
  • 正常读完 → done 分支置 completed = true → 跳过 cancel,只 releaseLock()
  • cancel() 包在独立 try/catch 里且吞掉异常,不会覆盖消费者的原始错误 —— 这点很关键,finally 里抛出的异常会替换掉正在传播的异常
  • releaseLock()finally 尾部无条件执行,两条路径都释放

四条验收标准都满足。三个测试分别覆盖提前 break、消费者抛错(且断言 cancel 失败时原始错误仍被保留)、正常完整消费,覆盖面完整。

无阻塞问题。

@cc-hearts
cc-hearts merged commit ae828c6 into main Aug 4, 2026
1 check passed
@cc-hearts
cc-hearts deleted the sync/ant-design-x-1970 branch August 4, 2026 15:02
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.

[sync][P1] x-sdk: 修复 XStream 提前退出时 reader lock 泄漏 (#1970)

1 participant