apache/druid · error · JsonMappingException

Premature EOF

Error message

Premature EOF

What it means

ResponseContextDeserializer.skipTo throws JsonMappingException("Premature EOF") when the JSON parser returns a null token (end of input) before reaching the expected end token (END_OBJECT or END_ARRAY) while skipping an unknown key's structured value. The serialized response context is truncated or unbalanced.

Source

Thrown at processing/src/main/java/org/apache/druid/query/context/ResponseContextDeserializer.java:112

      default:
        if (token.isScalarValue()) {
          return;
        }
        throw new JsonMappingException(jp, "Invalid JSON inside unknown key: " + key);
    }
  }

  /**
   * Freewheel over the contents of a structured object, including any
   * nested structured objects, until the given end token.
   */
  private void skipTo(final JsonParser jp, JsonToken end) throws IOException
  {
    while (true) {
      jp.nextToken();
      final JsonToken token = jp.currentToken();
      if (token == null) {
        throw new JsonMappingException(jp, "Premature EOF");
      }
      switch (token) {
        case START_OBJECT:
          skipTo(jp, JsonToken.END_OBJECT);
          break;
        case START_ARRAY:
          skipTo(jp, JsonToken.END_ARRAY);
          break;
        default:
          if (token == end) {
            return;
          }
      }
    }
  }
}

View on GitHub (pinned to 9b90983fd2)

Solutions

  1. Inspect/repair the source header — check for truncation at proxies or load balancers (max header size settings)
  2. Reduce response-context size (fewer keys) or upgrade Druid versions that trim context contents
  3. Regenerate the request/response; the context is a per-request value so retry with a fresh query
  4. Catch JsonMappingException and fall back to an empty/default ResponseContext for the affected key

Example fix

// before
String ctx = request.getHeader("X-Druid-Response-Context"); // may be truncated
ResponseContext rc = mapper.readValue(ctx, ResponseContext.class);
// after
try {
  ResponseContext rc = mapper.readValue(ctx, ResponseContext.class);
} catch (JsonMappingException e) {
  log.warn(e, "Bad response context header; ignoring");
}
Defensive patterns

Strategy: try-catch

Validate before calling

// check balanced JSON before deserializing
try { mapper.readTree(headerValue); } catch (JsonProcessingException e) { /* header truncated/corrupt */ }

Try / catch

try { rc = mapper.readValue(headerValue, ResponseContext.class); } catch (JsonMappingException e) { log.warn(e, "Premature EOF in response context"); rc = ResponseContext.createEmpty(); }

Prevention

When it happens

Trigger: Deserializing a response context whose JSON is truncated — e.g. a header cut off by proxy limits, Base64 corruption, or a nested object/array that was never closed.

Common situations: HTTP header size limits truncating X-Druid-Response-Context on large contexts (many tasks/segments); intermediaries stripping or mangling headers; version-skewed serialization formats.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/a752d8bb309e13f2. Report an issue: GitHub.