SonarSource/sonarqube · error · IllegalArgumentException

Connect timeout must be positive. Got

Error message

Connect timeout must be positive. Got 

What it means

OkHttpClientBuilder.setConnectTimeoutMs(long) validates its argument and throws IllegalArgumentException when a negative value is passed. Zero is allowed and means 'no timeout'; only negative milliseconds are rejected. This fails fast on invalid builder configuration before an OkHttpClient is created.

Solutions

  1. Pass a non-negative value: 0 disables the timeout, positive values are milliseconds
  2. Clamp the configured value before building: Math.max(0, configuredMs)
  3. Validate/correct the source configuration (properties, env var, YAML) that yields the negative number

Example fix

// before
builder.setConnectTimeoutMs(timeoutMs);
// after
builder.setConnectTimeoutMs(Math.max(0, timeoutMs));
Defensive patterns

Strategy: validation

Validate before calling

if (connectTimeoutMs < 0) throw new IllegalArgumentException("connectTimeoutMs must be >= 0, got " + connectTimeoutMs);
builder.setConnectTimeoutMs(Math.max(0, connectTimeoutMs));

Try / catch

try {
  builder.setConnectTimeoutMs(cfg.connectTimeoutMs());
} catch (IllegalArgumentException e) {
  log.warn("Bad connect timeout, using default", e);
}

Prevention

When it happens

Trigger: Calling setConnectTimeoutMs with any negative long (e.g. setConnectTimeoutMs(-1)), often from configuration code that computed the value dynamically or loaded a negative value from properties/environment, or a later subtraction producing a negative result.

Common situations: Config files or env vars containing negative timeout values; arithmetic like now - deadline going negative; unit confusion after conversions; copy-paste of a signed constant.

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 SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/9a6c878e1c6cc8f2. Report an issue: GitHub.

Appendix: source

Thrown at sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpClientBuilder.java:152

  }

  /**
   * Password used for proxy authentication. It is ignored if
   * proxy login is not defined (see {@link #setProxyLogin(String)}).
   * It can be null or empty when login is defined.
   */
  public OkHttpClientBuilder setProxyPassword(@Nullable String s) {
    this.proxyPassword = s;
    return this;
  }

  /**
   * Sets the default connect timeout for new connections. A value of 0 means no timeout.
   * Default is defined by OkHttp (10 seconds in OkHttp 3.3).
   */
  public OkHttpClientBuilder setConnectTimeoutMs(long l) {
    if (l < 0) {
      throw new IllegalArgumentException("Connect timeout must be positive. Got " + l);
    }
    this.connectTimeoutMs = l;
    return this;
  }

  /**
   * Set credentials that will be passed on every request
   */
  public OkHttpClientBuilder setCredentials(String credentials) {
    this.credentials = credentials;
    return this;
  }

  /**
   * Sets the default read timeout for new connections. A value of 0 means no timeout.
   * Default is defined by OkHttp (10 seconds in OkHttp 3.3).
   */
  public OkHttpClientBuilder setReadTimeoutMs(long l) {

View on GitHub (pinned to 184c821202)