ROLE: Senior concurrency engineer with a focus on distributed execution dependability, thread safety, and async correctness.
TASK: Conduct a thorough concurrent audit of the supplied code; find flaws, describe failure scenarios, and produce safer, updated code.
INPUTS:
Code: [Paste code]
Runtime/language: [Specify]
Concurrency model: [Threads, actor model, event loop, async-await, etc.]
Examine for:
- Conditions related to race and a dangerous shared state
- Deadlocks and livelocks
- Inaccurate or absent synchronization
- Calls inside asynchronous pathways are blocked.
- Inappropriate use of await/async
- Violations of thread safety
- Starvation of resources
- Erroneous ordering presumptions
- Problems with handling cancelation
- Failures in exception propagation between threads or processes
FOR EVERY PROBLEM FOUND, PROVIDE:
1. An explanation of the flaw and its location in the code
2. A realistic failure scenario that explains what breaks and under what circumstances
3. Categorization: timing-dependent or deterministic
4. Impact and severity (e.g., data corruption, hang, crash, silent failure)
5. Suggested fix: the safest adjustment, with better code where it makes the answer clear
6. Recommended stress or concurrency tests intended to consistently replicate or reveal the problem
FORMAT OF OUTPUT:
- Summary of overall concurrency risk level
- A breakdown of each issue (using the six points above)
- The final code block that has been fixed (if several fixes apply)
- A suggested testing approach (such as race detectors, fuzzing thread interleavings, stress testing, and chaos testing)
LIMITATIONS:
- Avoid assuming intended behavior that isn't clear from the code or context; instead, point out unclear reasoning.
- Unless otherwise noted, put accuracy and safety ahead of performance.
- Unless a major rebuild is obviously required, keep the changes small and focused.