|Description||It's possible to get two separate channels (an in_channel and an out_channel) to share the same operating-system-level file descriptor by using Unix.openfile in O_RDWR mode and then calling both Unix.in_channel_of_descr and Unix.out_channel_of_descr. I did this because it seemed like the intuitive way of getting hold of a file in a proper way for reading and writing simultaneously.|
I expected this to end with a single file pointer shared between the two channels, but at least for them to work properly as long as you were careful to always seek a channel before using it.
Unfortunately, this is not the case because the standard library adds some buffering to in_channel, and omits making the OS-level lseek() call in cases when a seek can be satisfied within the buffer. This means that if you (1) do a bit of reading with the in_channel, (2) seek a long way away with the out_channel and do some writing, and then (3) seek the in_channel back close to where you were reading earlier, then what happens is that the call to seek_in omits the lseek() syscall due to being satisfied in the buffer, but as you start reading, you run off the end of the buffer... but then the attempt to refill the buffer reads from the last place you *wrote*, because no lseek() was done!