title: Re-insert into channel causing deadlock
body: <p>I have a steady inbound flow of "jobs" that I feed into an unbuffered channel. I have a <code>for range</code> loop to iterate over the items and process them. If processing the item fails, I re-insert the item back into the channel so I can try again later.</p>
  
  <p>The problem is when I re-insert the item back into the channel - it deadlocks. I understand why it's happening: the processor isn't reading from the channel when it tries to send, thus the send blocks forever. But I can't think of a pattern to solve the problem. Can anybody assist with finding a solution?</p>
  
  <p>Here is a simple example code showing my problem (<a href="https://play.golang.org/p/N_-jWL5aOCo" rel="nofollow noreferrer">https://play.golang.org/p/N_-jWL5aOCo</a>):</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "fmt"
      "time"
  )
  
  type Job struct {
      ID       int
      Attempts int
  }
  
  func main() {
      ch := make(chan *Job)
      go fetchJobs(ch)
  
      for job := range ch {
          if success := processJob(job); !success {
              ch &lt;- job
          }
      }
  }
  
  func processJob(job *Job) bool {
      job.Attempts++
      fmt.Printf("Processing job %+v\n", job)
  
      // Simulate work.
      time.Sleep(time.Millisecond * 500)
  
      // Simulate failure on some jobs (IDs 10 to 19, 30 to 39, etc.)
      if job.ID%20 &gt;= 10 &amp;&amp; job.Attempts == 1 {
          return false
      }
  
      return true
  }
  
  func fetchJobs(ch chan *Job) {
      for i := 0; ; i++ {
          ch &lt;- &amp;Job{ID: i}
      }
  }
  
  </code></pre>