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
266
267
268
269
270
271
272
273
274
275
276
// Connection Cleanup Verification
//
// This document outlines how Synclaire ensures proper cleanup of resources
// when connections close, including verification of Drop implementations.
/// CLEANUP FLOW ANALYSIS:
///
/// 1. CONNECTION ACCEPTED
/// - TcpStream acquired from listener.accept()
/// - Semaphore permit acquired (limited by max_connections)
/// - GuardSession created (tracks connection state)
/// - Connection wrapper created (owns AsyncStream/SyncStream)
///
/// 2. CONNECTION ACTIVE
/// - Connection held by handler task/thread
/// - GuardSession tracks lifecycle with on_reserve/on_payload/on_close
/// - Guard observers monitor for attacks
///
/// 3. CONNECTION CLOSES (normal or error)
/// - Handler returns Ok/Err
/// - Connection dropped (via handler return or panic unwind)
/// - → Connection::drop() called
/// → GuardSession::close() called
/// → Guard observers receive on_close event
/// - AsyncStream/SyncStream dropped
/// → Underlying TcpStream dropped (async: tokio cleanup, sync: close)
/// → TLS wrapper (if any) dropped (flushes + closes)
/// - Semaphore permit dropped
/// → max_connections counter decremented
/// → Next pending connection (if any) can proceed
///
/// 4. RESOURCE STATE AFTER CLOSE
/// - All file descriptors closed (TCP socket)
/// - All TLS state cleared (cipher, keys, session)
/// - All per-IP metrics updated (connection close recorded)
/// - Guard tracking cleared (connection removed from tracking)
///
/// DETAILED CLEANUP VERIFICATION:
///
/// ┌─────────────────────────────────────────────────────────┐
/// │ AsyncStream Ownership & Cleanup │
/// └─────────────────────────────────────────────────────────┘
///
/// AsyncStream variants (all own the underlying socket):
///
/// enum AsyncStream {
/// Tcp(tokio::net::TcpStream), ← owns TcpStream
/// ServerTls(tokio_rustls::server::TlsStream<...>) ← owns TlsStream(owns TcpStream)
/// ClientTls(tokio_rustls::client::TlsStream<...>) ← owns TlsStream(owns TcpStream)
/// }
///
/// When AsyncStream is dropped:
/// 1. Match arm is dropped
/// 2. TcpStream (or TlsStream wrapper) is dropped
/// 3. Tokio TcpStream::drop() called:
/// - OS socket handle closed
/// - All pending I/O cancelled
/// - Memory freed
///
/// TLS wrapper cleanup (via TlsStream::drop):
/// - Flushes any pending data
/// - Closes TLS session gracefully (if possible)
/// - Clears sensitive keying material
/// - Drops underlying TcpStream
///
/// ┌─────────────────────────────────────────────────────────┐
/// │ Connection Drop Implementation │
/// └─────────────────────────────────────────────────────────┘
///
/// From src/handler.rs:
///
/// impl Drop for Connection {
/// fn drop(&mut self) {
/// if let Some(session) = &self.guard_session {
/// session.close(); ← Notify guard system
/// }
/// }
/// }
///
/// Guarantees:
/// - GuardSession::close() ALWAYS called (even on panic)
/// - Guard lifecycle properly terminated
/// - Per-IP tracking updated
/// - Attack detection counters decremented
///
/// ┌─────────────────────────────────────────────────────────┐
/// │ GuardSession Cleanup │
/// └─────────────────────────────────────────────────────────┘
///
/// When GuardSession::close() is called:
///
/// 1. Each guard in stack receives on_close() event:
/// - SynGuard: Decrement half-open count for this IP
/// - RateLimiter: Update token bucket final state
/// - Throttle: Decrement active connection count per IP
/// - SlowLoris: Remove idle timer for this connection
/// - IpBan: (no state to clean)
///
/// 2. PerIpMetrics updated:
/// - active_connections counter decremented
/// - Latency recorded (if enabled)
/// - failures counter updated (if closed abnormally)
///
/// 3. No hanging resources:
/// - All timers cancelled
/// - All counters decremented
/// - All per-connection state freed
///
/// ┌─────────────────────────────────────────────────────────┐
/// │ Semaphore Permit Cleanup │
/// └─────────────────────────────────────────────────────────┘
///
/// From src/server/async_server.rs:
///
/// let permit = semaphore.clone().try_acquire_owned()?; ← Guard limit
///
/// tokio::spawn(async move {
/// let result = handle_async_connection(...).await;
/// // ... error logging ...
/// drop(permit); ← EXPLICIT DROP
/// });
///
/// When permit is dropped:
/// - Semaphore counter incremented
/// - Next pending connection (if any) can proceed
/// - max_connections limit properly maintained
///
/// Even on panic unwind:
/// - permit is still owned by the task
/// - Task cleanup drops all owned values
/// - permit is dropped
/// - Semaphore counter incremented
///
/// ┌─────────────────────────────────────────────────────────┐
/// │ Memory & File Descriptor Cleanup │
/// └─────────────────────────────────────────────────────────┘
///
/// What gets freed/closed:
///
/// ✓ TCP Socket File Descriptor
/// - OS resource immediately released
/// - Any pending send/receive cancelled
/// - Backlog entry removed
///
/// ✓ TLS Session State
/// - Cipher suites cleared
/// - Master keys zeroed (if supported by rustls)
/// - Session resumption ticket discarded
/// - Certificate references dropped
///
/// ✓ Buffer Memory
/// - Read/write buffers dropped
/// - Connection-local allocations freed
/// - Guard tracking data freed
///
/// ✓ Metrics Data
/// - Per-connection latency recorded
/// - Per-IP active count decremented
/// - Guard counters updated
///
/// ✓ Task/Thread Resources
/// - Handler task/thread completes
/// - All locals dropped
/// - Arc references dropped
///
/// ┌─────────────────────────────────────────────────────────┐
/// │ Panic Safety │
/// └─────────────────────────────────────────────────────────┘
///
/// Even if handler panics:
///
/// async mode:
/// - tokio::spawn task unwinds
/// - All owned values dropped (Connection, permit, stream)
/// - GuardSession::close() called via Connection::drop()
/// - Semaphore permit dropped
/// - No resource leaks
///
/// sync mode:
/// - Handler thread panics
/// - Stack unwinding calls all Drop impls
/// - Connection cleaned up
/// - Permit released
/// - Thread exits (can be caught by thread::JoinHandle)
///
/// ┌─────────────────────────────────────────────────────────┐
/// │ Test: Connection Cleanup │
/// └─────────────────────────────────────────────────────────┘
///
/// To verify cleanup in tests:
///
/// 1. Create connection
/// 2. Let it go out of scope or explicitly drop
/// 3. Verify:
/// - GuardSession::close() was called (via metrics or callbacks)
/// - Socket is closed (e.g., no pending data)
/// - Guard counters decremented
/// - Per-IP active count decremented
///
/// Example scenarios tested:
/// - Normal connection close (handler returns Ok)
/// - Error close (handler returns Err)
/// - Guard rejection (connection dropped immediately)
/// - Handler panic (unwind cleanup)
/// - Max connections reached (permit never acquired)
///
/// ┌─────────────────────────────────────────────────────────┐
/// │ SUMMARY: No Resource Leaks │
/// └─────────────────────────────────────────────────────────┘
///
/// Guarantees:
/// ✓ Every accepted connection is eventually closed
/// ✓ Every acquired semaphore permit is released
/// ✓ Every TLS session is flushed and closed
/// ✓ Every TCP socket is closed
/// ✓ Every guard session is notified
/// ✓ Every file descriptor is released
/// ✓ Every per-IP counter is updated
/// ✓ Even if handler panics or errors
///
/// Mechanisms:
/// 1. RAII (Resource Acquisition Is Initialization)
/// - Connection owns AsyncStream/SyncStream
/// - AsyncStream owns TcpStream
/// - All dropped at scope exit
///
/// 2. Drop Trait Impl
/// - Connection::drop() notifies guards
/// - Tokio/std cleanup releases OS resources
///
/// 3. Explicit Cleanup
/// - Semaphore permit explicitly dropped
/// - Error logging doesn't prevent cleanup
///
/// 4. Panic Safety
/// - Rust's panic unwind calls all Drop impls
/// - No cleanup code skipped
///