diff --git a/compiler/GHC/Utils/Error.hs b/compiler/GHC/Utils/Error.hs
index 999bc49ac2bcad5b3aaacf76b0db5d677f8e862e..1597a7b8c34a743cad7c086bcbf4957a6d668796 100644
--- a/compiler/GHC/Utils/Error.hs
+++ b/compiler/GHC/Utils/Error.hs
@@ -414,7 +414,7 @@ withTiming' :: MonadIO m
             -> m a
 withTiming' logger what force_result prtimings action
   = if logVerbAtLeast logger 2 || logHasDumpFlag logger Opt_D_dump_timings
-    then do whenPrintTimings $
+    then do when printTimingsNotDumpToFile $ liftIO $
               logInfo logger $ withPprStyle defaultUserStyle $
                 text "***" <+> what <> colon
             let ctx = log_default_user_context (logFlags logger)
@@ -432,7 +432,7 @@ withTiming' logger what force_result prtimings action
             let alloc = alloc0 - alloc1
                 time = realToFrac (end - start) * 1e-9
 
-            when (logVerbAtLeast logger 2 && prtimings == PrintTimings)
+            when (logVerbAtLeast logger 2 && printTimingsNotDumpToFile)
                 $ liftIO $ logInfo logger $ withPprStyle defaultUserStyle
                     (text "!!!" <+> what <> colon <+> text "finished in"
                      <+> doublePrec 2 time
@@ -452,7 +452,16 @@ withTiming' logger what force_result prtimings action
             pure r
      else action
 
-    where whenPrintTimings = liftIO . when (prtimings == PrintTimings)
+    where whenPrintTimings =
+            liftIO . when printTimings
+
+          printTimings =
+            prtimings == PrintTimings
+
+          -- Avoid both printing to console and dumping to a file (#20316).
+          printTimingsNotDumpToFile =
+            printTimings
+            && not (log_dump_to_file (logFlags logger))
 
           recordAllocs alloc =
             liftIO $ traceMarkerIO $ "GHC:allocs:" ++ show alloc
diff --git a/testsuite/tests/driver/T20316.stderr b/testsuite/tests/driver/T20316.stderr
deleted file mode 100644
index ef979dec18abbac62991856672aead956b38cda5..0000000000000000000000000000000000000000
--- a/testsuite/tests/driver/T20316.stderr
+++ /dev/null
@@ -1,6 +0,0 @@
-*** initializing unit database:
-*** Chasing dependencies:
-*** Parser [Main]:
-*** Renamer/typechecker [Main]:
-*** Desugar [Main]:
-*** CoreTidy [Main]: