{"record":{"id":"4a969790ca82e8a9","repo":"wavetermdev/waveterm","slug":"error-inserting-mainserver-w","errorCode":null,"errorMessage":"error inserting mainserver: %w","messagePattern":"error inserting mainserver: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/wcore/wcore.go","lineNumber":175,"sourceCode":"\t\tif err != nil {\n\t\t\tlog.Printf(\"[error] sending no-telemetry update: %v\\n\", err)\n\t\t\treturn\n\t\t}\n\t}()\n}\n\nfunc InitMainServer() error {\n\tctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)\n\tdefer cancelFn()\n\n\tmainServer, err := wstore.DBGetSingleton[*waveobj.MainServer](ctx)\n\tif err == wstore.ErrNotFound {\n\t\tmainServer = &waveobj.MainServer{\n\t\t\tOID: uuid.NewString(),\n\t\t}\n\t\terr = wstore.DBInsert(ctx, mainServer)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error inserting mainserver: %w\", err)\n\t\t}\n\t} else if err != nil {\n\t\treturn fmt.Errorf(\"error getting mainserver: %w\", err)\n\t}\n\n\tneedsUpdate := false\n\tif mainServer.JwtPrivateKey == \"\" || mainServer.JwtPublicKey == \"\" {\n\t\tkeyPair, err := wavejwt.GenerateKeyPair()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error generating jwt keypair: %w\", err)\n\t\t}\n\t\tmainServer.JwtPrivateKey = base64.StdEncoding.EncodeToString(keyPair.PrivateKey)\n\t\tmainServer.JwtPublicKey = base64.StdEncoding.EncodeToString(keyPair.PublicKey)\n\t\tneedsUpdate = true\n\t}\n\n\tif needsUpdate {\n\t\terr = wstore.DBUpdate(ctx, mainServer)","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/wcore/wcore.go#L157-L193","documentation":"InitMainServer loads the singleton MainServer record; if it is not found it creates a new one with a fresh UUID and inserts it. This error wraps a failure of that DBInsert, i.e. a brand-new MainServer record could not be persisted.","triggerScenarios":"wstore.DBGetSingleton returns wstore.ErrNotFound, the new MainServer object is built, and wstore.DBInsert then fails — DB locked, unwritable, or schema problems on first launch.","commonSituations":"First launch creating the mainserver record on a failing DB; two instances racing to create the singleton simultaneously; corrupted data directory.","solutions":["Check the wrapped wstore error for the root DB failure","Ensure the Wave data directory is writable and not locked by another process","Remove a corrupted DB to allow clean re-initialization","Retry launch — the second run should find an existing mainserver row"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// mainserver singleton presence check\n_, err := wstore.DBGetSingleton[*waveobj.MainServer](ctx)\nif err != nil && !errors.Is(err, wstore.ErrNotFound) { log.Printf(\"DB unhealthy: %v\", err) }","typeGuard":null,"tryCatchPattern":"err := wcore.InitMainServer(ctx)\nif err != nil && strings.Contains(err.Error(), \"inserting mainserver\") {\n    log.Printf(\"mainserver init failed: %v\", err)\n    return err\n}","preventionTips":["Ensure the data directory is writable before first launch","Avoid racing multiple instances to create the singleton","Verify DB integrity after crashes","Keep disk space available on the data volume"],"tags":["database","mainserver","persistence"],"backgroundTag":"database-insert-failed","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}