lib: correctly handle read failure