forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atexit.jl
265 lines (263 loc) · 10.6 KB
/
atexit.jl
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Test
@testset "atexit.jl" begin
function _atexit_tests_gen_cmd_eval(expr::String)
# We run the atexit tests with 2 threads, for the parallelism tests at the end.
cmd_eval = ```
$(Base.julia_cmd()) -t2 -e $(expr)
```
return cmd_eval
end
function _atexit_tests_gen_cmd_script(temp_dir::String, expr::String)
script, io = mktemp(temp_dir)
println(io, expr)
close(io)
# We run the atexit tests with 2 threads, for the parallelism tests at the end.
cmd_script = ```
$(Base.julia_cmd()) -t2 $(script)
```
return cmd_script
end
atexit_temp_dir = mktempdir()
atexit(() -> rm(atexit_temp_dir; force = true, recursive = true))
@testset "these should exit with exit code 0" begin
julia_expr_list = Dict(
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> exit(0))
exit(22)
""" => 0,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(exitcode -> exitcode > 10 && exit(0))
exit(22)
""" => 0,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
)
for julia_expr in keys(julia_expr_list)
cmd_eval = _atexit_tests_gen_cmd_eval(julia_expr)
cmd_script = _atexit_tests_gen_cmd_script(atexit_temp_dir, julia_expr)
expected_exit_code = julia_expr_list[julia_expr]
@test success(cmd_eval)
@test success(cmd_script)
p_eval = run(cmd_eval; wait = false)
p_script = run(cmd_script; wait = false)
wait(p_eval)
wait(p_script)
@test p_eval.exitcode == expected_exit_code
@test p_script.exitcode == expected_exit_code
end
end
@testset "these should exit with exit code 1" begin
julia_expr_list = Dict(
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> exit(1))
exit(22)
""" => 1,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> ("No error"))
atexit(() -> exit(1))
exit(22)
""" => 1,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> exit(1))
atexit(() -> exit(1))
exit(22)
""" => 1,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
)
for julia_expr in keys(julia_expr_list)
cmd_eval = _atexit_tests_gen_cmd_eval(julia_expr)
cmd_script = _atexit_tests_gen_cmd_script(atexit_temp_dir, julia_expr)
expected_exit_code = julia_expr_list[julia_expr]
@test_throws ProcessFailedException run(cmd_eval)
@test_throws ProcessFailedException run(cmd_script)
p_eval = run(cmd_eval; wait = false)
p_script = run(cmd_script; wait = false)
wait(p_eval)
wait(p_script)
@test p_eval.exitcode == expected_exit_code
@test p_script.exitcode == expected_exit_code
end
end
@testset "test exit codes other than 0 or 1" begin
julia_expr_list = Dict(
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> exit(13))
exit(22)
""" => 13,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(exitcode -> exit(exitcode+3))
exit(22)
""" => 25,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> ("No error"))
atexit(() -> exit(5))
exit(22)
""" => 5,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> exit(33))
atexit(() -> exit(33))
exit(22)
""" => 33,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> exit(21))
atexit(() -> exit(21))
exit(22)
""" => 21,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
)
for julia_expr in keys(julia_expr_list)
cmd_eval = _atexit_tests_gen_cmd_eval(julia_expr)
cmd_script = _atexit_tests_gen_cmd_script(atexit_temp_dir, julia_expr)
expected_exit_code = julia_expr_list[julia_expr]
@test_throws(ProcessFailedException, run(cmd_eval))
@test_throws(ProcessFailedException, run(cmd_script))
p_eval = run(cmd_eval; wait = false)
p_script = run(cmd_script; wait = false)
wait(p_eval)
wait(p_script)
@test p_eval.exitcode == expected_exit_code
@test p_script.exitcode == expected_exit_code
end
end
@testset "test what happens if multiple places call exit(n)" begin
julia_expr_list = Dict(
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> exit(22))
atexit(() -> exit(11))
atexit(() -> exit(33))
""" => 22,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> exit(4))
atexit(() -> exit(16))
atexit(() -> exit(7))
exit(22)
""" => 4,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(() -> exit(21))
atexit(exitcode -> exit(exitcode+3))
exit(22)
""" => 21,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
atexit(exitcode -> exit(exitcode+3))
atexit(() -> exit(21))
exit(22)
""" => 24,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
)
for julia_expr in keys(julia_expr_list)
cmd_eval = _atexit_tests_gen_cmd_eval(julia_expr)
cmd_script = _atexit_tests_gen_cmd_script(atexit_temp_dir, julia_expr)
expected_exit_code = julia_expr_list[julia_expr]
@test_throws(ProcessFailedException, run(cmd_eval))
@test_throws(ProcessFailedException, run(cmd_script))
p_eval = run(cmd_eval; wait = false)
p_script = run(cmd_script; wait = false)
wait(p_eval)
wait(p_script)
@test p_eval.exitcode == expected_exit_code
@test p_script.exitcode == expected_exit_code
end
end
@testset "test calling atexit() in parallel with running atexit hooks." begin
# These tests cover 3 parallelism cases, as described by the following comments.
julia_expr_list = Dict(
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 1. registering a hook from inside a hook
"""
atexit() do
atexit() do
exit(11)
end
end
# This will attempt to exit 0, but the execution of the atexit hook will
# register another hook, which will exit 11.
exit(0)
""" => 11,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 2. registering a hook from another thread while hooks are running
"""
c = Channel()
# This hook must execute _last_. (Execution is LIFO.)
atexit() do
put!(c, nothing)
put!(c, nothing)
end
atexit() do
# This will run in a concurrent task, testing that we can register atexit
# hooks from another task while running atexit hooks.
Threads.@spawn begin
take!(c) # block on c
atexit() do
exit(11)
end
take!(c) # keep the _atexit() loop alive until we've added another item.
end
end
exit(0)
""" => 11,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 3. attempting to register a hook after all hooks have finished (disallowed)
"""
const atexit_has_finished = Threads.Atomic{Int}(0)
atexit() do
Threads.@spawn begin
# Block until the atexit hooks have all finished. We use a manual "spin
# lock" because task switch is disallowed inside the finalizer, below.
atexit_has_finished[] = 1
while atexit_has_finished[] == 1; GC.safepoint(); end
try
# By the time this runs, all the atexit hooks will be done.
# So this will throw.
atexit() do
exit(11)
end
catch
# Meaning we _actually_ exit 22.
exit(22)
end
end
while atexit_has_finished[] == 0; GC.safepoint(); end
end
# Finalizers run after the atexit hooks, so this blocks exit until the spawned
# task above gets a chance to run.
x = []
finalizer(x) do x
# Allow the spawned task to finish
atexit_has_finished[] = 2
# Then spin forever to prevent exit.
while atexit_has_finished[] == 2; GC.safepoint(); end
end
exit(0)
""" => 22,
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
)
for julia_expr in keys(julia_expr_list)
cmd_eval = _atexit_tests_gen_cmd_eval(julia_expr)
cmd_script = _atexit_tests_gen_cmd_script(atexit_temp_dir, julia_expr)
expected_exit_code = julia_expr_list[julia_expr]
@test_throws(ProcessFailedException, run(cmd_eval))
@test_throws(ProcessFailedException, run(cmd_script))
p_eval = run(cmd_eval; wait = false)
p_script = run(cmd_script; wait = false)
wait(p_eval)
wait(p_script)
@test p_eval.exitcode == expected_exit_code
@test p_script.exitcode == expected_exit_code
end
end
rm(atexit_temp_dir; force = true, recursive = true)
end