diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc
index 70832513b6d9..0ab2b83bff9a 100644
--- a/gcc/diagnostic-format-sarif.cc
+++ b/gcc/diagnostic-format-sarif.cc
@@ -1539,6 +1539,14 @@ sarif_builder::on_report_diagnostic (const diagnostic_info &diagnostic,
   if (diagnostic.kind == DK_ICE || diagnostic.kind == DK_ICE_NOBT)
     {
       m_invocation_obj->add_notification_for_ice (diagnostic, *this);
+
+      /* Print a header for the remaining output to stderr, and
+	 return, attempting to print the usual ICE messages to
+	 stderr.  Hopefully this will be helpful to the user in
+	 indicating what's gone wrong (also for DejaGnu, for pruning
+	 those messages).   */
+      fnotice (stderr, "Internal compiler error:\n");
+
       return;
     }
 
@@ -3138,23 +3146,6 @@ sarif_builder::make_artifact_content_object (const char *text) const
   return content_obj;
 }
 
-/* Callback for diagnostic_context::ice_handler_cb for when an ICE
-   occurs.  */
-
-static void
-sarif_ice_handler (diagnostic_context *context)
-{
-  /* Attempt to ensure that a .sarif file is written out.  */
-  diagnostic_finish (context);
-
-  /* Print a header for the remaining output to stderr, and
-     return, attempting to print the usual ICE messages to
-     stderr.  Hopefully this will be helpful to the user in
-     indicating what's gone wrong (also for DejaGnu, for pruning
-     those messages).   */
-  fnotice (stderr, "Internal compiler error:\n");
-}
-
 class sarif_output_format : public diagnostic_output_format
 {
 public:
@@ -3387,9 +3378,6 @@ diagnostic_output_format_init_sarif (diagnostic_context &context,
   /* Suppress normal textual path output.  */
   context.set_path_format (DPF_NONE);
 
-  /* Override callbacks.  */
-  context.set_ice_handler_callback (sarif_ice_handler);
-
   /* Don't colorize the text.  */
   pp_show_color (fmt->get_printer ()) = false;
   context.set_show_highlight_colors (false);
diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc
index 41656a607b81..4a1e347776e9 100644
--- a/gcc/diagnostic.cc
+++ b/gcc/diagnostic.cc
@@ -284,7 +284,6 @@ diagnostic_context::initialize (int n_opts)
   m_diagnostic_groups.m_emission_count = 0;
   m_output_format = new diagnostic_text_output_format (*this);
   m_set_locations_cb = nullptr;
-  m_ice_handler_cb = nullptr;
   m_client_data_hooks = nullptr;
   m_diagrams.m_theme = nullptr;
   m_original_argv = nullptr;
@@ -782,16 +781,15 @@ diagnostic_context::action_after_output (diagnostic_t diag_kind)
     case DK_ICE:
     case DK_ICE_NOBT:
       {
-	/* Optional callback for attempting to handle ICEs gracefully.  */
-	if (void (*ice_handler_cb) (diagnostic_context *) = m_ice_handler_cb)
+	/* Attempt to ensure that any outputs are flushed e.g. that .sarif
+	   files are written out.
+	   Only do it once.  */
+	static bool finishing_due_to_ice = false;
+	if (!finishing_due_to_ice)
 	  {
-	    /* Clear the callback, to avoid potentially re-entering
-	       the routine if there's a crash within the handler.  */
-	    m_ice_handler_cb = NULL;
-	    ice_handler_cb (this);
+	    finishing_due_to_ice = true;
+	    finish ();
 	  }
-	/* The context might have had diagnostic_finish called on
-	   it at this point.  */
 
 	struct backtrace_state *state = NULL;
 	if (diag_kind == DK_ICE)
diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h
index 83d73d2c4b70..dbb3803ffb19 100644
--- a/gcc/diagnostic.h
+++ b/gcc/diagnostic.h
@@ -468,7 +468,6 @@ public:
   friend class diagnostic_source_print_policy;
   friend class diagnostic_text_output_format;
 
-  typedef void (*ice_handler_callback_t) (diagnostic_context *);
   typedef void (*set_locations_callback_t) (diagnostic_context *,
 					    diagnostic_info *);
 
@@ -587,10 +586,6 @@ public:
   {
     m_escape_format = val;
   }
-  void set_ice_handler_callback (ice_handler_callback_t cb)
-  {
-    m_ice_handler_cb = cb;
-  }
 
   /* Various accessors.  */
   bool warning_as_error_requested_p () const
@@ -859,9 +854,6 @@ private:
      of a diagnostic's location.  */
   set_locations_callback_t m_set_locations_cb;
 
-  /* Optional callback for attempting to handle ICEs gracefully.  */
-  ice_handler_callback_t m_ice_handler_cb;
-
   /* A bundle of hooks for providing data to the context about its client
      e.g. version information, plugins, etc.
      Used by SARIF output to give metadata about the client that's
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_xhtml_format.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_xhtml_format.c
index 2ce8cc29660c..c74ecb018efa 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_xhtml_format.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_xhtml_format.c
@@ -362,7 +362,15 @@ void
 xhtml_builder::on_report_diagnostic (const diagnostic_info &diagnostic,
 				     diagnostic_t orig_diag_kind)
 {
-  // TODO: handle (diagnostic.kind == DK_ICE || diagnostic.kind == DK_ICE_NOBT)
+  if (diagnostic.kind == DK_ICE || diagnostic.kind == DK_ICE_NOBT)
+    {
+      /* Print a header for the remaining output to stderr, and
+	 return, attempting to print the usual ICE messages to
+	 stderr.  Hopefully this will be helpful to the user in
+	 indicating what's gone wrong (also for DejaGnu, for pruning
+	 those messages).   */
+      fnotice (stderr, "Internal compiler error:\n");
+    }
 
   auto diag_element
     = make_element_for_diagnostic (diagnostic, orig_diag_kind);
@@ -573,23 +581,6 @@ xhtml_builder::flush_to_file (FILE *outf)
   fprintf (outf, "\n");
 }
 
-/* Callback for diagnostic_context::ice_handler_cb for when an ICE
-   occurs.  */
-
-static void
-xhtml_ice_handler (diagnostic_context *context)
-{
-  /* Attempt to ensure that a .xhtml file is written out.  */
-  diagnostic_finish (context);
-
-  /* Print a header for the remaining output to stderr, and
-     return, attempting to print the usual ICE messages to
-     stderr.  Hopefully this will be helpful to the user in
-     indicating what's gone wrong (also for DejaGnu, for pruning
-     those messages).   */
-  fnotice (stderr, "Internal compiler error:\n");
-}
-
 class xhtml_output_format : public diagnostic_output_format
 {
 public:
@@ -707,9 +698,6 @@ static void
 diagnostic_output_format_init_xhtml (diagnostic_context &context,
 				     std::unique_ptr<xhtml_output_format> fmt)
 {
-  /* Override callbacks.  */
-  context.set_ice_handler_callback (xhtml_ice_handler);
-
   /* Don't colorize the text.  */
   pp_show_color (fmt->get_printer ()) = false;
   context.set_show_highlight_colors (false);
