The problem with the delay begins here, line 143 specifically in the return statement.

You may wait 2 seconds . and it will loop . but it will stop looping at 10 seconds (eof_timeout) and then return. This will result in all data within buffer getting lost. Then when we re-enter, and finally get a new line, we have lost half of the line.

The delay doesn't help with that as it doesn't avoid the 10 second.

Note that if we lose data here - we return less data and the authoritative offset talked about previously ends up behind too much. Then on resume we duplicate events or send incomplete corrupt events again.

Jason