summaryrefslogtreecommitdiff
path: root/.html/dev/shutdown-hooks.html
blob: 7917e8155a2e9b3da108b1c528bb6d4df27ac91b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
<head>
	<title>
		The Codex » 
		Falsehoods Programmers Believe About Shutdown Hooks
	</title>

	<link
		rel='stylesheet'
		type='text/css'
		href='http://fonts.googleapis.com/css?family=Buenard:400,700&amp;subset=latin,latin-ext'>
	<link
		rel="stylesheet"
		type="text/css"
		href="../media/css/reset.css">
	<link
		rel="stylesheet"
		type="text/css"
		href="../media/css/grimoire.css">
</head>
<body>

<div id="shell">
	
		<ol id="breadcrumbs">
			
				<li class="crumb-0 not-last">
					
						<a href="../">index</a>
					
				</li>
			
				<li class="crumb-1 not-last">
					
						<a href="./">dev</a>
					
				</li>
			
				<li class="crumb-2 last">
					
						shutdown-hooks
					
				</li>
			
		</ol>
	

	
	<div id="article">
		<h1 id="falsehoods-programmers-believe-about-shutdown-hooks">Falsehoods Programmers Believe About Shutdown Hooks</h1>
<p>Shutdown hooks are language features allowing programs to register callbacks to run during the underlying runtime's orderly teardown. For example:</p>
<ul>
<li>
<p>C's <a href="http://man7.org/linux/man-pages/man3/atexit.3.html"><code>atexit</code></a>,</p>
</li>
<li>
<p>Python's <a href="https://docs.python.org/library/atexit.html"><code>atexit</code></a>, which is subtly different,</p>
</li>
<li>
<p>Ruby's <a href="http://www.ruby-doc.org/core-2.1.3/Kernel.html#method-i-at_exit"><code>Kernel.at_exit</code></a>, which is different again,</p>
</li>
<li>
<p>Java's <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#addShutdownHook-java.lang.Thread-">Runtime.addShutdownHook</a>, which is yet again different</p>
</li>
</ul>
<p>(There's an example in your favourite language.)</p>
<p>The following beliefs are widespread and incorrect:</p>
<ol>
<li>
<p><strong>Your shutdown hook will run.</strong> Non-exhaustively: the power can go away. The OS may terminate the program immediately because of resource shortages. An administrator or process management tool may send <code>SIGKILL</code> to the process. All of these things, and others, will not run your shutdown hook.</p>
</li>
<li>
<p><strong>Your shutdown hook will run last.</strong> Look at the shapes of the various shutdown hook APIs above: they all allow multiple hooks to be registered in arbitrary orders, and at least one <em>outright requires</em> that hooks run concurrently.</p>
</li>
<li>
<p><strong>Your shutdown hook will not run last.</strong> Sometimes, you win, and objects your hook requires get cleaned up before your hook runs.</p>
</li>
<li>
<p><strong>Your shutdown hook will run to completion.</strong> Some languages run shutdown hooks even when the original termination request came from, for example, the user logging out. Most environments give programs a finite amount of time to wrap up before forcibly terminating them; your shutdown hook may well be mid-run when this occurs.</p>
</li>
<li>
<p><strong>Your shutdown hook will be the only thing running.</strong> In languages that support “daemon” threads, shutdown hooks may start before daemon threads terminate. In languages with concurrent shutdown hooks, other hooks will be in flight at the same time. On POSIX platforms, signals can still arrive during your shutdown hook. (Did you start any child processes? <code>SIGCHLD</code> can still arrive.)</p>
</li>
<li>
<p><strong>You need a shutdown hook.</strong> Closing files, terminating threads, and hanging up network connections are all done automatically by the OS as part of process destruction. The behaviour of the final few writes to a file handle aren't completely deterministic (unflushed data can be lost), but that's true even if a shutdown hook tries to close the file.</p>
</li>
</ol>
<p>Programs that rely on shutdown hooks for correctness should be treated as de-facto incorrect, much like object finalization in garbage-collected languages.</p>
	</div>


	
<div id="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'grimoire'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>


	
	<div id="footer">
		<p>
			
				The Codex —
			
			Powered by <a href="http://markdoc.org/">Markdoc</a>.
			
<a href="https://bitbucket.org/ojacobson/grimoire.ca/src/master/wiki/dev/shutdown-hooks.md">See this page on Bitbucket</a> (<a href="https://bitbucket.org/ojacobson/grimoire.ca/history-node/master/wiki/dev/shutdown-hooks.md">history</a>).

		</p>
	</div>
	
</div>
</body>
</html>