(options: CleanupOptions)
| 446 | * Handles in-process servers, stderr listener removal, signal escalation, and client close. |
| 447 | */ |
| 448 | export function createCleanup(options: CleanupOptions): () => Promise<void> { |
| 449 | const { |
| 450 | client, |
| 451 | transport, |
| 452 | transportType, |
| 453 | childPid, |
| 454 | inProcessServer, |
| 455 | stderrCleanup, |
| 456 | logger, |
| 457 | serverName, |
| 458 | } = options |
| 459 | |
| 460 | return async () => { |
| 461 | // In-process servers |
| 462 | if (inProcessServer) { |
| 463 | try { |
| 464 | await inProcessServer.close() |
| 465 | } catch (error) { |
| 466 | logger.debug( |
| 467 | `[${serverName}] Error closing in-process server: ${error}`, |
| 468 | ) |
| 469 | } |
| 470 | try { |
| 471 | await client.close() |
| 472 | } catch (error) { |
| 473 | logger.debug(`[${serverName}] Error closing client: ${error}`) |
| 474 | } |
| 475 | return |
| 476 | } |
| 477 | |
| 478 | // Remove stderr listener |
| 479 | stderrCleanup?.removeHandler() |
| 480 | |
| 481 | // Signal escalation for stdio |
| 482 | if (transportType === 'stdio' && childPid) { |
| 483 | await terminateWithSignalEscalation(childPid, logger, serverName) |
| 484 | } |
| 485 | |
| 486 | // Close the client connection (which also closes the transport) |
| 487 | try { |
| 488 | await client.close() |
| 489 | } catch (error) { |
| 490 | logger.debug(`[${serverName}] Error closing client: ${error}`) |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // ============================================================================ |
| 496 | // Connected server result builder |
nothing calls this directly
no test coverage detected