If you traverse up the super class chain you will eventually see that you inherit from IO
. Most IO
objects, in Ruby, buffer the data to be more efficient writing and reading from disk.
In your case, the buffer wasn't large enough (or enough time didn't pass) for it to flush out. However, when you closed the socket, this forced a flush of the buffer resources.
You have a few options:
- You can manually force the buffer to flush using
IO#flush
. - You can set the buffer to always sync after a write / read, by setting
IO#sync=
totrue
. You can check the status of yourIO
object's syncing usingIO#sync
; I'm guessing you'd seesocket.sync #=> false
- Use
BasicSocket#send
which will call POSIXsend(2)
; since sockets are initialized withO_FSYNC
set, the send will be synchronous and atomic.