I’m using process-send-string to send data to a socket connection, but I’m not satisfied with the effect of this function. Roughly, calling (process-send-string "foo") may end up in sending "bar" and then "foo", as explained below.
As pointed out by Emacs maintainers, the C code of process-send-string calls a function wait_reading_process_output (even before actually writing anything), which may run timers, which in turn may call process-send-string, and no ordering is enforced between such nested calls.
This makes it virtually impossible to implement an RPC protocol which is intended to be used by hooks called at uncontrolled times. So my question is, how could we achieve an atomic, “synchronized” writing primitive for this purpose ?
Finally, an option that worked for me was to use the transaction queue API, which is higher level (so it cannot handle all sorts of protocols), but ensures correct ordering of alternating messages.