*** a/src/bin/pg_basebackup/pg_basebackup.c
--- b/src/bin/pg_basebackup/pg_basebackup.c
***************
*** 584,589 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
--- 584,590 ----
  				{
  					fprintf(stderr, _("%s: could not write to compressed file \"%s\": %s\n"),
  							progname, filename, get_gz_error(ztarfile));
+ 					disconnect_and_exit(1);
  				}
  			}
  			else
***************
*** 600,606 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
  			if (strcmp(basedir, "-") == 0)
  			{
  #ifdef HAVE_LIBZ
! 				if (ztarfile)
  					gzclose(ztarfile);
  #endif
  			}
--- 601,607 ----
  			if (strcmp(basedir, "-") == 0)
  			{
  #ifdef HAVE_LIBZ
! 				if (ztarfile != NULL)
  					gzclose(ztarfile);
  #endif
  			}
***************
*** 609,617 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
  #ifdef HAVE_LIBZ
  				if (ztarfile != NULL)
  					gzclose(ztarfile);
  #endif
! 				if (tarfile != NULL)
! 					fclose(tarfile);
  			}
  
  			break;
--- 610,625 ----
  #ifdef HAVE_LIBZ
  				if (ztarfile != NULL)
  					gzclose(ztarfile);
+ 				else
  #endif
! 				{
! 					if (fclose(tarfile) != 0)
! 					{
! 						fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
! 								progname, filename, strerror (errno));
! 						disconnect_and_exit(1);
! 					}
! 				}
  			}
  
  			break;
***************
*** 630,635 **** ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
--- 638,644 ----
  			{
  				fprintf(stderr, _("%s: could not write to compressed file \"%s\": %s\n"),
  						progname, filename, get_gz_error(ztarfile));
+ 				disconnect_and_exit(1);
  			}
  		}
  		else