- if ( fp == NULL || (rc = unlockFile(fileno((FILE *)fp))) ) {
- /* If the file handle has been explicitly closed
- * (via closeFile()) or freed, we will have given
- * up our process lock, so we silently return here.
- */
- return;
+ /* Don't close the file, just flush the buffer */
+ if (fo != NULL && fo->fd != -1) {
+ if (fo->buf != NULL && (fo->flags & FILEOBJ_FLUSH) && fo->bufWPtr > 0) {
+ /* Flush buffer contents */
+ writeBuffer((StgForeignObj)fo, fo->bufWPtr);