LXD 容器重启后 /tmp(tmpfs) 清空导致文件上传 500 与评测失败
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 55/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- node.js, typescript
- Área
- backend, testing-qa
Línea de trabajo
Start by tracing the upload failure from the reported ENOENT in framework/server.ts:435 and inspect how HydroOJ creates and uses /tmp/hydro/upload; then check hydrojudge.tmp_dir handling for the same missing-directory case. Reproduce after removing the temporary directory, and verify that uploads and judging recover when it is absent. The issue does not name relevant test files, so locate or add coverage for both paths.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
问题描述
在 LXD/LXC 容器中部署 HydroOJ 时,容器重启后出现以下问题:
- 文件上传失败:上传测试数据(
.in/.out/.zip/图片等)时,POST /{pid}/files返回500 Internal Server Error,浏览器控制台仅显示Error: error。 - 评测功能失效:hydrojudge 无法正常判题。
更新或重启服务(pm2 restart)后功能临时恢复,但下次容器重启后问题复现。
系统环境
这是一个环境相关问题:问题的根因在于 LXD 容器的
/tmp挂载方式,而非 HydroOJ 代码本身的逻辑错误。请重点关注以下环境特征。
| 项目 | 信息 | 备注 |
|---|---|---|
| 部署方式 | LXD/LXC 容器 | 官方安装脚本安装 |
| 容器系统 | Debian/Ubuntu | LXD 默认镜像 |
/tmp 挂载 |
tmpfs(内存文件系统) | LXD 默认行为,非 fstab 配置 |
| HydroOJ 版本 | 4.58.5 | 更新后恢复 |
| 反向代理 | Caddy + nginx | — |
| 进程管理 | pm2 | — |
关键环境特征:/tmp 为 tmpfs
LXD 容器默认将 /tmp 挂载为 tmpfs(内存文件系统),容器重启后内容全部清空:
$ mount | grep ' /tmp'
tmpfs on /tmp type tmpfs (rw,nosuid,nodev)
$ df -h /tmp
Filesystem Size Used Avail Use% Mounted on
tmpfs 32G 2.3M 32G 1% /tmp
$ grep tmp /etc/fstab
# 空输出 —— 该 tmpfs 挂载由 LXD 管理,不在容器内 fstab 中
/etc/fstab 中没有 /tmp 配置,说明该 tmpfs 是 LXD 的默认行为,容器内无法通过修改 fstab 关闭。
根本原因
HydroOJ 及其评测组件的临时工作目录位于 /tmp 下:
| 组件 | 临时目录 | 来源 |
|---|---|---|
| HydroOJ 文件上传 | /tmp/hydro/upload/ |
os.tmpdir()(默认 /tmp) |
| hydrojudge 评测 | /tmp/hydro/judge |
配置项 hydrojudge.tmp_dir |
容器重启后 tmpfs 清空,上述目录消失。HydroOJ 处理上传/评测请求时未确保临时目录存在(未做 mkdir -p 兜底),直接对不存在的路径执行 lstat/open,触发 ENOENT 并返回 500。
错误日志
hydrooj 主服务
Error: ENOENT: no such file or directory, lstat '/tmp/hydro/upload/0/yt0tkjiy1pc43her94dpb17ud'
at Object.<anonymous> (node:fs:1249:5)
at .../framework/server.ts:435:92
所有文件类型(.in、.zip、.png)均失败,与文件本身无关。
浏览器端
- Console:
Error: error(错误信息被吞掉,无具体提示) - Network:
POST /{pid}/files→500,响应体仅 21 字节
hydrojudge
hydrojudge.tmp_dir 默认 /tmp/hydro/judge,重启后目录丢失,无法创建沙箱。
复现步骤
- 在 LXD/LXC 容器中按官方脚本安装 HydroOJ
- 确认
mount | grep ' /tmp'显示tmpfs - 重启容器(
lxc restart) - 登录后台 → 任意题目 → 评测设置 → 测试数据 → 上传文件
- 上传失败(500),hydrooj 日志出现
ENOENT - 提交代码评测 → 评测失败
pm2 restart hydrooj hydrojudge→ 功能恢复(目录被重建)
影响范围
- ✅ LXD/LXC 容器(
/tmp默认 tmpfs) - ⚠️ systemd
PrivateTmp部署可能受影响 - ❌ 裸机/普通 VPS 不受影响(
/tmp位于磁盘)
建议改进
- 运行时兜底创建临时目录(推荐,最小改动):写入临时文件前
fs.promises.mkdir(dir, { recursive: true }),即使 tmpfs 清空也能自愈。 - 启动时初始化:服务启动阶段统一
mkdir -p(当前已有部分逻辑,但运行期目录丢失仍 500)。 - 改善前端错误提示:上传失败时透出后端具体错误,便于排查。
- 文档补充:在 FAQ 中提示 LXD 用户将
TMPDIR和hydrojudge.tmp_dir指向持久化路径。
临时规避方案
将临时目录迁移到持久化磁盘:
mkdir -p /data/tmp && chmod 1777 /data/tmp
pm2 set hydrooj:TMPDIR /data/tmp
hydrooj cli system set hydrojudge.tmp_dir /data/tmp/judge
pm2 restart hydrooj hydrojudge
感谢开发 HydroOJ,非常棒的项目 🙏
- Lenguaje dominante
- TypeScript
- Estrellas
- 7.3k
- Forks
- 504
- Merge medio
- 11 h 29 min
- PR fusionados (30 d)
- 2
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de hydro-dev/Hydro
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
-
希望给编辑器加个选择,可回到旧版Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
-
希望比赛赛制增加CCPC新赛制Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
-
希望比赛中的“题目列表”页面,不显示详细的题目IDAbierto
Dificultad 3/5 1-2 días Aptitud para principiantes 65/100
-
Dificultad 4/5 3-5 días Aptitud para principiantes 52/100
Todos los issues de hydro-dev/Hydro
Issues similares
-
generate_image reference URL guard lets through IPv4-mapped IPv6 hosts and 100.64.0.0/10Posiblemente ocupada @goranai la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día
-
Inbox permission: an allow sent with `updatedInput` is ignored by Claude Code on its own safety checksPosiblemente ocupada @Raj1v la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
backnotprop/plannotator#4046 ·
Los mantenedores suelen responder en 1 día
-
進捗部屋の管理画面で削除対象を月ごとに絞り込むAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
traPtitech/knoQ_R-UI#290 ·
Los mantenedores suelen responder en 24 días
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
farion1231/cc-switch#9981 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
account bug course exam iris lecture
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día