Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If fclose() fails, is the file descriptor still open?

Tags:

c

posix

fclose

Imagine the following code running as a thread:

void *thread_worker(void *q) {
 for (;;) {
  int fd = some_queue_get(q);
  FILE *writer = fdopen(fd, "w");
  if (!writer) { perror("fdopen"; close(fd); continue; }
  // do something with writer

  if (fclose(writer) == EOF) {
   perror("fclose writer");
   // should fd be closed here?
   close(fd);
  }
}

fclose(3) can fail for various reasons - is there a guarantee that/when the underlying file descriptor is closed or that it is still open afterwards?

  • if fd is not closed by fclose on a flushing failure, one leaks fds without an additional close.
  • if fd is closed by fclose, an additional close might close a file descriptor that was newly opened by an other thread.
like image 341
thejonny Avatar asked Feb 04 '21 15:02

thejonny


1 Answers

man fclose does not provide the answer on my system either, but man 3p fclose reveals the official version from the POSIX Programmer's manual, which is much more comprehensive on the matter:

The fclose() function shall perform the equivalent of a close() on the file descriptor that is associated with the stream pointed to by stream.

like image 113
Thomas Avatar answered Sep 18 '22 13:09

Thomas