chore(scripts): 加 SysV 信号量泄漏回收工具(macOS + PGlite 专用)
macOS(XNU) 的 SysV 信号量集在创建进程退出后不回收,且系统不带 ipcrm/ipcs。
本仓每轮 cargo test 会新建若干 PGlite 实例(initdb 阶段 semget 一个
nsems=20 的 set)=> 跑够几百轮后 kern.sysv.semmns(87381) 被撑满,
任何新的 PostgreSQL 实例都起不来(semget: No space left on device)。
- scripts/ipc-reap.c:只读扫描 id 空间 + 精准 IPC_RMID 回收
口径:nsems==20(PGlite 指纹)且创建者 uid==当前用户
且最近一次 semop 距今超过阈值(活实例不会被误删)
- scripts/reap-ipc-leaks.sh:编译 + 调用的壳脚本,支持 --dry-run
实测(2026-10-26):回收 4350 个残留 set / 87000 个信号量,
可建 set 上限从 9 个信号量恢复到 4096 个。
Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
1 parent
1797aa0d64
commit
6e14fbeb47
2 files changed
+158
No files matched your search
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* ipc-reap —— macOS(SysV) 信号量泄漏回收器
|
||||
*
|
||||
* 🔴 为什么需要它:
|
||||
* macOS(XNU) 的 SysV 信号量集**在创建进程退出后不回收**(与 Linux 不同,
|
||||
* 且 macOS 不带 ipcrm/ipcs)⇒ 每次 `cargo test` 都会新建若干 PGlite 实例,
|
||||
* 每个实例在 initdb 阶段 semget 一个 **nsems=20** 的 set,进程退出后这 20 个
|
||||
* 信号量就永久留在系统表里。
|
||||
* 累积到 kern.sysv.semmns(87381)上限后,**任何新的 PostgreSQL 实例都起不来**
|
||||
* (initdb 报 `semget(...): No space left on device`)。
|
||||
*
|
||||
* 回收口径(刻意保守,宁可漏回收也不误伤活进程):
|
||||
* ① 只回收 nsems == 20(= PGlite/postgres-pglite 的指纹)的 set;
|
||||
* ② 只回收创建者 uid == 当前用户的 set(不碰 root / 其他用户的);
|
||||
* ③ 只回收「最近一次操作时间」距今超过 --min-idle 秒的 set
|
||||
* (活着的 PostgreSQL 会持续 semop ⇒ otime 是新的 ⇒ 不会被误删)。
|
||||
*
|
||||
* 用法:
|
||||
* cc -O2 -o ipc-reap ipc-reap.c && ./ipc-reap [--dry-run] [--nsems 20]
|
||||
* [--min-idle 3600] [--max-id 2000000]
|
||||
* 退出码:0 = 正常(--dry-run 时不落任何修改) / 2 = 参数错
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/sem.h>
|
||||
|
||||
static int g_dry_run = 0;
|
||||
static int g_nsems = 20; /* PGlite 指纹 */
|
||||
static long g_min_idle = 3600; /* 秒:最近操作距今超过它才算「死的」 */
|
||||
static long g_max_id = 2000000;
|
||||
|
||||
static void usage(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"用法: ipc-reap [--dry-run] [--nsems N] [--min-idle SECONDS] [--max-id N]\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
/** 当前还能新建多大的 set(每个建完立刻释放 ⇒ 净中性)⇒ 余量。 */
|
||||
static long free_capacity(void)
|
||||
{
|
||||
long lo = 0, hi = 4096;
|
||||
while (lo < hi) {
|
||||
long mid = lo + (hi - lo + 1) / 2;
|
||||
int id = semget(IPC_PRIVATE, (int)mid, 0600 | IPC_CREAT);
|
||||
if (id == -1) {
|
||||
hi = mid - 1;
|
||||
} else {
|
||||
semctl(id, 0, IPC_RMID);
|
||||
lo = mid;
|
||||
}
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--dry-run") == 0) {
|
||||
g_dry_run = 1;
|
||||
} else if (strcmp(argv[i], "--nsems") == 0 && i + 1 < argc) {
|
||||
g_nsems = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--min-idle") == 0 && i + 1 < argc) {
|
||||
g_min_idle = atol(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--max-id") == 0 && i + 1 < argc) {
|
||||
g_max_id = atol(argv[++i]);
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
uid_t me = getuid();
|
||||
time_t now = time(NULL);
|
||||
long before = free_capacity();
|
||||
|
||||
long scanned_sets = 0, matched = 0, reaped = 0, freed_sems = 0, skipped_busy = 0;
|
||||
|
||||
for (long id = 0; id < g_max_id; id++) {
|
||||
struct semid_ds ds;
|
||||
if (semctl((int)id, 0, IPC_STAT, &ds) == -1)
|
||||
continue;
|
||||
scanned_sets++;
|
||||
if ((int)ds.sem_nsems != g_nsems)
|
||||
continue;
|
||||
if (ds.sem_perm.cuid != me)
|
||||
continue;
|
||||
matched++;
|
||||
if (now - ds.sem_otime < g_min_idle) {
|
||||
skipped_busy++; /* 最近还被用过 ⇒ 大概率是活着的实例 */
|
||||
continue;
|
||||
}
|
||||
if (!g_dry_run) {
|
||||
if (semctl((int)id, 0, IPC_RMID) == -1) {
|
||||
fprintf(stderr, " ⚠️ id=%ld IPC_RMID 失败:%s\n", id, strerror(errno));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
reaped++;
|
||||
freed_sems += ds.sem_nsems;
|
||||
}
|
||||
|
||||
long after = g_dry_run ? before : free_capacity();
|
||||
|
||||
printf("回收前余量:最多可建 %ld 个信号量的 set\n", before);
|
||||
printf("扫描 id 空间 [0, %ld):现存 set %ld 个\n", g_max_id, scanned_sets);
|
||||
printf("命中口径(nsems=%d 且 uid=%d):%ld 个\n", g_nsems, (int)me, matched);
|
||||
printf(" 其中最近被使用过(疑似活实例,保留):%ld 个\n", skipped_busy);
|
||||
printf("%s:回收 %ld 个 set / %ld 个信号量\n",
|
||||
g_dry_run ? "[dry-run] 本应回收" : "已回收", reaped, freed_sems);
|
||||
printf("回收后余量:最多可建 %ld 个信号量的 set\n", after);
|
||||
if (!g_dry_run && after < g_nsems) {
|
||||
printf("⚠️ 余量仍不足 %d 个信号量 ⇒ 还有别处的占用(PGlite 需要 %d 个)\n",
|
||||
g_nsems, g_nsems);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env bash
|
||||
# 回收 macOS 上**泄漏的 SysV 信号量集**(PGlite 专用)。
|
||||
#
|
||||
# 🔴 背景:macOS(XNU) 的 SysV 信号量集在创建进程退出后**不回收**,且不带 ipcrm。
|
||||
# 本仓的 `cargo test` 每轮会新建若干 PGlite 实例(initdb 阶段 semget 一个
|
||||
# nsems=20 的 set)⇒ 跑够几百轮后 kern.sysv.semmns(87381) 被撑满
|
||||
# ⇒ **任何新的 PostgreSQL 实例都起不来**(`semget: No space left on device`)。
|
||||
#
|
||||
# 用法:
|
||||
# bash scripts/reap-ipc-leaks.sh # 回收
|
||||
# bash scripts/reap-ipc-leaks.sh --dry-run # 只报不删
|
||||
#
|
||||
# 退出码:0 = 正常(回收后余量够用)/ 1 = 回收后余量仍不足 / 2 = 前置错误
|
||||
#
|
||||
# 🔴 口径(宁可漏回收,不误伤活进程):
|
||||
# 只回收 ① nsems==20(PGlite 指纹)② 创建者 uid==当前用户
|
||||
# ③ 最近一次操作距今 > 1 小时的(活着的 PostgreSQL 会持续 semop ⇒ 不会命中)。
|
||||
set -u
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
SRC="${ROOT}/scripts/ipc-reap.c"
|
||||
|
||||
if ! command -v cc >/dev/null 2>&1; then
|
||||
echo "❌ 前置错误:找不到 cc(Xcode Command Line Tools)。"
|
||||
exit 2
|
||||
fi
|
||||
[ -f "${SRC}" ] || { echo "❌ 前置错误:找不到 ${SRC}。"; exit 2; }
|
||||
|
||||
TMP="$(mktemp -d)"
|
||||
trap 'rm -rf "${TMP}"' EXIT
|
||||
BIN="${TMP}/ipc-reap"
|
||||
|
||||
cc -O2 -o "${BIN}" "${SRC}" || { echo "❌ 前置错误:编译 ipc-reap.c 失败。"; exit 2; }
|
||||
|
||||
"${BIN}" "$@"
|
||||
exit $?
|
||||
Reference in new issue
Block a user