git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Don't fflush(stdout) when it's not helpful

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Jun 30, 2007, 04:24 UTC
Message-ID
<alpine.LFD.0.98.0706292114350.8675@woody.linux-foundation.org>
In-Reply-To
<7vlke2dw6w.fsf@assigned-by-dhcp.pobox.com>
On Fri, 29 Jun 2007, Junio C Hamano wrote:
Show 20 quoted lines
> 
> Do you mean this part?
> 
> +	/* Somebody closed stdout? */
> +	if (fstat(fileno(stdout), &st))
> +		return 0;
> +	/* Ignore write errors for pipes and sockets.. */
> +	if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
> +		return 0;
> +
> +	/* Check for ENOSPC and EIO errors.. */
> +	if (ferror(stdout))
> +		die("write failure on standard output");
> +	if (fflush(stdout) || fclose(stdout))
> +		die("write failure on standard output: %s", strerror(errno));
> +
> +	return 0;
> +}
> 
> I was planning to push this out to 'master' this weekend.

I think that code is fine, but switching the order around could probably make it less likely that stdio loses the errno for us.

So doing the last part in a different order, and making it say
	/* Check for ENOSPC and EIO errors.. */
	if (fflush(stdout))
		die("write failure on standard output: %s", strerror(errno));
	if (ferror(stdout))
		die("unknown write failure on standard output");
	if (fclose(stdout))
		die("close failed on standard output: %s", strerror(errno));
	return 0;
may recover at least non-transient errors.

It's still not perfect. As I've been harping on, stdio simply isn't very good for error reporting. For example, if an IO error happened, you'd want to see EIO, wouldn't you? And yes, that's what the kernel would return. However, with buffered stdio (and flushing outside of our control), what would likely happen is that some intermediate error return _does_ return EIO, but then the kernel might decide to re-mount the filesystem read-only due to the error, and the actual *report* for us might be

	"write failure on standard output: read-only filesystem"
which lost the EIO. 

But even worse, if the output happened to be buffer-aligned, stdio will have thrown the error out entirely, and the "fflush()" will return 0, and then we end up with that "unknown write failure" after all.

Or we might have had a ENOSPC at some point, but removed a temp-file, and the final fflush() doesn't error out at all, so now the incomplete write got done (with one or more buffer chunks missing), and we get "unknown write failure" again, because we again lost the ENOSPC.

So you basically cannot get "perfect" with stdio. It's impossible. But the above re-ordering will at least get you _closer_, and *most* of the time you'll get exactly the error you'd expect.

(I'm not a huge fan of "most of the time it works", but that's stdio for you).

		Linus
Previous: Junio C HamanoNext: Theodore Tso
Message 24 of 33 in “git-rev-list: give better diagnostic for failed write”
  1. git-rev-list: give better diagnostic for failed writeJim Meyering, Jun 25, 2007
  2. Linus TorvaldsJun 25, 2007
  3. Jim MeyeringJun 25, 2007
  4. Linus TorvaldsJun 25, 2007
  5. Linus TorvaldsJun 25, 2007
  6. Linus TorvaldsJun 25, 2007
  7. Jim MeyeringJun 27, 2007
  8. Linus TorvaldsJun 25, 2007
  9. Theodore TsoJun 26, 2007
  10. Linus TorvaldsJun 26, 2007
  11. Theodore TsoJun 26, 2007
  12. Linus TorvaldsJun 26, 2007
  13. Theodore TsoJun 28, 2007
  14. Jeff KingJun 28, 2007
  15. Don't fflush(stdout) when it's not helpfulTheodore Tso, Jun 28, 2007
  16. Frank LichtenheldJun 29, 2007
  17. Theodore TsoJun 29, 2007
  18. Jeff KingJun 29, 2007
  19. Junio C HamanoJun 29, 2007
  20. Linus TorvaldsJun 29, 2007
  21. Theodore TsoJun 29, 2007
  22. Linus TorvaldsJun 29, 2007
  23. Junio C HamanoJun 30, 2007
  24. Linus TorvaldsJun 30, 2007
  25. Theodore TsoJun 30, 2007
  26. Junio C HamanoJun 30, 2007
  27. Jeff KingJun 26, 2007
  28. Linus TorvaldsJun 26, 2007
  29. Jim MeyeringJun 27, 2007
  30. Linus TorvaldsJun 27, 2007
  31. Jim MeyeringJun 25, 2007
  32. Linus TorvaldsJun 25, 2007
  33. Jim MeyeringJun 25, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.