Commit 1550cf6
committed
fix: prevent write_response_core crashes on Android
This fixes crashes in httplib::Server::write_response_core that occur
when writing HTTP responses to clients on Android devices. The crash
was caused by socket state issues when clients disconnect during
response transmission or when connections become invalid.
Root causes:
1. Long-lived keep-alive connections: Connections could stay open
indefinitely, increasing the chance of write failures when the
client disconnects unexpectedly
2. Socket buffering issues: Nagle's algorithm could delay small writes,
causing timeouts or connection issues
3. Slow connection cleanup: Idle connections weren't being closed
promptly, leading to stale socket state
4. Empty error responses: Some error responses had no body, which could
cause issues in write_response_core
Fixes:
1. Connection limiting:
- set_keep_alive_max_count(10): Limits requests per connection to
prevent indefinitely long-lived connections
- set_keep_alive_timeout(5s): Closes idle connections promptly to
avoid stale socket state
- set_idle_interval(1s): Improves connection cleanup on Android
2. Socket configuration:
- set_tcp_nodelay(true): Disables Nagle's algorithm to avoid
buffering delays during response writes, reducing the window
for client disconnects during transmission
3. Error handling:
- set_error_handler(): Ensures all error responses have content
and logs errors for debugging. Empty response bodies could
cause issues in write_response_core.
These settings reduce the likelihood of writing to invalid/closed
sockets, which was causing the SIGSEGV crashes in write_response_core
on Android devices.
Backtrace showed crash at:
httplib::Server::write_response_core
-> Socket write operations1 parent 3e306b1 commit 1550cf6
1 file changed
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
31 | 43 | | |
32 | 44 | | |
33 | 45 | | |
| |||
46 | 58 | | |
47 | 59 | | |
48 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
49 | 74 | | |
50 | 75 | | |
51 | 76 | | |
| |||
0 commit comments