concurrency-safety

concurrency-safety is a skill for Claude Code, Codex from Wade-DevCode/awesome-coding-skills-cn. It costs 27 tokens per session (3,459 once invoked), scanned A, original, MIT.

A set of rules for writing safe concurrent and asynchronous code, where multiple tasks may run at the same time. It covers shared state, race conditions, deadlocks, timeouts, cancellation, and resource cleanup.

In plain words
What is it for?
It is for designing or reviewing multithreaded, asynchronous, or multiprocess code, especially code that shares mutable data or must handle errors, cancellation, and time limits.
Why use it?
It helps prevent timing-dependent bugs such as inconsistent data, exceeded limits, locked processes, leaked resources, and failures that are difficult to reproduce.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

Good fit It is for designing or reviewing multithreaded, asynchronous, or multiprocess code, especially code that shares mutable data or must handle errors, cancellation, and time limits.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/wade-devcode/awesome-coding-skills-cn/concurrency-safety
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add Wade-DevCode/awesome-coding-skills-cn --skill concurrency-safety
Clone the repo
git clone --depth 1 https://github.com/Wade-DevCode/awesome-coding-skills-cn

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for concurrency-safety

README.md
[![agentmods](https://agentmods.dev/badge/skills/wade-devcode/awesome-coding-skills-cn/concurrency-safety/github.svg)](https://agentmods.dev/skills/wade-devcode/awesome-coding-skills-cn/concurrency-safety)
Your own site
<a href="https://agentmods.dev/skills/wade-devcode/awesome-coding-skills-cn/concurrency-safety"><img src="https://agentmods.dev/badge/skills/wade-devcode/awesome-coding-skills-cn/concurrency-safety/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for concurrency-safety

Your own site · 80×15
<a href="https://agentmods.dev/skills/wade-devcode/awesome-coding-skills-cn/concurrency-safety"><img src="https://agentmods.dev/badge/skills/wade-devcode/awesome-coding-skills-cn/concurrency-safety.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 27 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 3,459 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00027 $0.03459
Opus 5.5 $0.00011 $0.01384
Sonnet 5.5 $0.00005 $0.00692
Haiku 4.5 $0.00003 $0.00346

Measured 2mo ago against content hash 33e683f19136, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

concurrency-safety scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2mo ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/concurrency-safety/SKILL.md · 268 lines

How it starts

The opening of the file, as written. The whole thing — 268 lines — stays where its author put it; the contents beside it link to each section on GitHub.

并发安全

何时用

  • 写多线程、多协程、多进程代码,共享状态需要同步时。
  • 异步任务(asyncio/goroutine/CompletableFuture/Task)有超时、取消或错误传播需求时。
  • 发现压测下出现数据不一致、随机 panic、连接池耗尽等难以稳定复现的 bug 时。
  • code review 发现锁的获取顺序不一致,或资源在异常路径下未释放时。

核心规则

1. 共享可变状态必须加保护,识别 check-then-act 竞态

规则: 所有被多个线程/协程读写的可变状态,必须用锁、原子操作或不可变数据结构保护;尤其要识别"先检查再操作"(check-then-act)这一最常见的竞态模式——检查和操作之间的窗口期可能被其他线程插入。

为什么: AI 生成并发代码时最常见的错误是:检查 if count < limit 后再递增——看起来没问题,但在高并发下两个线程可能同时通过检查,两个都执行递增,导致超出限制。还有"懒初始化"竞态:if instance is None: instance = ...,两个线程可能同时判断为 None 并各自创建一个实例。这类 bug 在低并发下压根不触发,在生产流量高峰时突然出现,且极难稳定复现。

怎么做:

// 反例:check-then-act 竞态(Go)
if counter < limit {        // ❌ 检查
    counter++               // ❌ 操作:两步之间有窗口,并发时超限
}

// 正例:用原子操作或锁合并 check 和 act
mu.Lock()
if counter < limit {
    counter++
    mu.Unlock()
    proceed()
} else {
    mu.Unlock()
    return ErrLimitExceeded
}
// 或者对于简单计数器,使用 sync/atomic
newVal := atomic.AddInt64(&counter, 1)
if newVal > limit {
    atomic.AddInt64(&counter, -1)  // 回退
    return ErrLimitExceeded
}
  • 共享变量的读写要么全部在锁内,要么全部用原子类型(sync/atomic、std::atomic、Interlocked)。
  • 不可变对象天然线程安全,优先设计成不可变:初始化后不修改,需要"修改"时创建新对象。
  • 使用通道(channel)/消息传递代替共享状态时,明确通道的所有权(谁关闭、谁读、谁写)。

2. 锁粒度小、获取顺序一致,能用无锁结构优先

规则: 锁的粒度要尽量细(只锁需要保护的最小代码段),多个锁的获取顺序在整个代码库中必须一致;有线程安全的数据结构(sync.Map、ConcurrentHashMap、channel)可以替代手动锁时优先选用。

为什么: AI 生成代码时有两种相反的极端:一种是粗粒度地用一把大锁保护整个方法,锁持有时间过长,并发度接近零;另一种是在不同地方以不同顺序加多把锁——函数 A 先锁 X 再锁 Y,函数 B 先锁 Y 再锁 X,只要这两个函数同时在不同线程执行,就必然死锁。死锁在开发环境极难触发,因为并发度不够高,往往到压测或生产才暴露。

怎么做:

  • 每个锁的用途和保护范围写在注释里:// mu 保护 cache 和 cacheExpiry。
  • 多锁时建立全局获取顺序约定(如按锁的变量名字母序,或按资源层级:账户锁先于订单锁),文档化并通过 code review 强制执行。
  • 锁内代码不做 IO(网络、磁盘)、不调用外部服务,否则锁持有时间不可控。
  • 读多写少的场景用读写锁(sync.RWMutex、ReentrantReadWriteLock),提升并发读吞吐。
  • 考虑无锁替代方案:原子操作、sync.Map、channel、CAS(Compare-And-Swap)。

3. 异步任务管理生命周期:超时、取消、异常传播

规则: 每个启动的异步任务都必须有:超时限制(防止无限挂起)、取消机制(支持优雅关闭)、异常传播路径(确保失败被感知);禁止"发射后不管"的孤儿任务(fire-and-forget without error handling)。

为什么: AI 生成异步代码时极容易写出 asyncio.create_task(do_something()) 或 go func() { ... }() 然后不管——任务抛异常直接消失,日志里没有任何痕迹;任务永久挂起导致 goroutine 泄漏、协程泄漏;服务要关闭时孤儿任务还在运行,半途打断导致数据不一致。这类问题在轻载下毫无症状,在长时间运行后进程内存持续增长、协程数/线程数不断攀升,最终 OOM 或超时告警。

Read the full file on GitHub · 268 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2mo ago First seen · 268 lines · 27 tokens per session scan A 33e683f19136

Subscribe to this mod's changes

concurrency-safety is a skill published in the GitHub repository Wade-DevCode/awesome-coding-skills-cn (7 stars, last pushed 5mo ago), licensed MIT. It adds 27 tokens to every session and 3,459 once invoked, about $0.0001 per session on Opus 5.5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

concurrency-safety compared with skills from other repositories

concurrency-safety next to 6 skills from other repositories
ModTypeStarsLast pushLicenceScanTokensWorks with
concurrency-safetyWade-DevCode/awesome-coding-skills-cnSkill75mo agoMITA27Claude Code, Codex
systematic-debuggingjnMetaCode/superpowers-zhSkill8.5k2d agoMITA24Claude Code
diagnosing-superpowersjnMetaCode/superpowers-zhSkill8.5k2d agoMITA111Claude Code
complexity-cutsfindscripter/everything-skillsSkill11todaynoneA111Claude Code
kaizen-continuous-improvementfindscripter/everything-skillsSkill11todaynoneA96Claude Code
cqrs-implementationfindscripter/everything-skillsSkill11todaynoneA111Claude Code
parallel-agent-dispatchfindscripter/everything-skillsSkill11todaynoneA128Claude Code