pulsar icon indicating copy to clipboard operation
pulsar copied to clipboard

[bug][broker] Broker interceptor (NAR) causes HTTP 400 FAIL_ON_TRAILING_TOKENS on admin API calls in 4.0.12

Open jamf-software opened this issue 4 months ago • 7 comments

Search before reporting

  • [x] I searched in the issues and found nothing similar.

Read release policy

  • [x] I understand that unsupported versions don't get bug fixes. I will attempt to reproduce the issue on a supported version of Pulsar client and Pulsar broker.

User environment

Environment

│ Pulsar │ 4.0.12
│ Java │ 21
│ Jackson BOM │ 2.18.8
│ jersey-media-json-jackson │ 2.42
│ OS │ Linux (Docker)

Works on: Pulsar 4.0.11 (Jackson BOM 2.18.6) Fails on: Pulsar 4.0.12 (Jackson BOM 2.18.8) when any broker interceptor NAR is loaded

Issue Description

When a broker interceptor NAR is loaded in Pulsar 4.0.12, admin API calls with a JSON request body (e.g. resetSubscriptionToPosition, setBookieRack) fail with HTTP 400:

javax.ws.rs.BadRequestException: HTTP 400 Trailing token (of type START_OBJECT) found after value (bound as org.apache.pulsar.client.impl.ResetCursorData): not allowed as per DeserializationFeature.FAIL_ON_TRAILING_TOKENS

The same NAR works correctly on Pulsar 4.0.11. Without the NAR, 4.0.12 works correctly. This is a regression introduced between 4.0.11 and 4.0.12.

Root cause analysis

PreInterceptFilter is only registered when a BrokerInterceptor is loaded. It wraps every incoming HTTP request in a RequestWrapper that buffers the body. The wrapped request is then passed to Jersey for deserialization.

jackson-jaxrs-providers (used by Jersey) enables FAIL_ON_TRAILING_TOKENS unconditionally via JaxRSFeature.READ_FULL_STREAM (default true since 2.15.0):

// ProviderBase._configForReading(): if (JaxRSFeature.READ_FULL_STREAM.enabledIn(_jaxRSFeatures)) { r = r.withFeatures(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); }

This means any trailing bytes after the valid JSON in the body will cause a 400. The feature was active in 4.0.11 as well, but no trailing bytes were present.

Between 4.0.11 and 4.0.12, the Jackson BOM bumped from 2.18.6 to 2.18.8. The error appears on the forwarded request leg (note the AsyncHttpClient thread) — when broker A forwards an admin call to the partition-owning broker B, broker B's PreInterceptFilter receives the forwarded request and Jersey rejects it. This suggests the forwarded request body may be constructed differently in 4.0.12, resulting in trailing content.

Investigation confirmed:

  • PreInterceptFilter and RequestWrapper class bytecode is identical between 4.0.11 and 4.0.12
  • The RequestWrapper IOUtils.toByteArray(Reader, Charset) round-trip is byte-for-byte lossless (verified by unit test)
  • FAIL_ON_TRAILING_TOKENS is not referenced anywhere in Pulsar's own codebase — it is enabled by the JAX-RS provider
  • Jersey and Jetty versions are unchanged between 4.0.11 and 4.0.12

The precise source of the trailing content in the forwarded request body is still under investigation.

Error messages

