Fix the WaitGroup close check in a worker pool
Your task
An AI agent wrote a Go worker pool. A producer sends jobs on one channel, workers send results on a second, and the caller ranges over the results. It only finishes cleanly when two things happen. The jobs channel gets closed. And one goroutine waits on the WaitGroup, until every worker is done, and then closes the results channel. You check a trace of the pool's channel operations against those rules.
An assistant wrote diagnose_worker_pool(events). It is in the editor. It has a bug. Find it and fix it.
Input and output:
events: the operations in the order they happen, each"<goroutine>:<action>".- The actions that matter are
send_jobs,close_jobs,wg_wait,close_resultsandrange_results. Any other action is ignored. - Return one of
"deadlock:jobs-never-closed","deadlock:results-never-closed","deadlock:close-after-range","panic:send-on-closed-results"or"ok".
Rules the tests check:
- Split each event at its first
:. The part before is the goroutine and the part after is the action. An event with no:is all goroutine, with an empty action. - Check in this order and return the first result that applies:
- No event has the action
close_jobs:"deadlock:jobs-never-closed". - No event has the action
close_results:"deadlock:results-never-closed". - Take the first
close_resultsevent. If an earlier event in the same goroutine has the actionrange_results:"deadlock:close-after-range". - If an earlier event in that same goroutine has the action
wg_wait:"ok". - Otherwise:
"panic:send-on-closed-results".
- No event has the action
- Goroutine names are compared exactly. A
wg_waitin any other goroutine does not count for step 4.
Examples
diagnose_worker_pool(events: list[str]) -> strExample 1
Input
diagnose_worker_pool(["producer:send_jobs", "producer:close_jobs", "main:range_results", "main:wg_wait"])Output
"deadlock:results-never-closed"Example 2
Input
diagnose_worker_pool(["producer:send_jobs", "producer:close_jobs", "closer:wg_wait", "closer:close_results", "main:range_results"])Output
"ok"Example 3
Input
diagnose_worker_pool(["producer:close_jobs", "main:range_results", "main:wg_wait", "main:close_results"])Output
"deadlock:close-after-range"