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
//! Tells the process-wide panic hook that a panic is *expected* and will
//! be caught, so it doesn't tear the terminal down on the way past.
//!
//! # The problem this exists for
//!
//! `main` installs a panic hook that calls [`restore`](super::restore) —
//! leaving the alternate screen and raw mode — and then chains to the
//! default hook, which prints the payload to stderr. That is exactly
//! right for a panic that ends the process: without it the user is left
//! at a shell with raw mode still on.
//!
//! It is exactly wrong for a panic wrapped in `catch_unwind`. The hook
//! runs *before* unwinding, so it fires whether or not anyone is going to
//! catch the panic, and it has no way to tell the two apart. A caught
//! panic therefore left the app **still running** on a terminal that had
//! been restored out from under it: no alt screen, no raw mode, panic
//! text on the scrollback, and no way back short of killing the process.
//! Strictly worse than the clean crash the hook was written to prevent.
//!
//! Seven call sites are wrapped in `catch_unwind` and all seven had it:
//! [`markdown::highlight`](crate::markdown::highlight)'s tokenizer and
//! its warm worker, [`diagram::mermaid`](crate::diagram::mermaid)'s
//! renderer and its font warmup, the image decode worker, its
//! scratch-encode step, and the `Picker::from_query_stdio` probe. The
//! highlighter is the one that made it worth fixing — it runs
//! synchronously on the render thread over attacker-controlled code-block
//! text, through a backtracking regex engine, which is the likeliest of
//! the seven to actually fire.
//!
//! # Scope it to the `catch_unwind`, and nothing else
//!
//! A guard left live over the code *after* the catch claims a panic is
//! expected when nobody is going to catch it — so the hook stands down
//! and the terminal is never restored, which is the original defect with
//! the sign flipped. On a worker thread that costs a silent death; on
//! the main thread (`detect_image_protocol` runs there, after the hook is
//! installed) it means unwinding out of `main` with the alternate screen
//! still up and no message printed. Bind the guard in the same
//! expression or block as the `catch_unwind` it belongs to.
//!
//! # Why the counter is thread-local
//!
//! The hook runs on the panicking thread, so a thread-local answers the
//! question that is actually being asked: "is *this* thread inside a
//! guarded section?" A process-global flag would let one thread's
//! guarded section silence an unrelated thread's genuine crash — the
//! image and encode workers run concurrently with the render thread, so
//! that overlap is the normal case rather than a corner.
//!
//! It is a counter rather than a flag because the guarded sections nest:
//! `image_dispatch`'s guarded decode calls `diagram::render_mermaid_svg`,
//! which guards a `catch_unwind` of its own. A flag would be cleared by
//! the inner section's exit and leave the rest of the outer one exposed.
use Cell;
thread_local!
/// Marks the current thread as being inside a `catch_unwind` for as long
/// as it is alive. Create one immediately before the `catch_unwind` that
/// will do the catching:
///
/// ```ignore
/// let _guard = ExpectedPanic::new();
/// catch_unwind(AssertUnwindSafe(|| risky()))
/// ```
///
/// `Drop` runs during unwinding — after the hook, before `catch_unwind`
/// returns — so the count is correct again by the time control comes
/// back, and a nested guard is restored rather than cleared.
);
/// `try_with`, never `with`: this runs during unwinding and during thread
/// teardown, where the thread-local may already be destroyed and `with`
/// would panic — inside a panic, which aborts.
/// Is the panicking thread inside a [`ExpectedPanic`] guard?
///
/// Answers `false` when the thread-local is unavailable, so an
/// unexpected panic during thread teardown still restores the terminal.
/// Getting this wrong in that direction merely prints a stack trace;
/// getting it wrong in the other leaves a live TUI on a dead terminal.