diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index e6d703abe740f2cb308a106b0f8013e252d2915c..fe37be66f2a854c8d1921327cee53754a568eb17 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1987,7 +1987,10 @@ process_file(char *filename, bool single_txn)
 		if ((res = PSQLexec("BEGIN", false)) == NULL)
 		{
 			if (pset.on_error_stop)
-				return EXIT_USER;
+			{
+				result = EXIT_USER;
+				goto error;
+			}
 		}
 		else
 			PQclear(res);
@@ -2000,13 +2003,19 @@ process_file(char *filename, bool single_txn)
 		if ((res = PSQLexec("COMMIT", false)) == NULL)
 		{
 			if (pset.on_error_stop)
-				return EXIT_USER;
+			{
+				result = EXIT_USER;
+				goto error;
+			}
 		}
 		else
 			PQclear(res);
 	}
 
-	fclose(fd);
+error:
+	if (fd != stdin)
+		fclose(fd);
+
 	pset.inputfile = oldfilename;
 	return result;
 }