Repository navigation
Commit 0fdbc4d
committed
fix(upstream): make ListTools coalescing deadlock-safe + tidy snapshot lifecycle
Address review feedback on PR smart-mcp-proxy#446:
1. acquireListToolsContext now ALWAYS allocates listToolsWaitCh and resets
the cached last-result when electing a leader. Previously the helper was
used by the health-check (client.go:750) and tool-count refresh
(client.go:1224) without setting up a wait channel, so a concurrent
ListTools waiter would block on a nil channel for up to 30s
(ctx.Done()) instead of being unblocked when the holder finished.
2. The two non-coalescing callers (health check, tool-count refresh) now
call publishListToolsResult so any ListTools waiter coalesced behind
them receives the real tools list rather than nil/stale data.
3. ListTools is split into a leader path (runListToolsAsLeader) that goes
through acquireListToolsContext and a wait path that loops on a
genuine race (rare: holder released between the failed acquire and the
re-check) before parking on the wait channel. Single source of truth
for leader bookkeeping.
4. Runtime: prune r.lastGoodTools entries for servers no longer in
GetAllServerNames() to prevent unbounded growth on config churn.
5. Demote the "reusing last-good tool snapshot" log from Warn to Info -
it's benign self-healing that can fire repeatedly during reconnect
storms and was noisy.
6. New regression tests in listtools_coalescing_test.go:
- TestListTools_MultipleWaitersUnblockedByLeader: real
leader/multi-waiter goroutine interaction.
- TestListTools_HealthCheckLeaderDoesNotDeadlockWaiters: directly
reproduces the nil-channel hang the old code allowed.
- TestListTools_LeaderErrorPropagatedToWaiters: error fan-out.
- TestListTools_AcquireContextResetsCachedResult: leader election
clears stale per-call state.
Tests pass with -race on internal/upstream/managed and internal/runtime.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>1 parent 1f83fef commit 0fdbc4d
3 files changed
Lines changed: 247 additions & 43 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
305 | | - | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
306 | 316 | | |
307 | 317 | | |
308 | 318 | | |
309 | 319 | | |
310 | 320 | | |
311 | 321 | | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
312 | 327 | | |
313 | 328 | | |
314 | 329 | | |
| |||
325 | 340 | | |
326 | 341 | | |
327 | 342 | | |
328 | | - | |
| 343 | + | |
329 | 344 | | |
330 | 345 | | |
331 | 346 | | |
| |||
342 | 357 | | |
343 | 358 | | |
344 | 359 | | |
345 | | - | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
346 | 363 | | |
347 | 364 | | |
348 | 365 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
421 | 421 | | |
422 | 422 | | |
423 | 423 | | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
424 | 431 | | |
425 | 432 | | |
426 | 433 | | |
| |||
429 | 436 | | |
430 | 437 | | |
431 | 438 | | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
432 | 442 | | |
433 | 443 | | |
434 | 444 | | |
| |||
438 | 448 | | |
439 | 449 | | |
440 | 450 | | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
441 | 455 | | |
442 | 456 | | |
443 | 457 | | |
444 | 458 | | |
445 | 459 | | |
446 | 460 | | |
447 | 461 | | |
448 | | - | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
449 | 476 | | |
450 | 477 | | |
451 | 478 | | |
| |||
459 | 486 | | |
460 | 487 | | |
461 | 488 | | |
462 | | - | |
463 | | - | |
464 | | - | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
465 | 496 | | |
| 497 | + | |
466 | 498 | | |
467 | 499 | | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
468 | 512 | | |
469 | 513 | | |
470 | 514 | | |
| |||
482 | 526 | | |
483 | 527 | | |
484 | 528 | | |
| 529 | + | |
485 | 530 | | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
492 | 535 | | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
500 | 542 | | |
501 | | - | |
502 | 543 | | |
503 | 544 | | |
504 | 545 | | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | | - | |
| 546 | + | |
510 | 547 | | |
511 | 548 | | |
512 | 549 | | |
| |||
522 | 559 | | |
523 | 560 | | |
524 | 561 | | |
525 | | - | |
526 | 562 | | |
527 | | - | |
528 | 563 | | |
529 | 564 | | |
530 | 565 | | |
| |||
788 | 823 | | |
789 | 824 | | |
790 | 825 | | |
791 | | - | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
792 | 830 | | |
793 | 831 | | |
794 | 832 | | |
| |||
1267 | 1305 | | |
1268 | 1306 | | |
1269 | 1307 | | |
1270 | | - | |
| 1308 | + | |
| 1309 | + | |
1271 | 1310 | | |
| 1311 | + | |
1272 | 1312 | | |
1273 | 1313 | | |
1274 | 1314 | | |
| |||
0 commit comments