{"record":{"id":"ffc8e8f74657299d","repo":"wavetermdev/waveterm","slug":"error-truncating-blockfile-w","errorCode":null,"errorMessage":"error truncating blockfile: %w","messagePattern":"error truncating blockfile: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/blockcontroller/blockcontroller.go","lineNumber":399,"sourceCode":"\t\tData: &wps.WSFileEventData{\n\t\t\tZoneId:   blockId,\n\t\t\tFileName: blockFile,\n\t\t\tFileOp:   wps.FileOp_Append,\n\t\t\tData64:   base64.StdEncoding.EncodeToString(data),\n\t\t},\n\t})\n\treturn nil\n}\n\nfunc HandleTruncateBlockFile(blockId string) error {\n\tctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)\n\tdefer cancelFn()\n\terr := filestore.WFS.WriteFile(ctx, blockId, wavebase.BlockFile_Term, nil)\n\tif err == fs.ErrNotExist {\n\t\treturn nil\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error truncating blockfile: %w\", err)\n\t}\n\terr = filestore.WFS.DeleteFile(ctx, blockId, wavebase.BlockFile_Cache)\n\tif err == fs.ErrNotExist {\n\t\terr = nil\n\t}\n\tif err != nil {\n\t\tlog.Printf(\"error deleting cache file (continuing): %v\\n\", err)\n\t}\n\twps.Broker.Publish(wps.WaveEvent{\n\t\tEvent:  wps.Event_BlockFile,\n\t\tScopes: []string{waveobj.MakeORef(waveobj.OType_Block, blockId).String()},\n\t\tData: &wps.WSFileEventData{\n\t\t\tZoneId:   blockId,\n\t\t\tFileName: wavebase.BlockFile_Term,\n\t\t\tFileOp:   wps.FileOp_Truncate,\n\t\t},\n\t})\n\treturn nil","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/blockcontroller/blockcontroller.go#L381-L417","documentation":"HandleTruncateBlockFile resets a block's terminal file by writing nil via filestore.WFS.WriteFile, then deletes the cache file. Any WriteFile error other than fs.ErrNotExist is wrapped as 'error truncating blockfile: %w'. Truncation failed at the filestore layer, so stale terminal output may remain.","triggerScenarios":"WFS.WriteFile for BlockFile_Term failing: file locked by an active writer (running shell controller), permissions issue, disk full, or corrupted filestore entry — anything not fs.ErrNotExist.","commonSituations":"Clearing a terminal while a running process is actively appending output; read-only filestore mount; disk quota exceeded; filestore cache corruption after a crash.","solutions":["Stop/pause the block's controller before truncating so no concurrent appends hold the file","Check disk space and write permissions for the wave filestore location","Delete the block and recreate it if the terminal file is persistently corrupt","Inspect the wrapped %w error to determine the exact filesystem cause"],"exampleFix":"// before\n// truncate while controller still running\nHandleTruncateBlockFile(blockId)\n// after\nStopController(blockId)\nerr := HandleTruncateBlockFile(blockId)\nif err != nil { log.Printf(\"truncate failed: %v\", err) }","handlingStrategy":"try-catch","validationCode":"// stop writers first\nif c := getController(blockId); c != nil && c.GetRuntimeStatus().ShellProcStatus == \"running\" { /* stop before truncate */ }","typeGuard":null,"tryCatchPattern":"if err := HandleTruncateBlockFile(blockId); err != nil && !errors.Is(err, fs.ErrNotExist) {\n    log.Printf(\"truncate failed: %v\", err)\n}","preventionTips":["Stop or pause the controller before truncating its terminal file","Ensure the filestore mount is writable","Check disk quota before bulk truncations"],"tags":["filestore","io","truncate"],"backgroundTag":"file-write-failed","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}