[Bug]: FileSequence: CLI processes with different TMPDIR use separate sequence counters, sporadic Duplicate entry on oc_job_runs in 35.0.3 (after #65434/#65425)

未关闭
#66,935 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
22/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
linux, mysql, php

调研方向

Start with FileSequence and ITempManager::getTempBaseDir() to see how the counter directory is derived from TMPDIR and related settings, then look at JobRuns::started where the duplicate insert on oc_job_runs fails. Reproducing needs two CLI processes with different TMPDIR values. Done would mean both processes share one counter, or the duplicate is retried, with the behaviour covered by a test.

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

描述

Summary

On Nextcloud 35.0.3.2 (which includes #65425, the fix for #65434), sporadic Duplicate entry '<id>' for key 'PRIMARY' errors on oc_job_runs still occurred, roughly 5 per day, always in OC\BackgroundJob\JobRuns::started called from cron.php. The cause was not the argument order but that two CLI processes of the same instance used different counter directories, because FileSequence derives its directory from the per-process temp base dir (TMPDIR etc.). After giving both processes the same TMPDIR, the errors stopped completely (0 in 7 days, ~35 expected).

Environment
  • Nextcloud 35.0.3.2, MariaDB, Linux
  • cron.php every 5 minutes from the crontab of the Nextcloud user, run with TMPDIR=/home/<user>/tmp/ (set in that crontab by the hosting panel, KeyHelp)
  • occ background-job:worker --interval=1 --stop_after=10m as a systemd service of the same user, without TMPDIR (so it falls back to /tmp)
  • Web requests use APCuSequence, only CLI processes use FileSequence
Cause

FileSequence keeps its counters in <temp base dir>/sfi_file_sequence_<instanceid>/seq-NNN.slots. The temp base dir comes from ITempManager::getTempBaseDir() (config tempdirectory, upload_tmp_dir, TMP, TEMP, TMPDIR, sys_get_temp_dir()).

So cron.php used /home/<user>/tmp/sfi_file_sequence_<id>/ and the worker used /tmp/sfi_file_sequence_<id>/: two independent counters for the same instance, same server id, same CLI bit. The worker creates about 17 job runs per second, so a cron start collides with a worker run in the same millisecond with a probability of about 1.7 %. Both then get sequence 0 and the second insert fails. With 288 cron starts per day this gives about 5 errors per day, which matches what we saw. All 9 rows we examined had cli=1, seq=0, and every error was at the :x5 cron start.

Evidence
  • The slot files in the cron temp dir changed only at the :x5 marks; the files in /tmp changed every second.
  • 9 errors between 13 Nov (update to 35.0.3.2) and 15 Nov 01:00.
  • On 15 Nov 10:59 the systemd unit got Environment=TMPDIR=/home/<user>/tmp/ (same as cron). Since then 0 errors in 7 days, and the /tmp counter directory is no longer written.
Workaround

Use the same temp directory for every CLI entry point (cron, systemd units, manual occ), or set 'tempdirectory' in config.php.

Suggestions

The coordination path should not depend on per-process environment variables. For example:

  • use a fixed directory below the data directory (or tempdirectory only), or
  • add a setup check that warns when CLI processes resolve different temp base dirs, or
  • treat a duplicate in JobRuns::started as retryable (generate a new id) instead of aborting the run.

Related: #65434 (closed, locked), #65425.

主要语言
PHP
星标
37.6k
派生
5.4k
平均合并
1 天 13 小时
30 天内合并 PR
623

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

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

nextcloud/server 的其他 Issue

查看 nextcloud/server 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

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