<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://vegard.wiki/mediawiki/index.php?action=history&amp;feed=atom&amp;title=close%28%29</id>
	<title>close() - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://vegard.wiki/mediawiki/index.php?action=history&amp;feed=atom&amp;title=close%28%29"/>
	<link rel="alternate" type="text/html" href="http://vegard.wiki/mediawiki/index.php?title=close()&amp;action=history"/>
	<updated>2026-07-04T21:31:38Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.1</generator>
	<entry>
		<id>http://vegard.wiki/mediawiki/index.php?title=close()&amp;diff=68&amp;oldid=prev</id>
		<title>Vegard: fix line wrapping</title>
		<link rel="alternate" type="text/html" href="http://vegard.wiki/mediawiki/index.php?title=close()&amp;diff=68&amp;oldid=prev"/>
		<updated>2019-12-18T06:48:56Z</updated>

		<summary type="html">&lt;p&gt;fix line wrapping&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 06:48, 18 December 2019&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;
  &lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;
  &lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;From the [https://github.com/mkerrisk/man-pages/blob/5373f62f1e4352e665c24dfe49b7e3fe03721cab/man2/close.2#L134 current Linux man page]:&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;From the [https://github.com/mkerrisk/man-pages/blob/5373f62f1e4352e665c24dfe49b7e3fe03721cab/man2/close.2#L134 current Linux man page]:&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;blockquote&amp;gt;&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;blockquote&amp;gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       A careful programmer will check the return value of &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;, since it is quite &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;possi‐&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       A careful programmer will check the return value of &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;, since it is quite &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;possible&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;      &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt; ble&lt;/del&gt; that errors on a previous &amp;lt;tt&amp;gt;write(2)&amp;lt;/tt&amp;gt; operation  are  reported  only  on  the  final&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       that errors on a previous &amp;lt;tt&amp;gt;write(2)&amp;lt;/tt&amp;gt; operation  are  reported  only  on  the  final&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;  that  releases the open file description.  Failing to check the return value&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;  that  releases the open file description.  Failing to check the return value&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       when closing a file may lead to silent loss of data.  This can especially be observed&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       when closing a file may lead to silent loss of data.  This can especially be observed&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;
  &lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       Note,  however,  that  a  failure  return should be used only for diagnostic purposes&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       Note,  however,  that  a  failure  return should be used only for diagnostic purposes&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       (i.e., a warning to the application that there may still be I/O pending or there  may&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       (i.e., a warning to the application that there may still be I/O pending or there  may&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       have  been failed I/O) or remedial purposes (e.g., writing the file once more or &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;cre‐&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       have  been failed I/O) or remedial purposes (e.g., writing the file once more or &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;creating&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;      &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt; ating&lt;/del&gt; a backup).&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       a backup).&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br /&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       Retrying the &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt; after a failure return is the wrong thing to do, since this  may&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       Retrying the &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt; after a failure return is the wrong thing to do, since this  may&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       cause  a  reused  file  descriptor  from another thread to be closed.  This can occur&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       cause  a  reused  file  descriptor  from another thread to be closed.  This can occur&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       because the Linux kernel always releases the file descriptor early in the close &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;oper‐&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       because the Linux kernel always releases the file descriptor early in the close &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;operation,&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;  ation,&lt;/del&gt;  freeing  it  for  reuse; the steps that may return an error, such as flushing&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       freeing  it  for  reuse; the steps that may return an error, such as flushing&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       data to the filesystem or device, occur only later in the close operation.&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       data to the filesystem or device, occur only later in the close operation.&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br /&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       Many other implementations similarly always close the file descriptor (except in  the&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       Many other implementations similarly always close the file descriptor (except in  the&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       case  of  &amp;lt;tt&amp;gt;EBADF&amp;lt;/tt&amp;gt;,  meaning  that  the file descriptor was invalid) even if they &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;subse‐&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       case  of  &amp;lt;tt&amp;gt;EBADF&amp;lt;/tt&amp;gt;,  meaning  that  the file descriptor was invalid) even if they &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;subsequently&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;      &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt; quently&lt;/del&gt; report an error on return from &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;.  POSIX.1 is currently silent on  this&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       report an error on return from &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;.  POSIX.1 is currently silent on  this&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       point,  but there are plans to mandate this behavior in the next major release of the&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       point,  but there are plans to mandate this behavior in the next major release of the&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       standard&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;       standard&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;!-- diff cache key mediawiki:diff:1.41:old-67:rev-68:wikidiff2=table:1.13.0:bc2a06be --&gt;
&lt;/table&gt;</summary>
		<author><name>Vegard</name></author>
	</entry>
	<entry>
		<id>http://vegard.wiki/mediawiki/index.php?title=close()&amp;diff=67&amp;oldid=prev</id>
		<title>Vegard: new page</title>
		<link rel="alternate" type="text/html" href="http://vegard.wiki/mediawiki/index.php?title=close()&amp;diff=67&amp;oldid=prev"/>
		<updated>2019-12-18T06:47:42Z</updated>

		<summary type="html">&lt;p&gt;new page&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt; is notoriously difficult to get right. You are supposed to check the return value, but not really handle it (other than printing a diagnostic). If you are wrapping a file descriptor using RAII, then you may not be able to throw an exception or return an error if you are calling &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt; from the destructor.&lt;br /&gt;
&lt;br /&gt;
From the [https://github.com/mkerrisk/man-pages/blob/5373f62f1e4352e665c24dfe49b7e3fe03721cab/man2/close.2#L134 current Linux man page]:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
       A careful programmer will check the return value of &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;, since it is quite possi‐&lt;br /&gt;
       ble that errors on a previous &amp;lt;tt&amp;gt;write(2)&amp;lt;/tt&amp;gt; operation  are  reported  only  on  the  final&lt;br /&gt;
       &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;  that  releases the open file description.  Failing to check the return value&lt;br /&gt;
       when closing a file may lead to silent loss of data.  This can especially be observed&lt;br /&gt;
       with NFS and with disk quota.&lt;br /&gt;
&lt;br /&gt;
       Note,  however,  that  a  failure  return should be used only for diagnostic purposes&lt;br /&gt;
       (i.e., a warning to the application that there may still be I/O pending or there  may&lt;br /&gt;
       have  been failed I/O) or remedial purposes (e.g., writing the file once more or cre‐&lt;br /&gt;
       ating a backup).&lt;br /&gt;
&lt;br /&gt;
       Retrying the &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt; after a failure return is the wrong thing to do, since this  may&lt;br /&gt;
       cause  a  reused  file  descriptor  from another thread to be closed.  This can occur&lt;br /&gt;
       because the Linux kernel always releases the file descriptor early in the close oper‐&lt;br /&gt;
       ation,  freeing  it  for  reuse; the steps that may return an error, such as flushing&lt;br /&gt;
       data to the filesystem or device, occur only later in the close operation.&lt;br /&gt;
&lt;br /&gt;
       Many other implementations similarly always close the file descriptor (except in  the&lt;br /&gt;
       case  of  &amp;lt;tt&amp;gt;EBADF&amp;lt;/tt&amp;gt;,  meaning  that  the file descriptor was invalid) even if they subse‐&lt;br /&gt;
       quently report an error on return from &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;.  POSIX.1 is currently silent on  this&lt;br /&gt;
       point,  but there are plans to mandate this behavior in the next major release of the&lt;br /&gt;
       standard&lt;br /&gt;
&lt;br /&gt;
       A careful programmer who wants to know about I/O errors may precede  &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;  with  a&lt;br /&gt;
       call to &amp;lt;tt&amp;gt;fsync(2)&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, best practice would seem to be:&lt;br /&gt;
* Call &amp;lt;tt&amp;gt;fsync()&amp;lt;/tt&amp;gt; at some point after writing data, but before &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;; this should report any I/O errors&lt;br /&gt;
* Only use &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt; to actually destroy the file descriptor when the file descriptor is no longer in use&lt;br /&gt;
* On Linux, the file descriptor is always closed regardless of the return value (and should therefore not be retried); on other systems, &amp;lt;tt&amp;gt;EINTR&amp;lt;/tt&amp;gt; may signal that the file descriptor was &amp;#039;&amp;#039;not&amp;#039;&amp;#039; closed&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- WIP&lt;br /&gt;
&amp;lt;source lang=&amp;quot;C++&amp;quot;&amp;gt;&lt;br /&gt;
struct file {&lt;br /&gt;
    int fd;&lt;br /&gt;
    bool flushed;&lt;br /&gt;
&lt;br /&gt;
    file(const char *path):&lt;br /&gt;
        flushed(false)&lt;br /&gt;
    {&lt;br /&gt;
        fd = open(path, ...);&lt;br /&gt;
        if (fd == -1)&lt;br /&gt;
            throw errno;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void flush()&lt;br /&gt;
    {&lt;br /&gt;
        flushed = true;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void read(/* ... */)&lt;br /&gt;
    {&lt;br /&gt;
        assert(!flushed);&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void write(/* ... */)&lt;br /&gt;
    {&lt;br /&gt;
        assert(!flushed);&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    ~file()&lt;br /&gt;
    {&lt;br /&gt;
        assert(flushed);&lt;br /&gt;
&lt;br /&gt;
        while (true) {&lt;br /&gt;
            if (close(fd) == 0)&lt;br /&gt;
                return;&lt;br /&gt;
&lt;br /&gt;
            if (errno == EIO) {&lt;br /&gt;
                fprintf(stderr, &amp;quot;warning: close() returned I/O errors, data may have been lost&amp;quot;);&lt;br /&gt;
            } else if (errno == EINTR) {&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Programming]]&lt;br /&gt;
[[Category:C]]&lt;/div&gt;</summary>
		<author><name>Vegard</name></author>
	</entry>
</feed>