os.write() can hang, apparently

Wound up spending the whole afternoon tracking down an "only happens in production" bug where a sub-process would just hang, but only when big chunks of data were involved. Seems that os.write() was trying to write more than the target pipe could handle, so was winding up blocking. To tired to actually confirm that tonight, but if it winds up being confirmed, I'm going to have to go back and look at every case where I've used os.write(). I was under the assumption it would return immediately with the number of bytes written before blocking, something I now see is *not* necessarily implied in the documentation. Have to switch to traditional non-blocking IO on the pipes, it seems.


  1. Brandon Rhodes

    Brandon Rhodes on 12/13/2013 1:26 p.m. #

    Yes: on Unix, you ALWAYS have to ask for non-blocking I/O if you want the call to return immediately. Both socket calls (send, recv) and file/pipe calls (read, write) will block until at least 1 byte has been input or output.

    There is a small difference. Socket calls will then return, satisfied that they at least did something; whereas file calls will keep you blocked until either they write N bytes or the disk fills.

    But, in both cases the default behavior is blocking unless you decide to switch to non-blocking behavior.

Comments are closed.


Pingbacks are closed.