I’m having the same problem with my wordpress. I did notice something when this happens.
The error occurs when a draft of my entry has been temporarily saved (by the auto save function) in my server.
I don’t know what it does or what happens really, but when I try to publish and a tempy draft has been made, a prompt happens asking me if I want to edit post. Naturally I say Yes. and then an error (much like the ones posted here) happens.
My rule of thumb now is to copy paste pre-existing codes before WordPress even saves. It’s a little meticulous. ;A; I hope this gets fixed. ;A;