Fix the shutdown checker for a Go worker pool

Review the AI's codeEasy10 minGoogle and Anthropic style

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 written channel|goroutine|op, such as jobs|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, recv or close.
  • Every channel starts open.
  • close on an open channel closes it.
  • close on a closed channel is a violation, "close_of_closed:<channel>:<goroutine>". The channel stays closed.
  • send on a closed channel is a violation, "send_on_closed:<channel>:<goroutine>".
  • recv is never a violation, open or closed.

Examples

find_channel_close_violations(op_lines: list[str]) -> list[str]
Example 1
Inputfind_channel_close_violations(["jobs|worker_1|close", "jobs|worker_2|close", "jobs|worker_3|recv"])
Output["close_of_closed:jobs:worker_2"]
Example 2
Inputfind_channel_close_violations(["jobs|producer|send", "jobs|producer|close", "jobs|worker_1|recv"])
Output[]