Tencent/WeKnora · error

memory limit cannot be negative

Error message

memory limit cannot be negative

What it means

ValidateConfig rejects a SandboxConfig whose MaxMemory is negative. Memory limits are non-negative byte counts; a negative value is meaningless and indicates a config or parsing mistake, so Manager creation fails.

Source

Thrown at internal/sandbox/sandbox.go:382

// ValidateConfig validates sandbox configuration
func ValidateConfig(config *Config) error {
	if config == nil {
		return errors.New("config is nil")
	}

	switch config.Type {
	case SandboxTypeDocker, SandboxTypeCube, SandboxTypeE2B, SandboxTypeDisabled:
		// Valid types
	default:
		return errors.New("invalid sandbox type")
	}

	if config.DefaultTimeout < 0 {
		return errors.New("timeout cannot be negative")
	}

	if config.MaxMemory < 0 {
		return errors.New("memory limit cannot be negative")
	}

	if config.MaxCPU < 0 {
		return errors.New("CPU limit cannot be negative")
	}

	return nil
}

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Set MaxMemory to a non-negative byte count (0 = no limit), e.g. 512 << 20 for 512MiB.
  2. Fix unit conversion/parsing code so memory values are parsed with checked/unsigned logic.
  3. Validate MaxMemory >= 0 before constructing the Manager.

Example fix

// before
cfg.MaxMemory = -512 * 1024 * 1024
// after
cfg.MaxMemory = 512 * 1024 * 1024
Defensive patterns

Strategy: validation

Validate before calling

if cfg.MaxMemory < 0 {
    return fmt.Errorf("invalid sandbox config: MaxMemory=%d must be >= 0", cfg.MaxMemory)
}

Try / catch

if err := sandbox.ValidateConfig(cfg); err != nil {
    if strings.Contains(err.Error(), "memory") {
        return fmt.Errorf("fix MaxMemory (got %d bytes)", cfg.MaxMemory)
    }
    return err
}

Prevention

When it happens

Trigger: Passing a SandboxConfig with MaxMemory < 0 to NewManager or NewSessionBoundManager, e.g. from a negative number in a config file or a bad unit conversion / integer underflow.

Common situations: Entering '-512m' in a limits section; signed-int unit conversion that underflowed; copying an example with a flipped sign.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/76a32cb91404ca07. Report an issue: GitHub.