ocaml-community / yojson

Low-level JSON parsing and pretty-printing library for OCaml
https://ocaml-community.github.io/yojson/
BSD 3-Clause "New" or "Revised" License
322 stars 58 forks source link

thread-safety #183

Open vzaliva opened 1 week ago

vzaliva commented 1 week ago

Currently, yojson is not safe to use within domains, causing random Stdlib.Queue.Empty exceptions. The reason is that it is using Format, which uses a shared state by default.

The format issue was addressed in part by https://github.com/ocaml/ocaml/issues/10453, but it requires code changes.

vzaliva commented 1 week ago

I suspect the issue is around formatter_of_out_channel use which should be replaced with synchronized_formatter_of_out_channel

Leonidas-from-XIV commented 2 days ago

Thanks for the report, however it looks like Format.synchronized_formatter_of_out_channel is marked as "unstable" (and possibly only available in 5.x, but the latter can be worked around with conditional compilation). That's a rather regrettable state of affairs.

But Format.formatter_of_out_channel is only used in to_channel, the user can use pp with their own formatter (so possibly the synchornized one). Does that solve your the thread safety issue?

It would be nice to have a repro-case but I understand that building reliable thread-safety repro cases is pretty difficult.