File 4171-Close-cpu_sup-and-memsup-port-on-terminate.patch of Package erlang

From 00cfb44f8535752ca25f32ac8d12e094adc04323 Mon Sep 17 00:00:00 2001
From: jdamanalo <jamanalo5@up.edu.ph>
Date: Mon, 16 Jan 2023 22:58:57 +0800
Subject: [PATCH] Close cpu_sup and memsup port on terminate

cpu_sup and memsup shutdown fixes
---
 lib/os_mon/c_src/memsup.c     |  2 ++
 lib/os_mon/c_src/memsup.h     |  2 ++
 lib/os_mon/include/memsup.hrl |  1 +
 lib/os_mon/src/cpu_sup.erl    | 11 ++++++++---
 lib/os_mon/src/memsup.erl     | 32 ++++++++++++++++++--------------
 5 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/lib/os_mon/c_src/memsup.c b/lib/os_mon/c_src/memsup.c
index 3cc13c905b..f4e727c39e 100644
--- a/lib/os_mon/c_src/memsup.c
+++ b/lib/os_mon/c_src/memsup.c
@@ -609,6 +609,8 @@ message_loop(int erlin_fd)
 		    case SHOW_SYSTEM_MEM:
 			extended_show_mem();
 			break;
+		    case EXIT:
+			return;
 		    default:	/* ignore all other messages */
 			break;
 		    }
diff --git a/lib/os_mon/c_src/memsup.h b/lib/os_mon/c_src/memsup.h
index fa65db784a..f9d0e225df 100644
--- a/lib/os_mon/c_src/memsup.h
+++ b/lib/os_mon/c_src/memsup.h
@@ -30,6 +30,8 @@
 /* Extended memory statistics */
 /*IG*/ #define SHOW_SYSTEM_MEM 2
 
+#define EXIT 3
+
 /* Tags for the extended statistics */
 /*IG*/ #define SHOW_SYSTEM_MEM_END 0
 /*IG*/ #define MEM_SYSTEM_TOTAL 1
diff --git a/lib/os_mon/include/memsup.hrl b/lib/os_mon/include/memsup.hrl
index 2f76cfe9b3..8f93918180 100644
--- a/lib/os_mon/include/memsup.hrl
+++ b/lib/os_mon/include/memsup.hrl
@@ -26,6 +26,7 @@
 
 -define( SHOW_MEM , 1 ).
 -define( SHOW_SYSTEM_MEM , 2 ).
