wavetermdev/waveterm · error

error truncating blockfile: %w

Error message

error truncating blockfile: %w

What it means

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.

Source

Thrown at pkg/blockcontroller/blockcontroller.go:399

		Data: &wps.WSFileEventData{
			ZoneId:   blockId,
			FileName: blockFile,
			FileOp:   wps.FileOp_Append,
			Data64:   base64.StdEncoding.EncodeToString(data),
		},
	})
	return nil
}

func HandleTruncateBlockFile(blockId string) error {
	ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
	defer cancelFn()
	err := filestore.WFS.WriteFile(ctx, blockId, wavebase.BlockFile_Term, nil)
	if err == fs.ErrNotExist {
		return nil
	}
	if err != nil {
		return fmt.Errorf("error truncating blockfile: %w", err)
	}
	err = filestore.WFS.DeleteFile(ctx, blockId, wavebase.BlockFile_Cache)
	if err == fs.ErrNotExist {
		err = nil
	}
	if err != nil {
		log.Printf("error deleting cache file (continuing): %v\n", err)
	}
	wps.Broker.Publish(wps.WaveEvent{
		Event:  wps.Event_BlockFile,
		Scopes: []string{waveobj.MakeORef(waveobj.OType_Block, blockId).String()},
		Data: &wps.WSFileEventData{
			ZoneId:   blockId,
			FileName: wavebase.BlockFile_Term,
			FileOp:   wps.FileOp_Truncate,
		},
	})
	return nil

View on GitHub (pinned to a4447c1563)

Solutions

  1. Stop/pause the block's controller before truncating so no concurrent appends hold the file
  2. Check disk space and write permissions for the wave filestore location
  3. Delete the block and recreate it if the terminal file is persistently corrupt
  4. Inspect the wrapped %w error to determine the exact filesystem cause

Example fix

// before
// truncate while controller still running
HandleTruncateBlockFile(blockId)
// after
StopController(blockId)
err := HandleTruncateBlockFile(blockId)
if err != nil { log.Printf("truncate failed: %v", err) }
Defensive patterns

Strategy: try-catch

Validate before calling

// stop writers first
if c := getController(blockId); c != nil && c.GetRuntimeStatus().ShellProcStatus == "running" { /* stop before truncate */ }

Try / catch

if err := HandleTruncateBlockFile(blockId); err != nil && !errors.Is(err, fs.ErrNotExist) {
    log.Printf("truncate failed: %v", err)
}

Prevention

When it happens

Trigger: 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.

Common situations: Clearing a terminal while a running process is actively appending output; read-only filestore mount; disk quota exceeded; filestore cache corruption after a crash.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/ffc8e8f74657299d. Report an issue: GitHub.