jackson-core: `FilteringParserDelegate` can go into an infinite loop if underlying parser is non-blocking

Jackson version (BOM): 2.15.2

While experimenting with JsonPointer in Spring Framework, I tried to build up on the existing FilteringParserDelegate and associated JsonPointerBasedTokenFilter. Unfortunately, this doesn’t seem to work with a non-blocking JsonParser delegate, for several reasons:

  • it doesn’t delegate method canParseAsync() (ends up returning the default interface implem => false)
  • it doesn’t delegate method getNonBlockingInputFeeder() (same as above => null)
  • it isn’t aware of JsonToken.NOT_AVAILABLE

While the first two are easily circumvented by extending FilteringParserDelegate, the last one is the truly problematic one. It results in going down the code path of a “scalar value”, and will continuously call delegate.nextToken() which continues to result in NOT_AVAILABLE.

This leads to an infinite loop when attempting to filter based on a JsonPointer on top of a non-blocking parser.

Is there anything fundamentally preventing FilteringParserDelegate from being compatible with non-blocking parsers that I might have overlooked? Otherwise I think it’s pretty close, hopefully that can be fixed 😄

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Re-reading what I said earlier, I think that my thinking was that:

  1. It is indeed NOT possible (or at least practical) to make things work transparently.
  2. However: some not-really-async usage (where input is all fed at once, so no JsonToken.NOT_AVAILABLE ever returned)
  3. Since we cannot distinguish ok vs not-ok cases, it’d be good to make existing construction cases explicitly fail with exception, if given async-parser but
  4. To support future “ok-because-I-know-it-is” usage, new construction/usage method(s) added for
  5. As an addition or alternative to (3) we should also make encountering of JsonToken.NOT_AVAILABLE just fail (if it doesn’t already).

As to test, executor; I’m pretty sure that was just meant to bail out from test when infinite loop occurs.

Does this make more sense?

I guess combination of failing on existing constructors, along with adding factory method that allows might work

Not sure what you mean by that. That said I can understand the apprehensiveness of failing the constructor and how to document that.

If you feel the risk is too big in terms of impact on existing users, I guess a big WARN logging would perhaps be the next best thing 🤷‍♂️ But from my perspective there is currently no point in accepting a non-blocking parser in a constructor nor in a factory method, since that will inevitably lead to infinite looping.

Changes definitely need to be in new minor version (at this point 2.17), not patch

👍

@simonbasle All legit, and also bit challenging to do… esp. since patch release typically should not change behavior minus bug fixes. And while there are tests, I think due to complexity of filtering delegation, there are still gaps.

For patch release, ideally I do think NOT_AVAILABLE should be recognized… yet that’s not a small change probably. On refusing constructing for async-parser (there is a method to call to check fortunately) – and maybe having alternate constructor/factory method that would allow it? (since one can, in theory, make it all work as long as all content for a doc is passed immediately) – that’d have to go in a new minor version (2.17).

I’d like to improve things here, and maybe I can. But it does need to fight wrt all other priorities.

But I’ll keep this on my ever-expanding todo-list.

hey @cowtowncoder, thanks for the analysis. unfortunately I don’t think I will have the possibility to try to add changes anytime soon 😞

hopefully there is some kind of low hanging fruit where the parsing doesn’t work but fails fast ? (e.g. throwing an exception if the underlying parser is returning NOT_AVAILABLE ? or even if the parser passed in the constructor is async ?) this wouldn’t really help in my case (async parsing of merely a sub-set of a document, pointed at by a JsonPointer), but at least it would avoid tricky livelock situations.