title: Python Selector with FIFO running to infinite loop
body: <p>I am trying to write some non-blocking FIFO code with kqueue on my BSD machine. Here's the small server code: <code>server.py</code></p>
  <pre><code>import os
  import selectors
  
  sel = selectors.KqueueSelector()
  
  TMP_PATH=&quot;/tmp/myfifo&quot;
  
  def fifo_read(fd, mask):
      data = os.read(fd, 8)
      print(&quot;fd:{} gives:{} \n&quot;, fd, data)
      sel.unregister(fd)
      print(&quot;unregistered&quot;)
  
  def fifo_accept(listen_fd, mask):
      print(&quot;accepted {}&quot;.format(listen_fd))
      fd = os.dup(listen_fd)
      print(&quot;duped to {}&quot;.format(fd))
      sel.register(fd, selectors.EVENT_READ, fifo_read)
  
  if __name__ == &quot;__main__&quot;:
      try:
          os.unlink(TMP_PATH)
      except:
          pass
  
      os.mkfifo(TMP_PATH)
      listen_fd = os.open(TMP_PATH, os.O_RDONLY, mode=0o600)
  
      sel.register(listen_fd, selectors.EVENT_READ, fifo_accept)
  
      while True:
          events = sel.select()
          for key, mask in events:
              cb = key.data
              cb(key.fileobj, mask)
  
      sel.close()
  </code></pre>
  <p>Now, when I run a <code>client.py</code> as:</p>
  <pre><code>import os
  
  TMP_PATH=&quot;/tmp/myfifo&quot;
  
  fd = os.open(TMP_PATH, os.O_WRONLY, mode=0o600)
  
  res = os.write(fd, b&quot;1234567&quot;)
  
  print(&quot;sent {}&quot;.format(res))
  </code></pre>
  <p>When I run the client, I get:
  <code>sent 7</code></p>
  <p>But on server, it runs to inifinite loop. Now I understand why the infinite loop is happening. I actually tried mimicking the socket way of using <code>selectors</code> in this <a href="https://docs.python.org/3/library/selectors.html#examples" rel="nofollow noreferrer">Python Docs example</a>.
  Here's what I have tried:</p>
  <ul>
  <li>I did try the code without duplicating the <code>fd</code>, but it's still in infinite loop.</li>
  <li>I tried calling <code>sel.unregister</code> on the original <code>listen_fd</code>, but in this case, running the client the second time doesn't work (which is expected).</li>
  </ul>
  <p>Can anyone please let me know if I'm missing something?</p>