From b6fa55aa371fdefbf1b54c32c5a122ac1afafeb0 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <thinkyhead@users.noreply.github.com>
Date: Sat, 3 Mar 2018 23:13:53 -0600
Subject: [PATCH] Fix: M112 calling kill from interrupt (#9922)

Fix #9906
---
 Marlin/MarlinSerial.cpp |  4 +++-
 Marlin/MarlinSerial.h   | 17 ++++++++++-------
 Marlin/temperature.cpp  |  4 ++++
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/Marlin/MarlinSerial.cpp b/Marlin/MarlinSerial.cpp
index 36b8f2bdf5..dfea17cffc 100644
--- a/Marlin/MarlinSerial.cpp
+++ b/Marlin/MarlinSerial.cpp
@@ -80,6 +80,8 @@
 
   #if ENABLED(EMERGENCY_PARSER)
 
+    bool killed_by_M112; // = false
+
     #include "stepper.h"
     #include "language.h"
 
@@ -155,7 +157,7 @@
                 wait_for_user = wait_for_heatup = false;
                 break;
               case state_M112:
-                kill(PSTR(MSG_KILLED));
+                killed_by_M112 = true;
                 break;
               case state_M410:
                 quickstop_stepper();
diff --git a/Marlin/MarlinSerial.h b/Marlin/MarlinSerial.h
index b3b5096591..8603b654f9 100644
--- a/Marlin/MarlinSerial.h
+++ b/Marlin/MarlinSerial.h
@@ -21,13 +21,12 @@
  */
 
 /**
-  MarlinSerial.h - Hardware serial library for Wiring
-  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
-
-  Modified 28 September 2010 by Mark Sproul
-  Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
-
-*/
+ * MarlinSerial.h - Hardware serial library for Wiring
+ * Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
+ *
+ * Modified 28 September 2010 by Mark Sproul
+ * Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
+ */
 
 #ifndef MARLINSERIAL_H
 #define MARLINSERIAL_H
@@ -102,6 +101,10 @@
     extern ring_buffer_pos_t rx_max_enqueued;
   #endif
 
+  #if ENABLED(EMERGENCY_PARSER)
+    extern bool killed_by_M112;
+  #endif
+
   class MarlinSerial { //: public Stream
 
     public:
diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 2c00db9759..2d0a294fb7 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -744,6 +744,10 @@ void Temperature::manage_heater() {
     static bool last_pause_state;
   #endif
 
+  #if ENABLED(EMERGENCY_PARSER)
+    if (killed_by_M112) kill(PSTR(MSG_KILLED));
+  #endif
+
   if (!temp_meas_ready) return;
 
   updateTemperaturesFromRawValues(); // also resets the watchdog