{"record":{"id":"bf5b7710e58c6702","repo":"geektutu/7days-golang","slug":"rpc-service-already-defined-bf5b77","errorCode":null,"errorMessage":"rpc: service already defined: ","messagePattern":"rpc: service already defined: ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-rpc/day6-load-balance/server.go","lineNumber":223,"sourceCode":"\t\t}\n\t\tgo server.ServeConn(conn)\n\t}\n}\n\n// Accept accepts connections on the listener and serves requests\n// for each incoming connection.\nfunc Accept(lis net.Listener) { DefaultServer.Accept(lis) }\n\n// Register publishes in the server the set of methods of the\n// receiver value that satisfy the following conditions:\n//\t- exported method of exported type\n//\t- two arguments, both of exported type\n//\t- the second argument is a pointer\n//\t- one return value, of type error\nfunc (server *Server) Register(rcvr interface{}) error {\n\ts := newService(rcvr)\n\tif _, dup := server.serviceMap.LoadOrStore(s.name, s); dup {\n\t\treturn errors.New(\"rpc: service already defined: \" + s.name)\n\t}\n\treturn nil\n}\n\n// Register publishes the receiver's methods in the DefaultServer.\nfunc Register(rcvr interface{}) error { return DefaultServer.Register(rcvr) }\n\nconst (\n\tconnected        = \"200 Connected to Gee RPC\"\n\tdefaultRPCPath   = \"/_geeprc_\"\n\tdefaultDebugPath = \"/debug/geerpc\"\n)\n\n// ServeHTTP implements an http.Handler that answers RPC requests.\nfunc (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {\n\tif req.Method != \"CONNECT\" {\n\t\tw.Header().Set(\"Content-Type\", \"text/plain; charset=utf-8\")\n\t\tw.WriteHeader(http.StatusMethodNotAllowed)","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/geektutu/7days-golang/blob/cf3644382101dc13e7fd92e8f5c66cabc51bcd3b/gee-rpc/day6-load-balance/server.go#L205-L241","documentation":"Register (or RegisterByName via newService + LoadOrStore) detected that a service with the same name is already published in the server's serviceMap. The library forbids double registration to keep the method table unambiguous.","triggerScenarios":"Calling server.Register(new(Foo)) twice for the same type name, or registering two different types whose reflected names collide (same type name in different packages both resolve to \"Foo\").","commonSituations":"Duplicated Register calls in initialization code paths (e.g. both main and an init function register the service), hot-reload code that re-registers handlers, or same-named structs in different packages.","solutions":["Remove the duplicate Register call; register each service once during server setup.","If a different type shares the Go name, alias one of them or register only one receiver per name.","Ignore the error deliberately if re-registration is expected and harmless, e.g. if err != nil && !strings.Contains(err.Error(), \"already defined\") { return err }."],"exampleFix":"// before\n_ = srv.Register(new(Foo))\n_ = srv.Register(new(Foo)) // panics/errors here\n// after\n_ = srv.Register(new(Foo))","handlingStrategy":"try-catch","validationCode":"// register each service exactly once, e.g. via a registration table\nvar registered = map[string]bool{}\nfunc registerOnce(srv *geeRPC.Server, rcvr interface{}) error {\n    name := reflect.TypeOf(rcvr).Elem().Name()\n    if registered[name] { return nil }\n    if err := srv.Register(rcvr); err != nil { return err }\n    registered[name] = true\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := srv.Register(new(Foo)); err != nil && !strings.Contains(err.Error(), \"already defined\") { return err } // tolerate deliberate duplicates","preventionTips":["Centralize all Register calls in one init function.","Avoid registering in both package init() and main().","Give receivers in different packages distinct type names or alias them."],"tags":["rpc","registration","duplicate-service"],"backgroundTag":"duplicate-service-registration","analyzedSha":"cf3644382101dc13e7fd92e8f5c66cabc51bcd3b","analyzedAt":"2026-09-03T18:31:24.087Z","contentChangedAt":"2026-09-03T18:31:24.087Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}