fix leaked file descriptor; improved error messages from C-land