vxcontrol/pentagi · error

failed to close tar writer: %w

Error message

failed to close tar writer: %w

What it means

tar.Writer.Close flushes the two trailing zero blocks of the tar archive. This error wraps that Close failing after the header and content were written. Like the previous cases, the sink is a bytes.Buffer, so failure almost always means an I/O/memory error while finalizing the archive; it is exceptionally rare.

Source

Thrown at backend/pkg/tools/terminal.go:484

	filename := filepath.Base(path)
	fileDescriptor := &tar.Header{
		Name: filename,
		Mode: 0600,
		Size: int64(len(content)),
	}
	err = archiveWriter.WriteHeader(fileDescriptor)
	if err != nil {
		return fmt.Errorf("tar archive header generation failed: %w", err)
	}

	_, err = archiveWriter.Write([]byte(content))
	if err != nil {
		return fmt.Errorf("tar archive content serialization failed: %w", err)
	}

	err = archiveWriter.Close()
	if err != nil {
		return fmt.Errorf("failed to close tar writer: %w", err)
	}

	dir := filepath.Dir(path)
	err = t.dockerClient.CopyToContainer(ctx, containerName, dir, tarBuffer, client.CopyToContainerOptions{
		AllowOverwriteDirWithFile: true,
	})
	if err != nil {
		return fmt.Errorf("container file transfer failed: %w", err)
	}

	return nil
}

// EditFile applies a unified diff to the file at path: it reads the current
// content, applies the diff to it entirely in memory (see applyUnifiedDiff),
// and only if every hunk applied cleanly writes the result back - a diff
// that doesn't fully apply leaves the file untouched.
func (t *terminal) EditFile(ctx context.Context, flowID int64, path, diffText string) (string, error) {

View on GitHub (pinned to ea665308ba)

Solutions

  1. Free memory / restart the backend process if the host is OOM-ing
  2. Retry the write operation once memory pressure subsides
  3. Reduce write payload size (chunk content across multiple files)
  4. Inspect the wrapped cause; a deterministic Close failure on a bytes.Buffer points to a corrupted build or vendored stdlib — rebuild the backend
Defensive patterns

Strategy: retry

Try / catch

_, err := term.WriteFile(ctx, flowID, content, path)
if err != nil && strings.Contains(err.Error(), "failed to close tar writer") {
    time.Sleep(time.Second) // transient memory pressure
    _, err = term.WriteFile(ctx, flowID, content, path)
}

Prevention

When it happens

Trigger: WriteFile or EditFile while the process is under severe memory pressure so that even the archive trailer write/allocation fails.

Common situations: OOM-adjacent conditions on small hosts; typically seen alongside the sibling 'content serialization failed' error when writing large payloads.

Related errors


AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01). Data as JSON: /api/errors/f955b99cefa99511. Report an issue: GitHub.