diff --git a/src/bin/pg_waldump/archive_waldump.c b/src/bin/pg_waldump/archive_waldump.c
index b078c2d6960..023a549ae9e 100644
--- a/src/bin/pg_waldump/archive_waldump.c
+++ b/src/bin/pg_waldump/archive_waldump.c
@@ -474,7 +474,11 @@ get_archive_wal_entry(const char *fname, XLogDumpPrivate *privateInfo)
 		entry = ArchivedWAL_lookup(privateInfo->archive_wal_htab, fname);

 		if (entry != NULL)
+		{
+			if (write_fp != NULL)
+				fclose(write_fp);
 			return entry;
+		}

 		/*
 		 * Capture the current entry before calling read_archive_file(),
diff --git a/src/bin/pg_waldump/meson.build b/src/bin/pg_waldump/meson.build
index 5296f21b82c..d2b4bd0c048 100644
--- a/src/bin/pg_waldump/meson.build
+++ b/src/bin/pg_waldump/meson.build
@@ -34,6 +34,7 @@ tests += {
     'tests': [
       't/001_basic.pl',
       't/002_save_fullpage.pl',
+      't/003_archive.pl',
     ],
   },
 }
--
2.43.0
