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

Aperta Adatta ai principianti
#16,691 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
68/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go
Ambito
backend

Direzione di ricerca

Inizia in internal/launcher/launcher.go, nei tre punti indicati nell’issue: i percorsi HTTP e stdio di getOrLaunchWithConfig e GetOrLaunchForSession. Confronta i relativi controlli di chiusura, archivi delle connessioni, gestione dei lock e chiamate a recordStart; l’issue descrive le differenze da preservare. Il lavoro è terminato quando la gestione ripetuta è stata consolidata e i test del launcher che coprono i percorsi di chiusura e avvio passano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Go
Stelle
188
Fork
60
Merge medio
6h 50m
PR unite (30g)
177

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di github/gh-aw-mcpg

Tutte le issue di github/gh-aw-mcpg

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.