{"record":{"id":"4ea48b158bc79c71","repo":"charmbracelet/crush","slug":"failed-to-subscribe-to-events-w-4ea48b","errorCode":null,"errorMessage":"failed to subscribe to events: %w","messagePattern":"failed to subscribe to events: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/run.go","lineNumber":255,"sourceCode":"\t\treturn fmt.Errorf(\"failed to resolve session: %w\", err)\n\t}\n\tif continueSessionID != \"\" || useLast {\n\t\tslog.Info(\"Continuing session for non-interactive run\", \"session_id\", sess.ID)\n\t\t// If no explicit model override was requested, restore the\n\t\t// model/provider from the last assistant message in the\n\t\t// session, provided it is still available.\n\t\tif largeModel == \"\" && smallModel == \"\" {\n\t\t\tif err := restoreModelFromSession(ctx, c, ws, sess.ID); err != nil {\n\t\t\t\tslog.Warn(\"Failed to restore model from session\", \"error\", err)\n\t\t\t}\n\t\t}\n\t} else {\n\t\tslog.Info(\"Created session for non-interactive run\", \"session_id\", sess.ID)\n\t}\n\n\tevents, err := c.SubscribeEvents(ctx, ws.ID)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to subscribe to events: %w\", err)\n\t}\n\n\t// Mint a per-call RunID so we can correlate the terminal\n\t// RunComplete with *this* SendMessage even if the session was\n\t// busy and another turn finished first. Without it the stream\n\t// loop would exit on whichever RunComplete arrived first for\n\t// the same session and drop the queued prompt's output.\n\trunID := uuid.New().String()\n\tif err := c.SendMessage(ctx, ws.ID, sess.ID, runID, prompt); err != nil {\n\t\treturn fmt.Errorf(\"failed to send message: %w\", err)\n\t}\n\n\tstream := &runStream{\n\t\tsessionID: sess.ID,\n\t\trunID:     runID,\n\t\tout:       os.Stdout,\n\t\tread:      make(map[string]int),\n\t}","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/run.go#L237-L273","documentation":"Wraps errors from c.SubscribeEvents(ctx, ws.ID), which opens the pubsub event stream used to relay streaming output for the run. Thrown when the coordinator event subscription cannot be established.","triggerScenarios":"`crush run` calls c.SubscribeEvents(ctx, ws.ID) after session resolution and it returns an error — coordinator/broker unavailable or the workspace context is already cancelled.","commonSituations":"Parent context cancelled (Ctrl-C, CI job timeout) before subscription; coordinator shut down due to an earlier init failure; internal pubsub wiring failure in an embedded/custom usage of the run command.","solutions":["Retry the run; transient cancellation usually resolves itself","Ensure the agent/coordinator initialized successfully earlier in the pipeline","Check that the context passed to the command isn't cancelled prematurely (timeouts, signal handling)","Update crush if using it as a library — pubsub wiring changed between versions"],"exampleFix":"// before: short timeout kills subscription\nctx, cancel := context.WithTimeout(ctx, 2*time.Second)\n// after\nctx, cancel := context.WithTimeout(ctx, 5*time.Minute)\nevents, err := c.SubscribeEvents(ctx, ws.ID)","handlingStrategy":"retry","validationCode":"select {\ncase <-ctx.Done():\n\treturn ctx.Err() // bail before subscribing on a dead context\ndefault:\n}","typeGuard":null,"tryCatchPattern":"events, err := c.SubscribeEvents(ctx, ws.ID)\nif err != nil {\n\tif errors.Is(err, context.Canceled) {\n\t\treturn err // don't retry on user cancellation\n\t}\n\tselect {\n\tcase <-time.After(time.Second):\n\t\tevents, err = c.SubscribeEvents(ctx, ws.ID)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to subscribe to events: %w\", err)\n\t\t}\n\tcase <-ctx.Done():\n\t\treturn ctx.Err()\n\t}\n}","preventionTips":["Avoid pre-cancelling or heavily time-boxing the run context","Retry subscription once on transient failures before giving up","Keep coordinator initialization errors fatal (don't continue after partial init)"],"tags":["go","pubsub","events","lifecycle"],"backgroundTag":"event-subscription-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}