Commit c88a62d
authored
fix(server/mcp): scope defer span.End inside loop iteration (googleapis#2558)
## Description
In `readInputStream()`, `defer span.End()` is called inside a `for`
loop. Since `defer` schedules execution when the enclosing **function**
returns (not the current loop iteration), all spans created per message
accumulate without ever being ended for the lifetime of the stdio
session.
For a long-lived stdio server, this means:
- **Memory leak**: span objects accumulate indefinitely
- **Incorrect traces**: spans are never properly closed, producing
misleading telemetry data
## Changes
Wrap the per-message processing logic in an immediately invoked function
expression (IIFE), so `defer span.End()` correctly fires at the end of
each iteration. This follows the same pattern already used in
`InitializeConfigs` (`server.go` lines 85-98) for scoping spans inside
loops.
## Test
All existing tests pass. The `TestStdioSession` test exercises
`readLine` and `write` on the stdio session. The `readInputStream` loop
is covered by the existing MCP endpoint tests that exercise the full
message processing pipeline.
Fixes googleapis#25491 parent 81253a0 commit c88a62d
1 file changed
+27
-21
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
198 | | - | |
199 | | - | |
200 | 198 | | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
206 | 202 | | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
214 | 208 | | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
222 | 215 | | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
223 | 229 | | |
224 | 230 | | |
225 | 231 | | |
| |||
0 commit comments