Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement stack based json stream that guarantees valid json in case of errors #10389

Open
taratorio opened this issue May 17, 2024 · 0 comments
Assignees

Comments

@taratorio
Copy link
Contributor

Relates to a group of issues related to invalid json responses in our trace_* and debug_* APIs in error cases due to the fact we use json streaming:

Json is not streaming friendly as in case of errors we may end up flushing invalid json with dangling (unclosed) arrays, objects, fields. API implementations need to keep track of these and make sure they correctly close them in case of errors. This is very error prone, difficult and results in messy code.

A simple solution to guarantee that our JSON streams are always valid no matter what is to use a stack based Stream wrapper around jsoniter.Stream:

  • use a Stack to keep track of unclosed "{" and "[" and "unfinished object field writes", "unfinished WriteMore commas", etc.
  • in case of errors at the rpc/handler level call stackStream.ClosePendingObjects which pops the stack and correctly closes nested-ness

Alex mentioned TrueBlocks use a similar approach to handle this problem. We could explore their solution too and potentially re-use if it is easy to.

This way API implementations that use streaming no longer have to worry about closing dangling json in case of errors. They can simply return an error and let the rpc/handler do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant