`runner.promise` resolves when the worker pool exits with an error

未关闭 适合新手
#635 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@Krrishkumar125 已经在做这个了。

开始于 2026年10月21日。

  • #648 来自 @Krrishkumar125 —— 未关闭

评估

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

调研方向

从 src/runner.ts 中的 buildRunner 开始,检查 workerPool.promise 和 cron.promise 如何传递给 runner promise。添加 issue 中描述的复现测试,然后运行相关测试套件。当 worker-pool 错误以原始错误拒绝 runner.promise,且 CLI 以退出码 1 退出时,即表示完成。

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

描述

Summary

Since 0.17.0, when the worker pool shuts itself down because of an error (for example Could not completeJobs; queue is in an inconsistent state; aborting.), runner.promise resolves, the same as after runner.stop(). The error only appears in the log line Runner stopping (reason: worker pool exited with error: …).

A host that treats a rejected runner.promise as the failure signal never finds out, and its process keeps running without a worker pool. The CLI is affected too: it awaits the same promise, so a pool crash now ends the CLI with exit code 0.

Steps to reproduce

Make one job's completion fail, then watch how runner.promise settles:

test("runner.promise rejects when the worker pool exits with an error", () =>
  withOptions(async (options) => {
    const { pgPool } = options;
    await reset(pgPool, options);
    await pgPool.query(`
      create or replace function public.refuse_job_completion() returns trigger
      language plpgsql as $$ begin raise exception 'completion refused'; end; $$;
      create trigger refuse_job_completion
        before delete on ${ESCAPED_GRAPHILE_WORKER_SCHEMA}._private_jobs
        for each row execute function public.refuse_job_completion();
    `);

    const runner = await run({
      ...options,
      taskList: { job1: async () => {} },
      preset: { worker: { completeJobBatchDelay: 0 } },
    });
    try {
      await runner.addJob("job1", { id: "1" });
      const outcome = await runner.promise.then(
        () => "resolved",
        (error: unknown) => `rejected: ${String(error)}`,
      );
      expect(outcome).toMatch(/^rejected: .*completion refused/);
    } finally {
      await runner.stop().catch(() => {});
    }
  }));

(completeJobBatchDelay: 0 only makes the pool give up immediately; with unbatched completions it retries with backoff first and ends the same way.)

On main (4cda192) this fails:

[core] ERROR: Failed to complete jobs '1':
[core] WARNING: Runner stopping (reason: worker pool exited with error: error: completion refused)
Expected pattern: /^rejected: .*completion refused/
Received string:  "resolved"
Expected

runner.promise rejects with the pool's error, as the error branch in buildRunner is written to do (logger.error("Stopping worker due to an error: …") then return Promise.reject(error)).

The library example in the configuration docs relies on this too: await runner.promise inside main(), with main().catch(… process.exit(1)). With the current behaviour that example exits with code 0 after the pool fails. (pool:fatalError is emitted, but it isn't listed in the worker events docs.)

Cause

src/runner.ts, buildRunner: the pool's (and cron's) rejection is turned into a stop(…) call and then swallowed:

const wp = workerPool.promise
  .then(
    () => (running ? stop("worker pool exited cleanly", true) : void 0),
    (e) => (running ? stop(`worker pool exited with error: ${e}`) : void 0),
  )
  .catch(noop);

so Promise.all([cp, wp]) always resolves and its rejection handler can't run for a pool or cron failure.

This came in with 6b63ebd ("Massively improve logging around worker shutdown", part of #544), before 0.17.0 shipped. Running the test above on different versions:

Version runner.promise after a failed completion
0.16.6 never settles (seppuku, then "Worker exited, but pool is in continuous mode … Did something go wrong?"), as in #592
6b63ebd^ (unreleased 0.17 work) rejects with error: completion refused, because .finally() kept the rejection: workerPool.promise.finally(() => { if (running) { stop(); } })
0.17.0 – 0.18.0 resolves

So the rework's intended rejection was lost in the same change that added the new shutdown logging.

Possible fix

Let the rejection reach the existing handler, which already stops the runner and rejects:

const wp = workerPool.promise.then(() =>
  running ? stop("worker pool exited cleanly", true) : void 0,
);
const cp = cron.promise.then(() =>
  running ? stop("cron exited cleanly", true) : void 0,
);

With this the test above passes and the rest of the suite is unchanged locally. In the logs, the runner now also prints Stopping worker due to an error: …, and its stop reason reads error: … instead of worker pool exited with error: ….

The CLI shows the same difference. With a job whose completion fails (same trigger, completeJobBatchDelay: 0 in the config), graphile-worker built from main logs the failure and exits with code 0; built with the fix it logs Stopping worker due to an error: error: completion refused and exits with code 1.

Happy to open a PR with the test and fix, or adjust to keep the old wording.

Additional context
  • graphile-worker 0.18.0 (also checked with @graphile-pro/worker 0.2.2 loaded: same behaviour), Node 24 and Bun, PostgreSQL 16.
  • Related: #501 (process not exiting after seppuku, so the supervisor never restarts it) and #592 (0.16.6: runner.promise never settled). The 0.17 rework fixed the "never settles" case from #592; this is the remaining half: it settles, but as a success.
主要语言
TypeScript
星标
2.5k
派生
134
平均合并
16 小时 24 分钟
30 天内合并 PR
3

环境准备

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

从这里开始

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

graphile/worker 的其他 Issue

查看 graphile/worker 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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