{"record":{"id":"58206171fe8de45c","repo":"gastownhall/beads","slug":"flush-server-not-reachable-w","errorCode":null,"errorMessage":"flush: server not reachable: %w","messagePattern":"flush: server not reachable: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/doltserver/doltserver.go","lineNumber":1582,"sourceCode":"func FlushWorkingSet(host string, port int) error {\n\tctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n\tdefer cancel()\n\n\tdsn := doltutil.ServerDSN{\n\t\tHost: host,\n\t\tPort: port,\n\t\tUser: \"root\",\n\t}.String()\n\tdb, err := sql.Open(\"mysql\", dsn)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"flush: failed to open connection: %w\", err)\n\t}\n\tdefer db.Close()\n\tdb.SetMaxOpenConns(1)\n\tdb.SetConnMaxLifetime(10 * time.Second)\n\n\tif err := db.PingContext(ctx); err != nil {\n\t\treturn fmt.Errorf(\"flush: server not reachable: %w\", err)\n\t}\n\n\t// List all databases, skipping system databases\n\trows, err := db.QueryContext(ctx, \"SHOW DATABASES\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"flush: failed to list databases: %w\", err)\n\t}\n\tvar databases []string\n\tfor rows.Next() {\n\t\tvar name string\n\t\tif err := rows.Scan(&name); err != nil {\n\t\t\tcontinue\n\t\t}\n\t\t// Skip Dolt system databases\n\t\tif name == \"information_schema\" || name == \"mysql\" || name == \"performance_schema\" {\n\t\t\tcontinue\n\t\t}\n\t\tdatabases = append(databases, name)","sourceCodeStart":1564,"sourceCodeEnd":1600,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/doltserver/doltserver.go#L1564-L1600","documentation":"FlushWorkingSet wraps db.PingContext failure with this message: the DSN was valid but no server answered at host:port within the 10-second context timeout, so uncommitted working-set changes cannot be flushed before the server stops. FlushWorkingSet is best-effort — StopWithForce logs a warning and continues the shutdown, risking loss of uncommitted changes.","triggerScenarios":"db.PingContext(ctx) fails during a bd dolt stop/restart: the server died before the flush, wrong port read from the port file of a stale state, connection refused because the server already exited, or the 10s timeout elapsed on a loaded server.","commonSituations":"Server crashed mid-session so stop-time flush can't connect (orphaned uncommitted changes); stale .beads/dolt-server.port pointing at a dead or reused port; remote shared server network blip during shutdown; slow server under heavy write load exceeding the 10s ping window.","solutions":["Check whether the server is actually running (bd dolt status / ps aux | grep dolt); if it died, restart it — bd dolt start — and commit pending work","Delete stale state files (.beads/dolt-server.pid, .beads/dolt-server.port) and retry bd dolt stop so the correct port is used","If the wrapped error is a timeout, retry — a loaded server may answer within the window on a second attempt","Check network reachability to a remote shared server (ping/nc) if host is remote","If uncommitted data was lost, inspect dolt logs / data dir; consider bd doctor --fix only if corruption is indicated"],"exampleFix":"// before\n// Warning: could not flush working set before stop: flush: server not reachable: dial tcp 127.0.0.1:41017: connect: connection refused\n// after: clear stale state and flush via a live server\n// $ bd dolt start      # or verify the port file matches the live server\n// $ bd dolt stop       # flush now succeeds before shutdown","handlingStrategy":"retry","validationCode":"// Confirm the server is alive and the port file matches before stopping\nstate, err := doltserver.IsRunning(beadsDir)\nif err != nil { return err }\nif !state.Running {\n    return errors.New(\"server already down; nothing to flush\")\n}\nconn, err := net.DialTimeout(\"tcp\", fmt.Sprintf(\"%s:%d\", host, state.Port), 2*time.Second)\nif err != nil {\n    return fmt.Errorf(\"server dead on port %d; remove stale pid/port files and restart\", state.Port)\n}\nconn.Close()","typeGuard":null,"tryCatchPattern":"err := doltserver.FlushWorkingSet(host, port)\nif err != nil && strings.Contains(err.Error(), \"server not reachable\") {\n    // one retry after a short pause — server may have been mid-restart\n    time.Sleep(time.Second)\n    if err := doltserver.FlushWorkingSet(host, port); err != nil {\n        log.Printf(\"warning: flush failed after retry: %v — uncommitted changes at risk\", err)\n    }\n}","preventionTips":["Use bd dolt stop / bd dolt restart rather than killing the server directly","Clean stale .beads/dolt-server.pid / .port files after crashes before restarting","Avoid concurrent lifecycle commands from multiple terminals/CI jobs","Verify network stability to remote shared servers","Commit critical working-set changes explicitly instead of relying on the flush"],"tags":["go","mysql","network","flush","shutdown"],"backgroundTag":"server-not-reachable","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}