title: lseek/write suddenly returns -1 with errno = 9 (Bad file descriptor)
body: <p>My application uses <code>lseek()</code> to seek the desired position to write data.
  The file is successfully opened using <code>open()</code> and my application was able to use <code>lseek()</code> and <code>write()</code> lots of times.</p>
  
  <p>At a given time, for some users and not easily reproducable, <code>lseek()</code> returns -1 with an <code>errno</code> of 9. File is not closed before this and the filehandle (int) isn't reset.</p>
  
  <p>After this, another file is created; <code>open()</code> is okay again and <code>lseek()</code> and <code>write()</code> works again.</p>
  
  <p>To make it even worse, this user tried the complete sequence again and all was well.</p>
  
  <p>So my question is, can the OS close the file handle for me for some reason?
  What could cause this? A file indexer or file scanner of some sort?</p>
  
  <p>What is the best way to solve this; is this pseudo code the best solution?
  (never mind the code layout, will create functions for it)</p>
  
  <pre><code>int fd=open(...);
  if (fd&gt;-1) {
    long result = lseek(fd,....);
    if (result == -1 &amp;&amp; errno==9) {
        close(fd..); //make sure we try to close nicely
        fd=open(...);
  
        result = lseek(fd,....);
    }
  }
  </code></pre>
  
  <p>Anybody experience with something similar?</p>
  
  <p>Summary: file seek and write works okay for a given fd and suddenly gives back errno=9 without a reason.</p>