Race condition when closing LDS

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

还没有人认领这个 Issue。

  • #756 来自 @ab-pm —— 已关闭,未合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
32/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
typescript

调研方向

从 packages/lds/src/index.ts 中 getChanges 调用附近的循环开始,检查调用 schema.__pgLdsSource?.close() 的集成测试 teardown。在负载下或使用最小的 sleepDuration 运行集成测试套件。完成的标准是关闭 source 能可靠地停止后续 polling,并且套件退出时不会反复出现“Pool has been closed”错误。

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

描述

Summary

In our integration test suite, we do teardown the LDS Live Source by calling schema.__pgLdsSource?.close(). This does however not work consistently, and from time to time (apparently when the server - that also runs the db in a docker container - is under higher load) it fails to stop the LDS loop and the test job (github action) is only stopped by timing out, failing the build step.

In the meantime, it does keep logging

Error during LDS loop: Pool has been closed

I believe I tracked this down to a bug in https://github.com/graphile/graphile-engine/blob/6692c2a65f406c55c3d1edfff51c8f7cc2296956/packages/lds/src/index.ts#L96-L101

  async function loop() {
    try {
      const rows = await client.getChanges(null, 500);
      …
    } catch (e) {
      console.error("Error during LDS loop:", e.message);
      // Recovery time...
      loopTimeout = setTimeout(loop, sleepDuration * 10);
      return;
    }
    loopTimeout = setTimeout(loop, sleepDuration);
  }
  loop();

While the client.getChanges query is awaited, no timeout is scheduled and the clearTimeout will be ineffective, so the loop will start over even after .close() has been called.

Steps to reproduce

By setting the sleepDuration to a minimum value, the loop spends much more time in the query so a close() will hit the sleep much less likely.

Possible Solution

I can see a few approaches:

  • just set a closed flag and check this before calling setTimeout again
  • .unref() each timer? I think this might have worked in my case, but probably not a generic solution
  • check whether the client.pool is closed before calling getChanges(), and breaking out of the loop
  • check whether the client.pool is closed in the catch handler of the loop, breaking out from there
  • a specific "client closed" exception that is handled differently in the catch of the loop, breaking out from there
主要语言
JavaScript
星标
764
派生
128
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

graphile/graphile-engine 的其他 Issue

查看 graphile/graphile-engine 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

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