docker/compose · error

stashing env for %q: %w

Error message

stashing env for %q: %w

What it means

Before creating the Docker-context OTLP client, compose stashes (unsets) every OTEL_* env var so OTEL's env-reading defaults do not interfere. If os.Unsetenv fails for a key, initialization returns 'stashing env for %q: %w'. The named key is the exact env var that could not be removed.

Source

Thrown at internal/tracing/docker_context.go:61

	}

	if cfg.Endpoint == "" {
		return nil, nil
	}

	// HACK: unfortunately _all_ public OTEL initialization functions
	// 	implicitly read from the OS env, so temporarily unset them all and
	// 	restore afterwards
	defer func() {
		for k, v := range otelEnv {
			if err := os.Setenv(k, v); err != nil {
				panic(fmt.Errorf("restoring env for %q: %w", k, err))
			}
		}
	}()
	for k := range otelEnv {
		if err := os.Unsetenv(k); err != nil {
			return nil, fmt.Errorf("stashing env for %q: %w", k, err)
		}
	}

	conn, err := grpc.NewClient(cfg.Endpoint,
		grpc.WithContextDialer(memnet.DialEndpoint),
		// this dial is restricted to using a local Unix socket / named pipe,
		// so there is no need for TLS
		grpc.WithTransportCredentials(insecure.NewCredentials()),
	)
	if err != nil {
		return nil, fmt.Errorf("initializing otel connection from docker context metadata: %w", err)
	}

	client := otlptracegrpc.NewClient(otlptracegrpc.WithGRPCConn(conn))
	return client, nil
}

// ConfigFromDockerContext inspects extra metadata included as part of the

View on GitHub (pinned to ddc4b044b6)

Solutions

  1. Address the wrapped cause (usually allocation failure): raise the memory limit for the compose process/container
  2. Retry the command — transient allocation failures at startup are the realistic case
  3. If it reproduces deterministically with one key, inspect that env var for anomalies (embedded NULs, gigantic value) and re-export it cleanly: unset OTEL_X && export OTEL_X=...
Defensive patterns

Strategy: validation

Validate before calling

// pre-sanitize OTEL env: drop vars with empty/oversized values before compose starts
for _, kv := range os.Environ() { if k, _, ok := strings.Cut(kv, "="); ok && strings.HasPrefix(k, "OTEL_") && len(kv) > 4096 { os.Unsetenv(k) } }

Try / catch

// treat as transient: catch the wrap from InitTracing, re-export the named OTEL_* var cleanly, retry the command once

Prevention

When it happens

Trigger: traceClientFromDockerContext with a non-empty otelEnv map (OTEL_* vars present) and os.Unsetenv(k) failing — realistically only under memory-allocation failure or platform-specific env corruption; returned as an error, joined into InitProvider's result.

Common situations: Running compose with OTEL_EXPORTER_OTLP_* vars exported while the process hits memory pressure; containerized CI environments with very low memory limits at startup.

Related errors


AI-assisted analysis of docker/compose@ddc4b044b6 (2026-08-15). Data as JSON: /api/errors/a85cd63a36eca777. Report an issue: GitHub.