{"record":{"id":"3f8eca3b3e0f80c9","repo":"slimtoolkit/slim","slug":"waiting-for-v-event-w","errorCode":null,"errorMessage":"waiting for %v event: %w","messagePattern":"waiting for (.+?) event: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/app/sensor/standalone/control/wait.go","lineNumber":20,"sourceCode":"\nimport (\n\t\"bufio\"\n\t\"context\"\n\t\"fmt\"\n\t\"os\"\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/slimtoolkit/slim/pkg/ipc/event\"\n)\n\nfunc ExecuteWaitEvenCommand(\n\tctx context.Context,\n\teventsFile string,\n\tevt event.Type,\n) error {\n\tif err := waitForEvent(ctx, eventsFile, evt); err != nil {\n\t\treturn fmt.Errorf(\"waiting for %v event: %w\", evt, err)\n\t}\n\n\treturn nil\n}\n\nfunc waitForEvent(ctx context.Context, eventsFile string, target event.Type) error {\n\tfor ctx.Err() == nil {\n\t\tfound, err := findEvent(eventsFile, target)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif found {\n\t\t\treturn nil\n\t\t}\n\n\t\ttime.Sleep(1 * time.Second)\n\t}","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/app/sensor/standalone/control/wait.go#L2-L38","documentation":"ExecuteWaitEvenCommand wraps any failure from waitForEvent (which polls an events file for a target event.Type) with the message \"waiting for %v event: %w\". The library throws it when the wait cannot complete: the events file never contains the expected event, the context is cancelled/deadline exceeded, or the events file cannot be read/parsed. It is a wrapper, so the root cause is always in the wrapped error (%w).","triggerScenarios":"Calling ExecuteWaitEvenCommand(ctx, eventsFile, evt) when the context is cancelled before the event appears, the eventsFile does not exist or is not being written to, or waitForEvent's polling loop hits its internal timeout/error while scanning for evt.","commonSituations":"Integration tests waiting for a sensor event that never fires because the sensor crashed or never started; CI jobs with too-short context deadlines; wrong path passed for eventsFile so the poller reads a stale or missing file; expecting an event type the component never emits (e.g. typo'd event.Type).","solutions":["Inspect the wrapped error: if it is context.DeadlineExceeded, increase the context timeout or fix why the event never appears.","Verify the eventsFile path is correct and the producing component is actually running and writing to it (ls/tail the file while waiting).","Confirm the event.Type passed matches what the sensor actually publishes (check event definitions).","Ensure the sensor under test started successfully before waiting; check its logs for startup failures."],"exampleFix":"// before\nctx := context.Background()\nerr := ExecuteWaitEvenCommand(ctx, eventsFile, event.StartMonitor)\n// after\nctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)\ndefer cancel()\nif _, statErr := os.Stat(eventsFile); statErr != nil {\n    log.Fatalf(\"events file missing: %v\", statErr)\n}\nerr := ExecuteWaitEvenCommand(ctx, eventsFile, event.StartMonitor)","handlingStrategy":"try-catch","validationCode":"if _, err := os.Stat(eventsFile); err != nil {\n    return fmt.Errorf(\"events file not available: %w\", err)\n}\nif ctx.Err() != nil {\n    return fmt.Errorf(\"context already done: %w\", ctx.Err())\n}","typeGuard":null,"tryCatchPattern":"err := ExecuteWaitEvenCommand(ctx, eventsFile, event.StartMonitor)\nif err != nil {\n    if errors.Is(err, context.DeadlineExceeded) {\n        log.Printf(\"event never arrived within deadline; check sensor health\")\n    }\n    return fmt.Errorf(\"wait for event aborted: %w\", err)\n}","preventionTips":["Always pass a context with an explicit timeout and handle DeadlineExceeded distinctly.","Confirm the sensor writes to eventsFile before waiting (stat/tail the file).","Use the exact event.Type constants from the event package, not ad-hoc strings.","Check sensor startup logs before polling so you fail fast on a dead sensor."],"tags":["go","event-wait","timeout","context"],"backgroundTag":"event-wait-timeout","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}