FIX Flush on memory exhaustion and when headers sent

2.4 version of #2285