Fix the shutdown checker for a Go worker pool
Your task
An AI helping you build a Go worker pool insisted that channels are thread-safe, so several goroutines can close the same channel for a clean shutdown. In Go that panics. Your team now replays recorded channel operations through a checker before trusting a shutdown design.
An assistant wrote find_channel_close_violations(op_lines). It is in the editor. It has a bug. Find it and fix it.
Input and output:
op_lines: channel operations in the order they happened, each writtenchannel|goroutine|op, such asjobs|worker_1|close.- Return the list of violations, in the order they happen.
Rules the tests check:
- Split each line on
|. Skip a line with fewer than 3 parts. Only the first 3 parts are used. - Trim whitespace from both ends of each part. Skip the line if the channel or the goroutine is empty, or if the op is not exactly
send,recvorclose. - Every channel starts open.
closeon an open channel closes it.closeon a closed channel is a violation,"close_of_closed:<channel>:<goroutine>". The channel stays closed.sendon a closed channel is a violation,"send_on_closed:<channel>:<goroutine>".recvis never a violation, open or closed.
Examples
find_channel_close_violations(op_lines: list[str]) -> list[str]Example 1
Input
find_channel_close_violations(["jobs|worker_1|close", "jobs|worker_2|close", "jobs|worker_3|recv"])Output
["close_of_closed:jobs:worker_2"]Example 2
Input
find_channel_close_violations(["jobs|producer|send", "jobs|producer|close", "jobs|worker_1|recv"])Output
[]