+-define( EXIT , 3 ).
 -define( SHOW_SYSTEM_MEM_END , 8#0 ).
 %% tags for extended statistics
 -define( MEM_SYSTEM_TOTAL , 1 ).
diff --git a/lib/os_mon/src/cpu_sup.erl b/lib/os_mon/src/cpu_sup.erl
index b4283bb436..f68a59a2a2 100644
--- a/lib/os_mon/src/cpu_sup.erl
+++ b/lib/os_mon/src/cpu_sup.erl
@@ -495,9 +495,6 @@ measurement_server_init() ->
 
 measurement_server_loop(State) ->
     receive
-	{_, quit} ->
-	    State#internal.port ! {self(), ?quit}, 
-	    ok;
 	{'DOWN',Monitor,process,_,_} ->
 	    measurement_server_loop(State#internal{ util = lists:keydelete(
 		Monitor,
@@ -528,6 +525,14 @@ measurement_server_loop(State) ->
         {'EXIT', OldPid, _n} when State#internal.port == OldPid ->
 	    {ok, NewPid} = port_server_start_link(),
 	    measurement_server_loop(State#internal{port = NewPid});
+        {'EXIT', _, normal} ->
+            case State#internal.port of
+                not_used ->
+                    ok;
+                Srv ->
+                    Srv ! {self(), ?quit},
+                    ok
+            end;
 	_Other ->
 	    measurement_server_loop(State)
     end.
diff --git a/lib/os_mon/src/memsup.erl b/lib/os_mon/src/memsup.erl
index e4f60b9972..dbf7609433 100644
--- a/lib/os_mon/src/memsup.erl
+++ b/lib/os_mon/src/memsup.erl
@@ -651,6 +651,10 @@ port_init() ->
 start_portprogram() ->
     os_mon:open_port("memsup",[{packet,1}]).
 
+port_shutdown(Port) ->
+    Port ! {self(), {command, [?EXIT]}},
+    port_close(Port).
+
 %% The connected process loops are a bit awkward (several different
 %% functions doing almost the same thing) as
 %%   a) strategies for receiving regular memory data and extensive
@@ -674,13 +678,13 @@ port_idle(Port) ->
 	    %% Received after reply already has been delivered...
 	    port_idle(Port);
 	close ->
-	    port_close(Port);
+	    port_shutdown(Port);
 	{Port, {data, Data}} ->
 	    exit({port_error, Data});
 	{'EXIT', Port, Reason} ->
 	    exit({port_died, Reason});
 	{'EXIT', _Memsup, _Reason} ->
-	    port_close(Port)
+	    port_shutdown(Port)
     end.
 
 get_memory_usage(Port, Alloc, Memsup) ->
@@ -694,11 +698,11 @@ get_memory_usage(Port, Alloc, Memsup) ->
 	cancel ->
 	    get_memory_usage_cancelled(Port, Alloc);
 	close ->
-	    port_close(Port);
+	    port_shutdown(Port);
 	{'EXIT', Port, Reason} ->
 	    exit({port_died, Reason});
 	{'EXIT', _Memsup, _Reason} ->
-	    port_close(Port)
+	    port_shutdown(Port)
     end.
 get_memory_usage_cancelled(Port, Alloc) ->
     receive
@@ -707,11 +711,11 @@ get_memory_usage_cancelled(Port, Alloc) ->
 	{Port, {data, _Data}} ->
 	    port_idle(Port);
 	close ->
-	    port_close(Port);
+	    port_shutdown(Port);
 	{'EXIT', Port, Reason} ->
 	    exit({port_died, Reason});
 	{'EXIT', _Memsup, _Reason} ->
-	    port_close(Port)
+	    port_shutdown(Port)
     end.
 
 tag2atag(Port, Tag) ->
@@ -734,11 +738,11 @@ get_ext_memory_usage(Port, Accum, Memsup) ->
 	ext_cancel ->
 	    get_ext_memory_usage_cancelled(Port);
 	close ->
-	    port_close(Port);
+	    port_shutdown(Port);
 	{'EXIT', Port, Reason} ->
 	    exit({port_died, Reason});
 	{'EXIT', _Memsup, _Reason} ->
-	    port_close(Port)
+	    port_shutdown(Port)
     end.
 get_ext_memory_usage_cancelled(Port) ->
     receive
@@ -748,11 +752,11 @@ get_ext_memory_usage_cancelled(Port) ->
             get_ext_memory_usage_cancelled(tag2atag(Port, Tag),
                                            Port);
 	close ->
-	    port_close(Port);
+	    port_shutdown(Port);
 	{'EXIT', Port, Reason} ->
 	    exit({port_died, Reason});
 	{'EXIT', _Memsup, _Reason} ->
-	    port_close(Port)
+	    port_shutdown(Port)
     end.
 
 get_ext_memory_usage(ATag, Port, Accum0, Memsup) ->
@@ -769,22 +773,22 @@ get_ext_memory_usage(ATag, Port, Accum0, Memsup) ->
 	cancel ->
 	    get_ext_memory_usage_cancelled(ATag, Port);
 	close ->
-	    port_close(Port);
+	    port_shutdown(Port);
 	{'EXIT', Port, Reason} ->
 	    exit({port_died, Reason});
 	{'EXIT', _Memsup, _Reason} ->
-	    port_close(Port)
+	    port_shutdown(Port)
     end.
 get_ext_memory_usage_cancelled(_ATag, Port) ->
     receive
 	{Port, {data, _Data}} ->
 	    get_ext_memory_usage_cancelled(Port);
 	close ->
-	    port_close(Port);
+	    port_shutdown(Port);
 	{'EXIT', Port, Reason} ->
 	    exit({port_died, Reason});
 	{'EXIT', _Memsup, _Reason} ->
-	    port_close(Port)
+	    port_shutdown(Port)
     end.
 
 %%--Collect process data------------------------------------------------
-- 
2.35.3

openSUSE Build Service is sponsored by