Commit 6182318
* fix(db): restore transaction control to get_db() for middleware sessions
PR #3600 introduced a transaction management violation where
ObservabilityMiddleware commits the shared database session instead
of get_db(), breaking the established contract where get_db() controls
transaction boundaries. This creates data integrity risks where failed
validations can be committed to the database.
This fix restores the correct behavior:
- Middleware manages session lifecycle (create/close)
- get_db() manages transactions (commit/rollback)
Changes:
- Remove commit logic from ObservabilityMiddleware (observability_middleware.py:210-216)
- Add commit/rollback handling to get_db() for middleware sessions (main.py:3137-3164)
- Update get_db() docstring to document transaction control responsibility
- Update 2 existing tests to reflect new behavior
- Add 7 comprehensive tests for transaction semantics
Security implications:
- Fixes data integrity bug where invalid data could be committed
- Maintains proper transaction isolation per request
- Preserves connection invalidation on broken connections
- No impact on auth/RBAC (middleware runs before route handlers)
Trade-offs:
- Observability data (traces/spans) is rolled back on errors (acceptable - best-effort tracing)
Closes #3731
Signed-off-by: Mohan Lakshmaiah <mohan.economist@gmail.com>
* test(db): add coverage for double-failure edge case in get_db()
Signed-off-by: Mohan Lakshmaiah <mohan.economist@gmail.com>
* fix(tests): clean up lint violations in transaction control tests
Remove unused AsyncMock import and unused variable assignments
flagged by ruff (F401, F841). Apply isort/black formatting.
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
---------
Signed-off-by: Mohan Lakshmaiah <mohan.economist@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Co-authored-by: Mihai Criveti <crivetimihai@gmail.com>
1 parent 10224d8 commit 6182318
4 files changed
Lines changed: 403 additions & 17 deletions
File tree
- mcpgateway
- middleware
- tests/unit/mcpgateway/middleware
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3092 | 3092 | | |
3093 | 3093 | | |
3094 | 3094 | | |
3095 | | - | |
3096 | | - | |
| 3095 | + | |
| 3096 | + | |
| 3097 | + | |
| 3098 | + | |
| 3099 | + | |
| 3100 | + | |
| 3101 | + | |
| 3102 | + | |
| 3103 | + | |
| 3104 | + | |
| 3105 | + | |
3097 | 3106 | | |
3098 | 3107 | | |
3099 | 3108 | | |
| |||
3114 | 3123 | | |
3115 | 3124 | | |
3116 | 3125 | | |
3117 | | - | |
| 3126 | + | |
| 3127 | + | |
| 3128 | + | |
| 3129 | + | |
3118 | 3130 | | |
3119 | 3131 | | |
3120 | 3132 | | |
| |||
3138 | 3150 | | |
3139 | 3151 | | |
3140 | 3152 | | |
3141 | | - | |
3142 | | - | |
3143 | | - | |
| 3153 | + | |
| 3154 | + | |
| 3155 | + | |
| 3156 | + | |
| 3157 | + | |
| 3158 | + | |
| 3159 | + | |
| 3160 | + | |
| 3161 | + | |
| 3162 | + | |
| 3163 | + | |
| 3164 | + | |
| 3165 | + | |
| 3166 | + | |
| 3167 | + | |
| 3168 | + | |
| 3169 | + | |
| 3170 | + | |
| 3171 | + | |
| 3172 | + | |
| 3173 | + | |
| 3174 | + | |
| 3175 | + | |
| 3176 | + | |
| 3177 | + | |
| 3178 | + | |
3144 | 3179 | | |
3145 | 3180 | | |
3146 | 3181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
217 | 214 | | |
218 | 215 | | |
219 | 216 | | |
| |||
Lines changed: 9 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
291 | | - | |
292 | | - | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
293 | 294 | | |
294 | 295 | | |
295 | 296 | | |
| |||
341 | 342 | | |
342 | 343 | | |
343 | 344 | | |
| 345 | + | |
344 | 346 | | |
345 | 347 | | |
346 | 348 | | |
| |||
350 | 352 | | |
351 | 353 | | |
352 | 354 | | |
353 | | - | |
| 355 | + | |
354 | 356 | | |
355 | 357 | | |
356 | 358 | | |
357 | 359 | | |
358 | 360 | | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
359 | 365 | | |
360 | | - | |
361 | 366 | | |
362 | 367 | | |
363 | 368 | | |
| |||
0 commit comments