thanos-io/thanos · error

open chunk writer

Error message

open chunk writer

What it means

NewDiskWriter fails when chunks.NewWriter cannot open a chunk segment writer in the temporary block dir's chunks/ directory. This prevents any block data from being written and aborts block creation immediately.

Solutions

  1. Check write permissions on the parent directory used for the block temp dir.
  2. Verify free disk space and that the filesystem is not read-only.
  3. Ensure the target path is not occupied by a regular file named 'chunks'.
  4. Clean stale temporary block dirs (<bTmp> leftovers) and retry.

Example fix

// before
d, err := NewDiskWriter(ctx, "/data/store/01BK...", logging.NewNopLogger()) // /data/store not writable
// after
os.Chmod("/data/store", 0755)
d, err := NewDiskWriter(ctx, "/data/store/01BK...", logging.NewNopLogger())
Defensive patterns

Strategy: try-catch

Validate before calling

chunksParent := filepath.Join(bdir, "tmp-for-creation")
if err := os.MkdirAll(chunksParent, 0750); err != nil {
    return fmt.Errorf("cannot create tmp dir %s: %w", chunksParent, err)
}
if err := unix.Access(chunksParent, unix.W_OK); err != nil {
    return fmt.Errorf("tmp dir not writable: %w", err)
}

Type guard

func canWriteDir(path string) bool {
    fi, err := os.Stat(path)
    return err == nil && fi.IsDir() && unix.Access(path, unix.W_OK) == nil
}

Try / catch

d, err := block.NewDiskWriter(ctx, bdir, logger)
if err != nil {
    if strings.Contains(err.Error(), "open chunk writer") {
        return fmt.Errorf("filesystem issue creating chunks writer for %s: %w", bdir, err)
    }
    return err
}

Prevention

When it happens

Trigger: chunks.NewWriter failing on <bTmp>/chunks — typically because the directory cannot be created/opened: permission denied, disk full, path is a file, or the underlying OS open failed.

Common situations: Insufficient permissions on the parent data directory; disk quota/full volume; bTmp path colliding with an existing non-directory file; read-only filesystem mount.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


AI-assisted analysis of thanos-io/thanos@35b8b99117 (2026-09-07). Data as JSON: /api/errors/1388a06cdc75664a. Report an issue: GitHub.

Appendix: source

Thrown at pkg/block/writer.go:88

	defer func() {
		if err != nil {
			err = tsdb_errors.NewMulti(err, tsdb_errors.CloseAll(d.closers)).Err()
			if err := os.RemoveAll(bTmp); err != nil {
				level.Error(logger).Log("msg", "removed tmp folder after failed compaction", "err", err.Error())
			}
		}
	}()

	if err = os.RemoveAll(bTmp); err != nil {
		return nil, err
	}
	if err = os.MkdirAll(bTmp, 0750); err != nil {
		return nil, err
	}

	chunkw, err := chunks.NewWriter(filepath.Join(bTmp, ChunksDirname))
	if err != nil {
		return nil, errors.Wrap(err, "open chunk writer")
	}
	d.closers = append(d.closers, chunkw)

	// TODO(bwplotka): Setup instrumentedChunkWriter if we want to upstream this code.

	indexw, err := index.NewWriter(ctx, filepath.Join(bTmp, IndexFilename))
	if err != nil {
		return nil, errors.Wrap(err, "open index writer")
	}
	d.closers = append(d.closers, indexw)
	d.statsGatheringSeriesWriter = statsGatheringSeriesWriter{iw: indexw, cw: chunkw}
	return d, nil
}

func (d *DiskWriter) Flush() (_ tsdb.BlockStats, err error) {
	defer func() {
		if err != nil {
			err = tsdb_errors.NewMulti(err, tsdb_errors.CloseAll(d.closers)).Err()

View on GitHub (pinned to 35b8b99117)