(ctx context.Context, params *lsproto.InitializeAPISessionParams, _ *lsproto.RequestMessage)
| 1730 | } |
| 1731 | |
| 1732 | func (s *Server) handleInitializeAPISession(ctx context.Context, params *lsproto.InitializeAPISessionParams, _ *lsproto.RequestMessage) (lsproto.CustomInitializeAPISessionResponse, error) { |
| 1733 | s.apiSessionsMu.Lock() |
| 1734 | defer s.apiSessionsMu.Unlock() |
| 1735 | |
| 1736 | if s.apiSessions == nil { |
| 1737 | s.apiSessions = make(map[string]*api.Session) |
| 1738 | } |
| 1739 | |
| 1740 | var apiSession *api.Session |
| 1741 | apiSession = api.NewSession(s.session, nil) |
| 1742 | |
| 1743 | // Use provided pipe path or generate a unique one |
| 1744 | var pipePath string |
| 1745 | if params.Pipe != nil && *params.Pipe != "" { |
| 1746 | pipePath = *params.Pipe |
| 1747 | } else { |
| 1748 | pipePath = s.generateAPIPipePath() |
| 1749 | } |
| 1750 | |
| 1751 | transport, err := api.NewPipeTransport(pipePath) |
| 1752 | if err != nil { |
| 1753 | return nil, fmt.Errorf("failed to create API transport: %w", err) |
| 1754 | } |
| 1755 | |
| 1756 | // Start accepting connections in the background |
| 1757 | go func() { |
| 1758 | defer func() { |
| 1759 | apiSession.Close() |
| 1760 | s.removeAPISession(apiSession.ID()) |
| 1761 | }() |
| 1762 | |
| 1763 | rwc, acceptErr := transport.Accept() |
| 1764 | _ = transport.Close() |
| 1765 | if acceptErr != nil { |
| 1766 | s.logger.Errorf("API session %s: failed to accept connection: %v", apiSession.ID(), acceptErr) |
| 1767 | return |
| 1768 | } |
| 1769 | |
| 1770 | // Create a cancellable context for the API connection |
| 1771 | apiCtx, apiCancel := context.WithCancel(s.backgroundCtx) |
| 1772 | defer apiCancel() |
| 1773 | |
| 1774 | // Run the connection with panic recovery |
| 1775 | defer func() { |
| 1776 | if r := recover(); r != nil { |
| 1777 | stack := debug.Stack() |
| 1778 | s.logger.Errorf("API session %s: panic: %v\n%s", apiSession.ID(), r, string(stack)) |
| 1779 | // Cancel the context to shut down the connection |
| 1780 | apiCancel() |
| 1781 | // Close the underlying connection |
| 1782 | rwc.Close() |
| 1783 | } |
| 1784 | }() |
| 1785 | |
| 1786 | conn := api.NewAsyncConn(rwc, apiSession) |
| 1787 | if apiErr := conn.Run(apiCtx); apiErr != nil { |
| 1788 | s.logger.Errorf("API session %s: %v", apiSession.ID(), apiErr) |
| 1789 | } |
nothing calls this directly
no test coverage detected