[duplicate-code] Duplicate Code Pattern: Launcher Connection Store-and-Record Block

Đang mở Phù hợp với người mới
#16,691 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
68/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu tại internal/launcher/launcher.go, ở ba vị trí được liệt kê trong issue: các luồng HTTP và stdio trong getOrLaunchWithConfig, cùng với GetOrLaunchForSession. So sánh các kiểm tra trạng thái đóng, nơi lưu kết nối, cách xử lý khóa và các lời gọi recordStart; issue mô tả những khác biệt cần được giữ lại. Hoàn tất khi phần xử lý lặp lại được hợp nhất và các bài kiểm thử launcher bao phủ luồng đóng và khởi chạy đều đạt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

automated-analysis code-quality

Part of duplicate code analysis: #16690

Summary

The launcher repeats the same "lock, re-check closed, store connection, unlock, record start" sequence at three call sites. Each copy must keep the same lock ordering and closed-launcher handling.

Duplication Details

Pattern: Closed-check, store, and recordStart after a launch
  • Severity: Medium

  • Occurrences: 3

  • Locations:

    • internal/launcher/launcher.go (lines 204-212): HTTP backend path in getOrLaunchWithConfig. Stores l.connections[serverID].
    • internal/launcher/launcher.go (lines 222-230): stdio backend path in getOrLaunchWithConfig. Stores l.connections[serverID].
    • internal/launcher/launcher.go (lines 321-330): GetOrLaunchForSession. Stores via l.sessionPool.Set(serverID, sessionID, conn).
  • Code Sample (from lines 222-230):

    l.mu.Lock()
    if l.closed {
      l.mu.Unlock()
      conn.Close()
      return nil, ErrLauncherClosed
    }
    l.connections[serverID] = conn
    l.mu.Unlock()
    l.recordStart(serverID)
    return conn, nil
    

The three copies differ only in the store statement, and the session variant also logs after the store.

Impact Analysis

  • Maintainability: Any change to closed-launcher semantics, such as cleanup or logging, must be made in three places.
  • Bug Risk: The copies could drift. A missed conn.Close() on the closed path would leak a backend process.
  • Code Bloat: About 27 duplicated lines.

Refactoring Recommendations

  1. Extract a locked store helper

    • Add a method such as (l *Launcher) storeConnectionIfOpen(serverID string, conn *mcp.Connection, store func()) error in internal/launcher/launcher.go. It takes the lock, checks l.closed, closes conn on the closed path, runs store() under the lock, and calls recordStart after unlocking.
    • Pass store as a closure: l.connections[serverID] = conn for the two server paths, and l.sessionPool.Set(serverID, sessionID, conn) for the session path.
    • Estimated effort: low (under 1 hour). Existing launcher tests cover the closed and launch paths.
    • Benefits: One place defines the shutdown race handling.
  2. Keep the lock ordering explicit

    • The helper must not call recordStart while holding l.mu, because recordStart takes its own state lock. Preserve the current post-unlock call.

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Parent Issue

See parent analysis report: #16690

Related to #16690

Generated by Duplicate Code Detector · copilot · routed: haiku55 xhigh · ~24 AIC · ⊞ 6.7K · ◷

  • expires on Nov 24, 2026, 2:34 AM UTC
Ngôn ngữ chính
Go
Star
188
Fork
60
Merge trung bình
6 giờ 50 phút
Pull request đã merge (30 ngày)
177

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của github/gh-aw-mcpg

Tất cả issue của github/gh-aw-mcpg

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.