Error log

  [AsyncHttpClient-50-1] WARN  org.apache.pulsar.broker.admin.impl.PersistentTopicsBase -
  [persistent://public/default/my-topic] got an error while copying the subscription to the
  partition javax.ws.rs.BadRequestException: HTTP 400 Trailing token (of type START_OBJECT)
  found after value (bound as `org.apache.pulsar.client.impl.ResetCursorData`): not allowed
  as per `DeserializationFeature.FAIL_ON_TRAILING_TOKENS`

Reproducing the issue

  1. Build any broker interceptor NAR and load it via brokerInterceptors config
  2. Create a partitioned topic
  3. Call resetSubscriptionToPosition (or any admin API that POSTs a JSON body) on that topic
  4. Observe HTTP 400 in the broker logs, originating from the forwarded request to the partition-owning broker

Additional information

Possible fix

RequestWrapper reads the body using a charset round-trip: IOUtils.toByteArray(new InputStreamReader(request.getInputStream()), Charset.defaultCharset())

This could be simplified to a direct stream read with no charset conversion: IOUtils.toByteArray(request.getInputStream())

This is safer and avoids any potential encoding-related body mutation. However, since we confirmed the round-trip is lossless for ASCII JSON, the real fix likely needs to address how the forwarded request body is constructed in the partition-copy path.

Are you willing to submit a PR?

  • [ ] I'm willing to submit a PR!

jamf-software avatar Jul 13 '26 15:07 jamf-software

A few additional findings from our investigation that help narrow this down and confirm the root cause is broker-side (not the NAR):

1. Excluding Jackson from the interceptor NAR does NOT help. We rebuilt our interceptor NAR with jackson-databind, jackson-core and jackson-annotations excluded from the bundle (so the interceptor uses the broker-provided Jackson only). The 400 still reproduces identically. This rules out the "NAR ships a conflicting Jackson" hypothesis and points squarely at the broker-side PreInterceptFilter / RequestWrapper body handling.

2. Deterministic isolation on a live broker (interceptor on vs off). Same broker, same request, single variable flipped:

  • brokerInterceptors=[pulsar-interceptor] → admin POST/PUT returns HTTP 400 FAIL_ON_TRAILING_TOKENS
  • brokerInterceptors=[] (broker recreated) → same request returns 204

So the filter chain installed when any interceptor is present is both necessary and sufficient to trigger it — consistent with PreInterceptFilter being registered unconditionally on getBrokerInterceptor() != null in WebService.

3. Not specific to one endpoint — every admin POST/PUT with a JSON body fails. Reproduced on multiple endpoints, all with the same FAIL_ON_TRAILING_TOKENS error, only differing by the bound type:

  • POST .../namespaces/{tenant}/{ns}/retention → RetentionPolicies
  • PUT .../namespaces/{tenant}/{ns} → Policies
  • POST .../bookies/racks-info/{bookie}?group=... → BookieInfo

GET requests (no body) are unaffected. This confirms it's the buffered request-body replay in RequestWrapper, independent of the specific resource.

adambagniewski avatar Jul 13 '26 15:07 adambagniewski

Hi folks, thanks for the detailed investigation.

The reports so far seem to point to a broker-side issue in the request body replay path when PreInterceptFilter wraps requests with RequestWrapper, rather than a dependency conflict in the interceptor NAR.

I noticed that master already has a related RequestWrapper change in #25912: it buffers the request body lazily, bounds the read by Content-Length, and returns a stable ServletInputStream. That looks relevant to the FAIL_ON_TRAILING_TOKENS error reported here.

Could you share a minimal reproducer, such as a simple interceptor NAR plus the admin calls used to trigger the failure? It would also help to know whether the same reproducer still fails on the latest branch-4.0 and whether it passes on current master. This would help confirm whether this issue is the RequestWrapper body replay bug and whether #25912 is the relevant master-side fix.

void-ptr974 avatar Jul 14 '26 23:07 void-ptr974

Thanks @void-ptr974 — that matches exactly what we found, and we can now confirm #25912 is the relevant fix.

We took the RequestWrapper change from #25912 and applied it to branch-4.0 (4.0.12). Rather than rebuilding the whole distribution, we compiled only the patched RequestWrapper.java against the jars shipped in apachepulsar/pulsar:4.0.12 and replaced the RequestWrapper.class inside pulsar-broker-4.0.12.jar in the image. The interceptor and admin API are otherwise untouched.

Result: the 400 is gone. With the patched broker jar and the same interceptor still registered (brokerInterceptors=[pulsar-interceptor]), every admin POST/PUT with a JSON body that previously returned HTTP 400 FAIL_ON_TRAILING_TOKENS now returns 2xx.

Concrete before/after on a live broker, interceptor active in both cases:

  • POST /admin/v2/bookies/racks-info/{bookie}?group=... (binds BookieInfo) — was 400, now 204
  • PUT /admin/v2/persistent/{tenant}/{ns}/{topic}/subscription/{sub}/properties (binds java.util.Map) — was 400 on 3/3 attempts, now 204 on 3/3
  • Natural cluster traffic (background clients firing setSubscriptionProperties continuously) went from a steady stream of 400s to 0 × 400 over a 15-minute window — all 204 (plus the usual 307 proxy redirects)

GET requests were unaffected throughout, consistent with the buffered request-body replay being the root cause.

Summary of what the reproducer needs: any interceptor NAR registered via brokerInterceptors (its code doesn't matter — it doesn't even need to read the body; merely being present installs PreInterceptFilter unconditionally per getBrokerInterceptor() != null in WebService), plus any admin POST/PUT carrying a JSON body. On stock branch-4.0 (4.0.12, Jersey 2.46) it returns 400; with the #25912 RequestWrapper change applied it returns 2xx.

So on our side this confirms:

  • the issue is the broker-side RequestWrapper body-replay path, not the NAR;
  • #25912 is the correct fix, and it resolves the failure on the branch-4.0 codebase, not just master.

Would a backport of #25912 to branch-4.0 be acceptable?

adambagniewski avatar Jul 15 '26 16:07 adambagniewski

Thanks for confirming.

@lhotari could you take a look when you have a chance? #25912 seems to have the related RequestWrapper fix on master.

void-ptr974 avatar Jul 18 '26 04:07 void-ptr974

Fix is #26223 for branch-4.2, this will be cherry-picked to branch-4.0

lhotari avatar Jul 22 '26 09:07 lhotari

Fix will be included in 4.0.13/4.2.4

lhotari avatar Jul 22 '26 14:07 lhotari

thx!

adambagniewski avatar Jul 23 '26 14:07 adambagniewski