Commit 71cb002
Attempt to fix Android app crashes with C++ library (#455)
* fix: prevent SIGSEGV crash in httplib HTTP server on Android
This fixes a crash that was affecting ~950 users with the following
root causes:
1. Chunked transfer encoding issues: Replace ContentProviderWithoutLength
with buffered content using set_content(). The streaming content
provider could crash when:
- Client disconnects during transfer
- Exceptions thrown during content generation
- Server stopped while requests in-flight
2. Unhandled exceptions: Add try-catch blocks around request handling
to gracefully handle exceptions instead of letting them propagate
into httplib's internals where they caused SIGSEGV.
3. Race condition in stop(): Reorder operations to call m_server.stop()
before clear() so that in-flight requests complete before resources
are invalidated.
The crash was reported as SIGSEGV in
httplib::Server::write_response_core on various Android devices and
versions (Android 10-16, Samsung, Motorola devices).
* fix: prevent crash in httplib process_request during shutdown
Additional fixes for Android crashes in httplib HTTP server:
1. Add destructor to Impl class: Ensures the server is properly stopped
before the Impl object is destroyed. This prevents worker threads
from accessing freed memory (use-after-free crash).
2. Add atomic stopping flag: Handlers check this flag and return 503
Service Unavailable during shutdown, preventing new work from
starting while resources are being freed.
3. Set up httplib exception handler: Catches any internal httplib
exceptions and returns HTTP 500 instead of crashing.
4. Change lambda captures from [&] to [this]: More explicit about what's
captured, making the code's intent clearer.
5. Delete copy constructor/assignment: Prevents unsafe copying since
lambdas capture 'this' pointer.
The second crash was occurring in std::__tree::__assign_multi during
httplib::Server::process_request, caused by accessing freed memory when
the Impl object was destroyed while worker threads were still running.
* fix: ensure thread pool is joined before resources are destroyed
Root cause: C++ member destruction order was causing use-after-free.
The m_content map was being destroyed BEFORE m_server, but m_server's
destructor is what joins the thread pool threads. This meant worker
threads could still be accessing m_content after it was destroyed.
Crashes were occurring in httplib's internal map operations
(__construct_node, __assign_multi) during process_request because
threads were accessing freed memory.
Fix:
1. Changed m_server to unique_ptr<httplib::Server> to enable explicit
destruction timing control.
2. In destructor, explicitly destroy server via m_server.reset() BEFORE
other members are destroyed. This ensures thread pool threads are
fully joined first.
3. In stop(), also destroy the server to ensure threads are joined
before clearing content.
4. Reordered member declarations: m_server is now declared LAST so if
we miss any explicit destruction, the natural destruction order will
still destroy it first (reverse of declaration order).
This is a critical fix for the SIGSEGV crashes in httplib's internal
map operations during request processing on Android.
* format
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>1 parent 53593cc commit 71cb002
1 file changed
Lines changed: 140 additions & 42 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
11 | 15 | | |
12 | 16 | | |
13 | 17 | | |
14 | 18 | | |
15 | 19 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 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 | + | |
30 | 81 | | |
31 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
32 | 87 | | |
33 | 88 | | |
34 | 89 | | |
35 | 90 | | |
36 | 91 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
46 | 115 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | 116 | | |
52 | 117 | | |
53 | 118 | | |
| |||
60 | 125 | | |
61 | 126 | | |
62 | 127 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
74 | 149 | | |
75 | 150 | | |
76 | 151 | | |
| |||
88 | 163 | | |
89 | 164 | | |
90 | 165 | | |
91 | | - | |
| 166 | + | |
92 | 167 | | |
93 | 168 | | |
94 | 169 | | |
| |||
107 | 182 | | |
108 | 183 | | |
109 | 184 | | |
110 | | - | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
111 | 198 | | |
112 | | - | |
| 199 | + | |
| 200 | + | |
113 | 201 | | |
114 | 202 | | |
115 | 203 | | |
116 | 204 | | |
117 | 205 | | |
118 | 206 | | |
119 | 207 | | |
120 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
121 | 211 | | |
122 | 212 | | |
123 | 213 | | |
| |||
126 | 216 | | |
127 | 217 | | |
128 | 218 | | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
129 | 227 | | |
130 | 228 | | |
131 | 229 | | |
| |||
0 commit comments